-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.tsx
169 lines (145 loc) · 5.06 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import React from 'react';
function shadeColor(color, percent) {
let R = parseInt(color.substring(1, 3), 16);
let G = parseInt(color.substring(3, 5), 16);
let B = parseInt(color.substring(5, 7), 16);
R = parseInt(`${(R * (100 + percent)) / 100}`, 10);
G = parseInt(`${(G * (100 + percent)) / 100}`, 10);
B = parseInt(`${(B * (100 + percent)) / 100}`, 10);
R = R < 255 ? R : 255;
G = G < 255 ? G : 255;
B = B < 255 ? B : 255;
const RR = R.toString(16).length === 1 ? `0${ R.toString(16)}` : R.toString(16);
const GG = G.toString(16).length === 1 ? `0${ G.toString(16)}` : G.toString(16);
const BB = B.toString(16).length === 1 ? `0${ B.toString(16)}` : B.toString(16);
return `#${ RR }${GG }${BB}`;
}
const DEFAULT_RADIUS = 60;
const MAX_VALUE = 10;
const Direction = {
CLOCKSWISE: -1,
ANTI_CLOCKWISE: 1,
};
type Props = {
maxValue?: number,
selectedValue?: number,
radius?: number,
strokeWidth?: number,
label?: string,
activeStrokeColor?: string,
inactiveStrokeColor?: string,
labelFontSize?: number,
valueFontSize?: number,
withGradient?: boolean,
anticlockwise?: boolean,
initialAngularDisplacement?: number,
backgroundColor?: string,
textColor?: string,
};
const CircularProgressBar = ({
maxValue,
selectedValue,
radius,
strokeWidth,
label,
activeStrokeColor,
inactiveStrokeColor,
backgroundColor,
textColor,
labelFontSize,
valueFontSize,
withGradient,
anticlockwise,
initialAngularDisplacement,
}:Props) => {
// ---- PIE calculation funciton --------
const generatePie = (value) => {
const x = radius - Math.cos((2 * Math.PI) / (100 / value)) * radius;
const y = radius + Math.sin((2 * Math.PI) / (100 / value)) * radius;
const long = value <= 50 ? 0 : 1;
const d = `M${radius} ${radius} L${radius} ${0} A${radius} ${radius} 0 ${long} 1 ${y} ${x} Z`;
return d;
};
// ---- PIE Area calculation --------
const calculatePieValue = (numberOfBars) => {
const angle = 360 / numberOfBars;
const pieValue = Math.floor(angle / 4);
return pieValue < 1 ? 1 : Math.floor(angle / 4);
};
// ---- PIE render funciton --------
const renderPie = (i) => {
const DIRECTION = anticlockwise ? Direction.ANTI_CLOCKWISE : Direction.CLOCKSWISE;
// Rotation Calculation
const primaryRotationAngle = (maxValue - 1) * (360 / maxValue);
const rotationAngle = DIRECTION * initialAngularDisplacement
+ -1 * DIRECTION * primaryRotationAngle
+ i * DIRECTION * primaryRotationAngle;
const rotationTransformation = `rotate(${rotationAngle}, ${radius}, ${radius})`;
const pieValue = calculatePieValue(maxValue);
const dValue = generatePie(pieValue);
const activeColor = withGradient
? shadeColor(activeStrokeColor, ((i + 1) * maxValue) / 50)
: activeStrokeColor;
const fillColor = selectedValue > 0 && i <= selectedValue
? activeColor : inactiveStrokeColor;
return (
<path
style={{ opacity: i === 0 ? 0 : 1 }}
key={i}
d={dValue}
fill={fillColor}
transform={rotationTransformation}
/>
);
};
// ---- Creates a circle by combining the Pie(s) --------
const renderOuterCircle = () => [...Array(maxValue + 1)].map((e, i) => renderPie(i));
const labelView = (
<text
fill={textColor}
fontSize={labelFontSize}
x={radius}
y={radius + labelFontSize}
textAnchor="middle"
>
{label}
</text>
);
const textValueY = label ? radius : radius + valueFontSize / 3;
// -------- MAIN Render --------
return (
<svg width={radius * 2} height={radius * 2}>
{renderOuterCircle()}
{/* This is the overlay circle */}
<circle r={radius - strokeWidth} cx={radius} cy={radius} fill={backgroundColor} />
<text
fill={textColor}
fontSize={valueFontSize}
fontWeight="bold"
x={radius}
y={textValueY}
textAnchor="middle"
>
{selectedValue}
</text>
{!!label.length && labelView}
</svg>
);
};
CircularProgressBar.defaultProps = {
maxValue: MAX_VALUE,
selectedValue: 0,
radius: DEFAULT_RADIUS,
strokeWidth: DEFAULT_RADIUS / 10,
label: '',
activeStrokeColor: '#05a168',
inactiveStrokeColor: '#ddd',
backgroundColor: '#fff',
textColor: '#000',
labelFontSize: Math.floor(DEFAULT_RADIUS / 3),
valueFontSize: Math.floor(DEFAULT_RADIUS / 2.5),
withGradient: false,
anticlockwise: false,
initialAngularDisplacement: 0,
};
export default CircularProgressBar;