@@ -4,6 +4,16 @@ import PropTypes from 'prop-types';
44import enhancer from './enhancer' ;
55import { propTypes , defaultProps } from './types' ;
66
7+ let gradientSeed = 0 ;
8+
9+ function stripPercentToNumber ( percent ) {
10+ return + percent . replace ( '%' , '' ) ;
11+ }
12+
13+ function toArray ( symArray ) {
14+ return Array . isArray ( symArray ) ? symArray : [ symArray ] ;
15+ }
16+
717function getPathStyles ( offset , percent , strokeColor , strokeWidth , gapDegree = 0 , gapPosition ) {
818 const radius = 50 - strokeWidth / 2 ;
919 let beginPositionX = 0 ;
@@ -51,6 +61,14 @@ function getPathStyles(offset, percent, strokeColor, strokeWidth, gapDegree = 0,
5161class Circle extends Component {
5262 paths = { } ;
5363
64+ gradientId = 0 ;
65+
66+ constructor ( ) {
67+ super ( ) ;
68+ this . gradientId = gradientSeed ;
69+ gradientSeed += 1 ;
70+ }
71+
5472 getStokeList ( ) {
5573 const {
5674 prefixCls,
@@ -61,15 +79,16 @@ class Circle extends Component {
6179 gapDegree,
6280 gapPosition,
6381 } = this . props ;
64- const percentList = Array . isArray ( percent ) ? percent : [ percent ] ;
65- const strokeColorList = Array . isArray ( strokeColor ) ? strokeColor : [ strokeColor ] ;
66-
67- const stroke =
68- Object . prototype . toString . call ( strokeColor ) === '[object Object]' ? 'url(#gradient)' : '' ;
82+ const percentList = toArray ( percent ) ;
83+ const strokeColorList = toArray ( strokeColor ) ;
6984
7085 let stackPtg = 0 ;
7186 return percentList . map ( ( ptg , index ) => {
7287 const color = strokeColorList [ index ] || strokeColorList [ strokeColorList . length - 1 ] ;
88+ const stroke =
89+ Object . prototype . toString . call ( color ) === '[object Object]'
90+ ? `url(#${ prefixCls } -gradient-${ this . gradientId } )`
91+ : '' ;
7392 const { pathString, pathStyle } = getPathStyles (
7493 stackPtg ,
7594 ptg ,
@@ -122,20 +141,32 @@ class Circle extends Component {
122141 gapPosition ,
123142 ) ;
124143 delete restProps . percent ;
125- const isGradient = Object . prototype . toString . call ( strokeColor ) === '[object Object]' ;
144+ const strokeColorList = toArray ( strokeColor ) ;
145+ const gradient = strokeColorList . find (
146+ color => Object . prototype . toString . call ( color ) === '[object Object]' ,
147+ ) ;
148+
126149 return (
127150 < svg
128151 className = { `${ prefixCls } -circle ${ className } ` }
129152 viewBox = "0 0 100 100"
130153 style = { style }
131154 { ...restProps }
132155 >
133- { isGradient && (
156+ { gradient && (
134157 < defs >
135- < linearGradient id = "gradient" x1 = "100%" y1 = "0%" x2 = "0%" y2 = "0%" >
136- { Object . keys ( strokeColor ) . map ( ( key , index ) => (
137- < stop key = { index } offset = { key } stopColor = { strokeColor [ key ] } />
138- ) ) }
158+ < linearGradient
159+ id = { `${ prefixCls } -gradient-${ this . gradientId } ` }
160+ x1 = "100%"
161+ y1 = "0%"
162+ x2 = "0%"
163+ y2 = "0%"
164+ >
165+ { Object . keys ( gradient )
166+ . sort ( ( a , b ) => stripPercentToNumber ( a ) - stripPercentToNumber ( b ) )
167+ . map ( ( key , index ) => (
168+ < stop key = { index } offset = { key } stopColor = { gradient [ key ] } />
169+ ) ) }
139170 </ linearGradient >
140171 </ defs >
141172 ) }
0 commit comments