@@ -7,51 +7,36 @@ import { Tools } from "../../tools";
7
7
import { select } from "d3-selection" ;
8
8
import { interpolateNumber } from "d3-interpolate" ;
9
9
10
- const donutCenterNumberTween = ( d3Ref , newNumber : number ) => {
11
- // Remove commas from the current value string, and convert to an int
12
- const currentValue = parseInt ( d3Ref . text ( ) . replace ( / [ , ] + / g, "" ) , 10 ) || 0 ;
13
- const i = interpolateNumber ( currentValue , newNumber ) ;
14
-
15
- const formatInterpolatedValue = number => Math . floor ( number ) . toLocaleString ( ) ;
16
-
17
- return t => d3Ref . text ( formatInterpolatedValue ( i ( t ) ) ) ;
18
- } ;
19
-
20
10
export class Donut extends Pie {
21
11
type = "donut" ;
22
12
23
13
render ( animate = true ) {
24
14
// Call render() from Pie
25
15
super . render ( animate ) ;
26
16
17
+ const self = this ;
18
+
27
19
const svg = DOMUtils . appendOrSelect ( this . getContainerSVG ( ) , "g.center" ) ;
28
20
const options = this . model . getOptions ( ) ;
29
21
30
22
// Compute the outer radius needed
31
23
const radius = this . computeRadius ( ) ;
32
24
33
- let donutCenterFigure = Tools . getProperty ( options , "center" , "number" ) ;
34
- if ( ! donutCenterFigure ) {
35
- donutCenterFigure = this . getDataList ( ) . reduce ( ( accumulator , d ) => {
36
- return accumulator + d . value ;
37
- } , 0 ) ;
38
- }
39
-
40
25
// Add the number shown in the center of the donut
41
26
DOMUtils . appendOrSelect ( svg , "text.donut-figure" )
42
27
. attr ( "text-anchor" , "middle" )
43
28
. style ( "font-size" , ( ) => options . donut . center . numberFontSize ( radius ) )
44
29
. transition ( this . services . transitions . getTransition ( "donut-figure-enter-update" , animate ) )
45
30
. tween ( "text" , function ( ) {
46
- return donutCenterNumberTween ( select ( this ) , donutCenterFigure ) ;
31
+ return self . centerNumberTween ( select ( this ) ) ;
47
32
} ) ;
48
33
49
34
// Add the label below the number in the center of the donut
50
35
DOMUtils . appendOrSelect ( svg , "text.donut-title" )
51
36
. attr ( "text-anchor" , "middle" )
52
37
. style ( "font-size" , ( ) => options . donut . center . titleFontSize ( radius ) )
53
38
. attr ( "y" , options . donut . center . titleYPosition ( radius ) )
54
- . text ( options . donut . center . label ) ;
39
+ . text ( Tools . getProperty ( options , " donut" , " center" , " label" ) ) ;
55
40
}
56
41
57
42
getInnerRadius ( ) {
@@ -60,4 +45,24 @@ export class Donut extends Pie {
60
45
61
46
return radius * ( 3 / 4 ) ;
62
47
}
48
+
49
+ centerNumberTween ( d3Ref ) {
50
+ const options = this . model . getOptions ( ) ;
51
+
52
+ let donutCenterFigure = Tools . getProperty ( options , "donut" , "center" , "number" ) ;
53
+ if ( ! donutCenterFigure ) {
54
+ donutCenterFigure = this . getDataList ( ) . reduce ( ( accumulator , d ) => {
55
+ return accumulator + d . value ;
56
+ } , 0 ) ;
57
+ }
58
+
59
+ // Remove commas from the current value string, and convert to an int
60
+ const currentValue = parseInt ( d3Ref . text ( ) . replace ( / [ , ] + / g, "" ) , 10 ) || 0 ;
61
+ const i = interpolateNumber ( currentValue , donutCenterFigure ) ;
62
+
63
+ return t => {
64
+ const { numberFormatter } = options . donut . center ;
65
+ d3Ref . text ( numberFormatter ( i ( t ) ) ) ;
66
+ } ;
67
+ }
63
68
}
0 commit comments