-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCircularProgressBarSvg.js
91 lines (75 loc) · 2.55 KB
/
CircularProgressBarSvg.js
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
import React, {PropTypes, Component} from 'react';
import {Animated, View} from 'react-native';
import Svg, {Defs, Stop, G, Path, LinearGradient} from 'react-native-svg';
import {arc} from 'd3-shape';
export default class CircularProgressBarSvg extends Component {
constructor(props) {
super(props);
}
static propTypes = {
size: React.PropTypes.number.isRequired,
outerRadius: React.PropTypes.number.isRequired,
innerRadius: React.PropTypes.number.isRequired,
progress: React.PropTypes.number.isRequired,
foregroundColor: React.PropTypes.string,
backgroundColor: React.PropTypes.string,
renderCenterDrawable: React.PropTypes.func,
};
static defaultProps = {
foregroundColor: 'gray',
}
renderBackgroundPath() {
const backgroundPath = arc()
.innerRadius(this.props.innerRadius)
.outerRadius(this.props.outerRadius)
.startAngle(0)
.endAngle(2 * Math.PI);
return (
<Path
x={this.props.size / 2}
y={this.props.size / 2}
d={backgroundPath()}
fill={this.props.backgroundColor}
/>
);
}
renderCirclePaths() {
let progress = Math.min(100, Math.max(0, this.props.progress));
let startAngle = 0;
let stopAngle = -(2 * Math.PI) / 100 * progress;
let circlePath = arc()
.innerRadius(this.props.innerRadius)
.outerRadius(this.props.outerRadius)
.startAngle(startAngle)
.endAngle(stopAngle);
return (
<Path
x={this.props.size / 2}
y={this.props.size / 2}
d={circlePath()}
fill={this.props.foregroundColor}
/>
);
}
render() {
const progress = Math.min(100, Math.max(0, this.props.progress));
let sizeStyle = {
width: this.props.size,
height: this.props.size,
};
return (
<View style={[this.props.style, sizeStyle]}>
<Svg
width={this.props.size}
height={this.props.size}
>
<G>
{this.props.backgroundColor && this.renderBackgroundPath()}
{this.renderCirclePaths()}
</G>
</Svg>
{this.props.renderCenterDrawable && this.props.renderCenterDrawable(progress)}
</View>
);
}
}