-
Notifications
You must be signed in to change notification settings - Fork 295
/
DonutRing.tsx
66 lines (61 loc) · 1.76 KB
/
DonutRing.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
// @flow
import React, { Component } from 'react';
import { observer } from 'mobx-react';
import styles from './DonutRing.scss';
type Props = {
percentage: number,
sqSize: number,
strokeWidth: number,
showText?: boolean,
};
@observer
export default class DonutRing extends Component<Props> {
static defaultProps = {
showText: false,
};
render() {
const { percentage, sqSize, strokeWidth, showText } = this.props;
const invertedPercentage = 100 - percentage;
const radius = (sqSize - strokeWidth) / 2;
const viewBox = `0 0 ${sqSize} ${sqSize}`;
const dashArray = radius * Math.PI * 2;
const dashOffset = dashArray - (dashArray * invertedPercentage) / 100;
const rotateDeg = -((invertedPercentage / 100) * 360 + 90);
return (
<div className={styles.component}>
<svg width={sqSize} height={sqSize} viewBox={viewBox}>
<circle
className={styles.circleProgress}
cx={sqSize / 2}
cy={sqSize / 2}
r={radius}
strokeWidth={`${strokeWidth}px`}
/>
<circle
className={styles.circleBackground}
cx={sqSize / 2}
cy={sqSize / 2}
r={radius}
strokeWidth={`${strokeWidth}px`}
transform={`rotate(${rotateDeg} ${sqSize / 2} ${sqSize / 2})`}
style={{
strokeDasharray: dashArray,
strokeDashoffset: dashOffset,
}}
/>
{showText && (
<text
className={styles.circleText}
x="50%"
y="50%"
dy=".3em"
textAnchor="middle"
>
{`${invertedPercentage}%`}
</text>
)}
</svg>
</div>
);
}
}