@@ -10,7 +10,17 @@ class SimpleGauge extends React.Component{
10
10
}
11
11
componentDidMount ( ) {
12
12
13
- const percent = this . props . percent ?this . props . percent :100 ;
13
+ const percent = this . props . percent ?this . props . percent >= 0 ?this . props . percent <= 100 ?this . props . percent :100 :0 :100 ;
14
+ let color = this . props . color || '#5BB030' ;
15
+ if ( this . props . intervals && this . props . colors ) {
16
+ let startValue = 0 ;
17
+ this . props . intervals . forEach ( ( element , idx ) => {
18
+ if ( percent >= startValue && percent < element ) {
19
+ color = this . props . colors [ idx ] || color ;
20
+ }
21
+ startValue = element ;
22
+ } ) ;
23
+ }
14
24
15
25
let container = this . container . current ;
16
26
const width = container . clientWidth ;
@@ -51,7 +61,7 @@ class SimpleGauge extends React.Component{
51
61
circle . style . strokeDashoffset = offset ;
52
62
circle . style . strokeWidth = strokeWidth . toString ( ) ;
53
63
circle . style . transform = `translate(${ strokeWidth / 2 } px,${ strokeWidth / 2 } px)` ;
54
-
64
+
55
65
circle = this . point . current ;
56
66
circle . style . width = '100%' ;
57
67
circle . style . height = '100%' ;
@@ -66,7 +76,9 @@ class SimpleGauge extends React.Component{
66
76
67
77
68
78
let progress = this . progress . current ;
69
- progress . style . stroke = '#5BB030' ;
79
+ progress . style . stroke = color ;
80
+
81
+
70
82
progress . style . strokeDasharray = `${ bar } ${ offsetBar } ` ;
71
83
72
84
let point = this . point . current ;
@@ -78,7 +90,7 @@ class SimpleGauge extends React.Component{
78
90
text . style . fontSize = `${ height / 3 } px` ;
79
91
text . style . fontWeight = 'bold' ;
80
92
text . style . marginTop = `-${ height / 2 } px` ;
81
- text . style . color = '#5BB030' ;
93
+ text . style . color = color ;
82
94
text . style . display = 'grid' ;
83
95
text . style . justifyContent = 'center' ;
84
96
@@ -91,7 +103,7 @@ class SimpleGauge extends React.Component{
91
103
< circle ref = { this . progress } > </ circle >
92
104
< circle ref = { this . point } > </ circle >
93
105
</ svg >
94
- < div className = "number" ref = { this . text } > { this . props . percent ?this . props . percent :100 } %</ div >
106
+ < div className = "number" ref = { this . text } > { this . props . percent ?this . props . percent >= 0 ? this . props . percent <= 100 ? this . props . percent : 100 : 0 :100 } %</ div >
95
107
</ div >
96
108
) ;
97
109
}
0 commit comments