@@ -105,7 +105,7 @@ export class PieChart extends BaseChart {
105
105
106
106
// Compute the correct inner & outer radius
107
107
const { pie : pieConfigs } = Configuration ;
108
- const marginedRadius = radius - ( pieConfigs . label . margin * ( chartSize . width / pieConfigs . maxWidth ) ) ;
108
+ const marginedRadius = this . computeRadius ( ) ;
109
109
this . arc = arc ( )
110
110
. innerRadius ( this . options . type === "donut" ? ( marginedRadius * ( 2 / 3 ) ) : 0 )
111
111
. outerRadius ( marginedRadius ) ;
@@ -127,16 +127,17 @@ export class PieChart extends BaseChart {
127
127
. each ( function ( d ) { this . _current = d ; } ) ;
128
128
129
129
// Draw the slice labels
130
+ const self = this ;
130
131
this . innerWrap
131
132
. selectAll ( "text.chart-label" )
132
133
. data ( this . pie ( dataList ) , ( d : any ) => d . data . label )
133
134
. enter ( )
134
135
. append ( "text" )
135
136
. classed ( "chart-label" , true )
136
137
. attr ( "dy" , Configuration . pie . label . dy )
137
- . style ( "text-anchor" , this . deriveTextAnchor )
138
- . attr ( "transform" , d => this . deriveTransformString ( d , radius ) )
139
- . text ( d => Tools . convertValueToPercentage ( d . data . value , dataList ) ) ;
138
+ . style ( "text-anchor" , "middle" )
139
+ . text ( d => Tools . convertValueToPercentage ( d . data . value , dataList ) )
140
+ . attr ( "transform" , function ( d ) { return self . deriveTransformString ( this , d , radius ) ; } ) ;
140
141
141
142
// Hide overlay
142
143
this . updateOverlay ( ) . hide ( ) ;
@@ -212,19 +213,18 @@ export class PieChart extends BaseChart {
212
213
. append ( "text" )
213
214
. classed ( "chart-label" , true )
214
215
. attr ( "dy" , Configuration . pie . label . dy )
215
- . style ( "text-anchor" , this . deriveTextAnchor )
216
- . attr ( "transform" , d => this . deriveTransformString ( d , radius ) )
216
+ . style ( "text-anchor" , "middle" )
217
217
. text ( d => Tools . convertValueToPercentage ( d . data . value , dataList ) )
218
+ . attr ( "transform" , function ( d ) { return self . deriveTransformString ( this , d , radius ) ; } )
218
219
. style ( "opacity" , 0 )
219
220
. transition ( )
220
221
. duration ( Configuration . transitions . default . duration / 2 )
221
222
. style ( "opacity" , 1 ) ;
222
223
223
224
text
224
- . attr ( "dy" , Configuration . pie . label . dy )
225
- . style ( "text-anchor" , this . deriveTextAnchor )
226
- . attr ( "transform" , d => this . deriveTransformString ( d , radius ) )
225
+ . style ( "text-anchor" , "middle" )
227
226
. text ( d => Tools . convertValueToPercentage ( d . data . value , dataList ) )
227
+ . attr ( "transform" , function ( d ) { return self . deriveTransformString ( this , d , radius ) ; } )
228
228
. transition ( )
229
229
. duration ( Configuration . transitions . default . duration / 2 )
230
230
. style ( "opacity" , 1 ) ;
@@ -342,8 +342,7 @@ export class PieChart extends BaseChart {
342
342
343
343
const chartSize : any = this . getChartSize ( this . container ) ;
344
344
const dimensionToUseForScale = Math . min ( chartSize . width , chartSize . height ) ;
345
- const scaleRatio = dimensionToUseForScale / pieConfigs . maxWidth ;
346
- const radius : number = dimensionToUseForScale / 2 ;
345
+ const radius : number = this . computeRadius ( ) ;
347
346
348
347
// Resize the SVG
349
348
select ( this . holder ) . select ( "svg" )
@@ -353,17 +352,17 @@ export class PieChart extends BaseChart {
353
352
. style ( "transform" , `translate(${ radius } px,${ radius } px)` ) ;
354
353
355
354
// Resize the arc
356
- const marginedRadius = radius - ( pieConfigs . label . margin * scaleRatio ) ;
357
355
this . arc = arc ( )
358
- . innerRadius ( this . options . type === "donut" ? ( marginedRadius * ( 2 / 3 ) ) : 0 )
359
- . outerRadius ( marginedRadius ) ;
356
+ . innerRadius ( this . options . type === "donut" ? ( radius * ( 2 / 3 ) ) : 0 )
357
+ . outerRadius ( radius ) ;
360
358
361
359
this . innerWrap . selectAll ( "path" )
362
360
. attr ( "d" , this . arc ) ;
363
361
362
+ const self = this ;
364
363
this . innerWrap
365
364
. selectAll ( "text.chart-label" )
366
- . attr ( "transform" , d => this . deriveTransformString ( d , radius ) ) ;
365
+ . attr ( "transform" , function ( d ) { return self . deriveTransformString ( this , d , radius ) ; } ) ;
367
366
368
367
// Reposition the legend
369
368
this . positionLegend ( ) ;
@@ -386,35 +385,18 @@ export class PieChart extends BaseChart {
386
385
* @returns final transform string to be applied to the <text> element
387
386
* @memberof PieChart
388
387
*/
389
- private deriveTransformString ( d , radius ) {
390
- const theta = d . endAngle - d . startAngle ;
391
- const xPosition = radius * Math . sin ( ( theta / 2 ) + d . startAngle ) ;
392
- const yPosition = - 1 * radius * Math . cos ( ( theta / 2 ) + d . startAngle ) ;
388
+ private deriveTransformString ( element , d , radius ) {
389
+ const textLength = element . getComputedTextLength ( ) ;
390
+ const textOffsetX = textLength / 2 ;
391
+ const textOffsetY = parseFloat ( getComputedStyle ( element ) . fontSize ) / 2 ;
393
392
394
- return `translate(${ xPosition } , ${ yPosition } )` ;
395
- }
393
+ const marginedRadius = radius + Configuration . pie . label . margin ;
396
394
397
- /**
398
- * Decide what text-anchor value the slice label item would need based on the quadrant it's in
399
- *
400
- * @private
401
- * @param {any } d - d3 data item for slice
402
- * @returns computed decision on what the text-anchor string should be
403
- * @memberof PieChart
404
- */
405
- private deriveTextAnchor ( d ) {
406
- const QUADRANT = Math . PI / 4 ;
407
- const rads = ( d . endAngle - d . startAngle ) / 2 + d . startAngle ;
408
-
409
- if ( rads >= QUADRANT && rads <= 3 * QUADRANT ) {
410
- return "start" ;
411
- } else if ( ( rads > 7 * QUADRANT && rads < QUADRANT ) || ( rads > 3 * QUADRANT && rads < 5 * QUADRANT ) ) {
412
- return "middle" ;
413
- } else if ( rads >= 5 * QUADRANT && rads <= 7 * QUADRANT ) {
414
- return "end" ;
415
- } else {
416
- return "middle" ;
417
- }
395
+ const theta = ( ( d . endAngle - d . startAngle ) / 2 ) + d . startAngle ;
396
+ const xPosition = ( textOffsetX + marginedRadius ) * Math . sin ( theta ) ;
397
+ const yPosition = ( textOffsetY + marginedRadius ) * - Math . cos ( theta ) ;
398
+
399
+ return `translate(${ xPosition } , ${ yPosition } )` ;
418
400
}
419
401
}
420
402
0 commit comments