@@ -16,7 +16,7 @@ function donutChart() {
1616
1717 // ===========================================================================================
1818 // Set up constructors for making donut. See https://github.com/d3/d3-shape/blob/master/README.md
19- var radius = 140 ;
19+ var radius = 120 ;
2020
2121 // creates a new pie generator
2222 var pie = d3 . pie ( )
@@ -37,14 +37,19 @@ function donutChart() {
3737 . innerRadius ( radius * 0.9 ) ;
3838 // ===========================================================================================
3939
40+ var tempAngle ;
41+ var tempOffset = {
42+ x : 1 ,
43+ y : 0.95
44+ } ;
4045 // ===========================================================================================
4146 // append the svg object to the selection
4247 var svg = selection . append ( 'svg' )
4348 . attr ( 'width' , width + margin . left + margin . right )
4449 . attr ( 'height' , height + margin . top + margin . bottom )
4550 . attr ( 'class' , 'backColorChart' )
4651 . append ( 'g' )
47- . attr ( 'transform' , 'translate(' + ( width / 2 + 30 ) + ',' + ( height / 2 + 30 ) + ')' ) ;
52+ . attr ( 'transform' , 'translate(' + ( width / 2 + 20 ) + ',' + ( height / 2 + 10 ) + ')' ) ;
4853 // ===========================================================================================
4954
5055 // ===========================================================================================
@@ -80,15 +85,24 @@ function donutChart() {
8085 // effectively computes the centre of the slice.
8186 // see https://github.com/d3/d3-shape/blob/master/README.md#arc_centroid
8287 var pos = outerArc . centroid ( d ) ;
83-
88+
8489 // changes the point to be on left or right depending on where label is.
8590 pos [ 0 ] = radius * 0.95 * ( midAngle ( d ) < Math . PI ? 1 : - 1 ) ;
8691
92+ var currentAngle = midAngle ( d ) ;
93+ if ( angleIsInRangeDifference ( tempAngle , currentAngle , 2 ) ) {
94+ tempOffset . x -= 0.1 ;
95+ tempOffset . y += 0.1 ;
96+ tempAngle = 0 ;
97+ }
98+
8799 if ( d . data . percentage < 0.10 || d . data . url == 'Others' ) {
88- pos [ 0 ] = pos [ 0 ] * 1.1 ;
89- pos [ 1 ] = pos [ 1 ] * 1.15 ;
100+ pos [ 0 ] = pos [ 0 ] * tempOffset . x ;
101+ pos [ 1 ] = pos [ 1 ] * tempOffset . y ;
90102 }
91103
104+ tempAngle = midAngle ( d ) ;
105+
92106 return 'translate(' + pos + ')' ;
93107 } )
94108 . style ( 'text-anchor' , function ( d ) {
@@ -97,6 +111,10 @@ function donutChart() {
97111 } ) ;
98112 // ===========================================================================================
99113
114+ tempOffset = {
115+ x : 1 ,
116+ y : 0.95
117+ } ;
100118 // ===========================================================================================
101119 // add lines connecting labels to slice. A polyline creates straight lines connecting several points
102120 var polyline = svg . select ( '.lines' )
@@ -108,10 +126,19 @@ function donutChart() {
108126 var pos = outerArc . centroid ( d ) ;
109127 pos [ 0 ] = radius * 0.95 * ( midAngle ( d ) < Math . PI ? 1 : - 1 ) ;
110128
129+ var currentAngle = midAngle ( d ) ;
130+ if ( angleIsInRangeDifference ( tempAngle , currentAngle , 2 ) ) {
131+ tempOffset . x -= 0.1 ;
132+ tempOffset . y += 0.1 ;
133+ tempAngle = 0 ;
134+ }
135+
111136 if ( d . data . percentage < 0.10 || d . data . url == 'Others' ) {
112- pos [ 0 ] = pos [ 0 ] * 1.1 ;
113- pos [ 1 ] = pos [ 1 ] * 1.15 ;
137+ pos [ 0 ] = pos [ 0 ] * tempOffset . x ;
138+ pos [ 1 ] = pos [ 1 ] * tempOffset . y ;
114139 }
140+
141+ tempAngle = midAngle ( d ) ;
115142 return [ arc . centroid ( d ) , outerArc . centroid ( d ) , pos ]
116143 } ) ;
117144 // ===========================================================================================
@@ -180,6 +207,10 @@ function donutChart() {
180207
181208 return tip ;
182209 }
210+
211+ function angleIsInRangeDifference ( tempAngle , currentAngle , difference ) {
212+ return currentAngle < ( tempAngle + difference ) && currentAngle > ( tempAngle - difference ) ;
213+ }
183214 // ===========================================================================================
184215
185216 } ) ;
0 commit comments