-
-
Notifications
You must be signed in to change notification settings - Fork 658
/
ProjectHealthChart.tsx
128 lines (117 loc) · 4.21 KB
/
ProjectHealthChart.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
import type React from 'react';
import { useTheme } from '@mui/material';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
interface ProgressComponentProps {
active: number;
stale: number;
potentiallyStale: number;
health: number;
}
export const ProjectHealthChart: React.FC<ProgressComponentProps> = ({
active,
stale,
potentiallyStale,
health,
}) => {
const theme = useTheme();
const gap = active === 0 || stale === 0 ? 0 : 10;
const strokeWidth = 6;
const radius = 50 - strokeWidth / 2;
const circumference = 2 * Math.PI * radius;
const gapAngle = (gap / circumference) * 360;
const totalCount = active + stale;
const activePercentage =
totalCount === 0 ? 100 : (active / totalCount) * 100;
const stalePercentage = totalCount === 0 ? 0 : (stale / totalCount) * 100;
const potentiallyStalePercentage =
active === 0 ? 0 : (potentiallyStale / active) * 100;
const activeLength = (activePercentage / 100) * circumference - gap;
const staleLength = (stalePercentage / 100) * circumference - gap;
const potentiallyStaleLength =
(potentiallyStalePercentage / 100) * activeLength;
const activeRotation = -90 + gapAngle / 2;
const potentiallyStaleRotation =
activeRotation +
((activeLength - potentiallyStaleLength) / circumference) * 360;
const staleRotation =
activeRotation + (activeLength / circumference) * 360 + gapAngle;
const innerRadius = radius / 1.2;
return (
<svg width='170' height='170' viewBox='0 0 100 100'>
<title>Project Health Chart</title>
<circle
data-testid='active-circle'
cx='50'
cy='50'
r={radius}
fill='none'
stroke={theme.palette.success.border}
strokeWidth={strokeWidth}
strokeLinecap='round'
strokeDasharray={`${activeLength} ${circumference}`}
transform={`rotate(${activeRotation} 50 50)`}
/>
<ConditionallyRender
condition={potentiallyStale > 0}
show={
<circle
data-testid='potentially-stale-circle'
cx='50'
cy='50'
r={radius}
fill='none'
stroke={theme.palette.warning.border}
strokeWidth={strokeWidth}
strokeLinecap='round'
strokeDasharray={`${potentiallyStaleLength} ${circumference}`}
transform={`rotate(${potentiallyStaleRotation} 50 50)`}
/>
}
/>
<ConditionallyRender
condition={stale > 0}
show={
<circle
data-testid='stale-circle'
cx='50'
cy='50'
r={radius}
fill='none'
stroke={theme.palette.error.border}
strokeWidth={strokeWidth}
strokeLinecap='round'
strokeDasharray={`${staleLength} ${circumference}`}
transform={`rotate(${staleRotation} 50 50)`}
/>
}
/>
<circle
cx='50'
cy='50'
r={innerRadius}
fill={theme.palette.warning.light}
/>
<text
x='50%'
y='50%'
fill='black'
fontSize={theme.spacing(2.25)}
textAnchor='middle'
fontWeight='bold'
>
{health}%
</text>
<text
x='50%'
y='50%'
dy='1.5em'
fill={theme.palette.text.secondary}
fontSize={theme.spacing(1.25)}
textAnchor='middle'
fontWeight='normal'
>
{active + stale} flags
</text>
</svg>
);
};