-
-
Notifications
You must be signed in to change notification settings - Fork 656
/
Gauge.tsx
192 lines (177 loc) · 5.85 KB
/
Gauge.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { Fragment, type VFC } from 'react';
import { Box, useTheme } from '@mui/material';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
const polarToCartesian = (
centerX: number,
centerY: number,
radius: number,
angleInRadians: number,
) => {
return {
x: centerX + radius * Math.cos(angleInRadians),
y: centerY + radius * Math.sin(angleInRadians),
};
};
const degreesToRadians = (degrees: number) => degrees * (Math.PI / 180);
const normalizeValue = (value: number, min: number, max: number) =>
(value - min) / (max - min);
const describeArc = (radius: number, startAngle: number, endAngle: number) => {
const start = polarToCartesian(0, 0, radius, endAngle);
const end = polarToCartesian(0, 0, radius, startAngle);
const largeArcFlag = endAngle - startAngle <= Math.PI ? '0' : '1';
const dSvgAttribute = [
'M',
start.x,
start.y,
'A',
radius,
radius,
0,
largeArcFlag,
0,
end.x,
end.y,
].join(' ');
return dSvgAttribute;
};
const GaugeLines = () => {
const theme = useTheme();
const startRadius = 0.875;
const endRadius = 1.14;
const lineWidth = 0.1;
const lineBorder = 0.05;
const angles = [180 + 50, 180 + 90 + 40];
return (
<>
{angles.map(degreesToRadians).map((angle) => {
const start = polarToCartesian(0, 0, startRadius, angle);
const end = polarToCartesian(0, 0, endRadius, angle);
return (
<Fragment key={angle}>
<path
d={`M ${start.x} ${start.y} L ${end.x} ${end.y}`}
fill='none'
stroke={theme.palette.background.paper}
strokeWidth={lineWidth}
strokeLinecap='round'
/>
<path
d={`M ${start.x} ${start.y} L ${end.x} ${end.y}`}
fill='none'
stroke={theme.palette.charts.gauge.sectionLine}
strokeWidth={lineWidth - lineBorder}
strokeLinecap='round'
/>
</Fragment>
);
})}
</>
);
};
const GaugeText = () => {
const fontSize = 0.175;
return (
<>
<text
x={0}
y={0}
transform='translate(-1.15 -0.5) rotate(-65)'
textAnchor='middle'
fontSize={fontSize}
fill='currentColor'
>
Slow
</text>
<text
textAnchor='middle'
transform='translate(0 -1.25)'
fontSize={fontSize}
fill='currentColor'
>
Medium
</text>
<text
transform='translate(1.15 -0.5) rotate(65)'
textAnchor='middle'
fontSize={fontSize}
fill='currentColor'
>
Fast
</text>
</>
);
};
type GaugeProps = {
value?: number;
min?: number;
max?: number;
};
export const Gauge: VFC<GaugeProps> = ({ value, min = 0, max = 100 }) => {
const theme = useTheme();
const radius = 1;
const lineWidth = 0.25;
const startAngle = -Math.PI;
const endAngle = 0;
// Calculate the filled arc proportion based on the value
const valueAngle =
startAngle +
(endAngle - startAngle) * normalizeValue(value || 0, min, max);
const backgroundArcPath = describeArc(radius, startAngle, endAngle);
const filledArcPath = describeArc(radius, startAngle, valueAngle);
return (
<Box
style={{ textAlign: 'center', fontFamily: 'system-ui, sans-serif' }}
sx={{ color: theme.palette.charts.gauge.text }}
>
<svg
width='250'
height='150'
viewBox={[-1.5, -1.5, 3, 1.5].join(' ')}
style={{ overflow: 'visible' }}
>
<title>Gauge chart for time to production</title>
<defs>
<linearGradient
id='Gauge__gradient'
gradientUnits='userSpaceOnUse'
x1='-1'
x2='1'
y1='0'
y2='0'
gradientTransform='rotate(-45)'
>
<stop
offset='0%'
stopColor={theme.palette.charts.gauge.gradientStart}
/>
<stop
offset='100%'
stopColor={theme.palette.charts.gauge.gradientEnd}
/>
</linearGradient>
</defs>
<path
d={backgroundArcPath}
fill='none'
stroke={theme.palette.charts.gauge.background}
strokeWidth={lineWidth - 0.01}
strokeLinecap='round'
/>
<ConditionallyRender
condition={value !== undefined}
show={
<path
d={filledArcPath}
fill='none'
stroke='url(#Gauge__gradient)'
strokeWidth={lineWidth}
strokeLinecap='round'
/>
}
/>
<GaugeLines />
<GaugeText />
</svg>
</Box>
);
};