@@ -8,6 +8,10 @@ var helpers = require('../helpers/index');
88var resolve = helpers . options . resolve ;
99var valueOrDefault = helpers . valueOrDefault ;
1010
11+ var PI = Math . PI ;
12+ var DOUBLE_PI = PI * 2 ;
13+ var HALF_PI = PI / 2 ;
14+
1115defaults . _set ( 'doughnut' , {
1216 animation : {
1317 // Boolean - Whether we animate the rotation of the Doughnut
@@ -91,10 +95,10 @@ defaults._set('doughnut', {
9195 cutoutPercentage : 50 ,
9296
9397 // The rotation of the chart, where the first data arc begins.
94- rotation : Math . PI * - 0.5 ,
98+ rotation : - HALF_PI ,
9599
96100 // The total circumference of the chart.
97- circumference : Math . PI * 2.0 ,
101+ circumference : DOUBLE_PI ,
98102
99103 // Need to override these to give a nice default
100104 tooltips : {
@@ -145,45 +149,52 @@ module.exports = DatasetController.extend({
145149 var chart = me . chart ;
146150 var chartArea = chart . chartArea ;
147151 var opts = chart . options ;
148- var size = { width : 1 , height : 1 } ;
149- var offset = { x : 0 , y : 0 } ;
152+ var ratioX = 1 ;
153+ var ratioY = 1 ;
154+ var offsetX = 0 ;
155+ var offsetY = 0 ;
150156 var meta = me . getMeta ( ) ;
151157 var arcs = meta . data ;
152- var cutoutPercentage = opts . cutoutPercentage ;
158+ var cutout = opts . cutoutPercentage / 100 || 0 ;
153159 var circumference = opts . circumference ;
154160 var chartWeight = me . _getRingWeight ( me . index ) ;
155161 var maxWidth , maxHeight , i , ilen ;
156162
157163 // If the chart's circumference isn't a full circle, calculate size as a ratio of the width/height of the arc
158- if ( circumference < Math . PI * 2.0 ) {
159- var startAngle = opts . rotation % ( Math . PI * 2.0 ) ;
160- startAngle += Math . PI * 2.0 * ( startAngle >= Math . PI ? - 1 : startAngle < - Math . PI ? 1 : 0 ) ;
164+ if ( circumference < DOUBLE_PI ) {
165+ var startAngle = opts . rotation % DOUBLE_PI ;
166+ startAngle += startAngle >= PI ? - DOUBLE_PI : startAngle < - PI ? DOUBLE_PI : 0 ;
161167 var endAngle = startAngle + circumference ;
162- var start = { x : Math . cos ( startAngle ) , y : Math . sin ( startAngle ) } ;
163- var end = { x : Math . cos ( endAngle ) , y : Math . sin ( endAngle ) } ;
164- var contains0 = ( startAngle <= 0 && endAngle >= 0 ) || ( startAngle <= Math . PI * 2.0 && Math . PI * 2.0 <= endAngle ) ;
165- var contains90 = ( startAngle <= Math . PI * 0.5 && Math . PI * 0.5 <= endAngle ) || ( startAngle <= Math . PI * 2.5 && Math . PI * 2.5 <= endAngle ) ;
166- var contains180 = ( startAngle <= - Math . PI && - Math . PI <= endAngle ) || ( startAngle <= Math . PI && Math . PI <= endAngle ) ;
167- var contains270 = ( startAngle <= - Math . PI * 0.5 && - Math . PI * 0.5 <= endAngle ) || ( startAngle <= Math . PI * 1.5 && Math . PI * 1.5 <= endAngle ) ;
168- var cutout = cutoutPercentage / 100.0 ;
169- var min = { x : contains180 ? - 1 : Math . min ( start . x * ( start . x < 0 ? 1 : cutout ) , end . x * ( end . x < 0 ? 1 : cutout ) ) , y : contains270 ? - 1 : Math . min ( start . y * ( start . y < 0 ? 1 : cutout ) , end . y * ( end . y < 0 ? 1 : cutout ) ) } ;
170- var max = { x : contains0 ? 1 : Math . max ( start . x * ( start . x > 0 ? 1 : cutout ) , end . x * ( end . x > 0 ? 1 : cutout ) ) , y : contains90 ? 1 : Math . max ( start . y * ( start . y > 0 ? 1 : cutout ) , end . y * ( end . y > 0 ? 1 : cutout ) ) } ;
171- size = { width : ( max . x - min . x ) * 0.5 , height : ( max . y - min . y ) * 0.5 } ;
172- offset = { x : ( max . x + min . x ) * - 0.5 , y : ( max . y + min . y ) * - 0.5 } ;
168+ var startX = Math . cos ( startAngle ) ;
169+ var startY = Math . sin ( startAngle ) ;
170+ var endX = Math . cos ( endAngle ) ;
171+ var endY = Math . sin ( endAngle ) ;
172+ var contains0 = ( startAngle <= 0 && endAngle >= 0 ) || endAngle >= DOUBLE_PI ;
173+ var contains90 = ( startAngle <= HALF_PI && endAngle >= HALF_PI ) || endAngle >= DOUBLE_PI + HALF_PI ;
174+ var contains180 = startAngle === - PI || endAngle >= PI ;
175+ var contains270 = ( startAngle <= - HALF_PI && endAngle >= - HALF_PI ) || endAngle >= PI + HALF_PI ;
176+ var minX = contains180 ? - 1 : Math . min ( startX , startX * cutout , endX , endX * cutout ) ;
177+ var minY = contains270 ? - 1 : Math . min ( startY , startY * cutout , endY , endY * cutout ) ;
178+ var maxX = contains0 ? 1 : Math . max ( startX , startX * cutout , endX , endX * cutout ) ;
179+ var maxY = contains90 ? 1 : Math . max ( startY , startY * cutout , endY , endY * cutout ) ;
180+ ratioX = ( maxX - minX ) / 2 ;
181+ ratioY = ( maxY - minY ) / 2 ;
182+ offsetX = - ( maxX + minX ) / 2 ;
183+ offsetY = - ( maxY + minY ) / 2 ;
173184 }
174185
175186 for ( i = 0 , ilen = arcs . length ; i < ilen ; ++ i ) {
176187 arcs [ i ] . _options = me . _resolveElementOptions ( arcs [ i ] , i ) ;
177188 }
178189
179190 chart . borderWidth = me . getMaxBorderWidth ( ) ;
180- maxWidth = ( chartArea . right - chartArea . left - chart . borderWidth ) / size . width ;
181- maxHeight = ( chartArea . bottom - chartArea . top - chart . borderWidth ) / size . height ;
191+ maxWidth = ( chartArea . right - chartArea . left - chart . borderWidth ) / ratioX ;
192+ maxHeight = ( chartArea . bottom - chartArea . top - chart . borderWidth ) / ratioY ;
182193 chart . outerRadius = Math . max ( Math . min ( maxWidth , maxHeight ) / 2 , 0 ) ;
183- chart . innerRadius = Math . max ( cutoutPercentage ? ( chart . outerRadius / 100 ) * ( cutoutPercentage ) : 0 , 0 ) ;
194+ chart . innerRadius = Math . max ( chart . outerRadius * cutout , 0 ) ;
184195 chart . radiusLength = ( chart . outerRadius - chart . innerRadius ) / ( me . _getVisibleDatasetWeightTotal ( ) || 1 ) ;
185- chart . offsetX = offset . x * chart . outerRadius ;
186- chart . offsetY = offset . y * chart . outerRadius ;
196+ chart . offsetX = offsetX * chart . outerRadius ;
197+ chart . offsetY = offsetY * chart . outerRadius ;
187198
188199 meta . total = me . calculateTotal ( ) ;
189200
@@ -206,7 +217,7 @@ module.exports = DatasetController.extend({
206217 var startAngle = opts . rotation ; // non reset case handled later
207218 var endAngle = opts . rotation ; // non reset case handled later
208219 var dataset = me . getDataset ( ) ;
209- var circumference = reset && animationOpts . animateRotate ? 0 : arc . hidden ? 0 : me . calculateCircumference ( dataset . data [ index ] ) * ( opts . circumference / ( 2.0 * Math . PI ) ) ;
220+ var circumference = reset && animationOpts . animateRotate ? 0 : arc . hidden ? 0 : me . calculateCircumference ( dataset . data [ index ] ) * ( opts . circumference / DOUBLE_PI ) ;
210221 var innerRadius = reset && animationOpts . animateScale ? 0 : me . innerRadius ;
211222 var outerRadius = reset && animationOpts . animateScale ? 0 : me . outerRadius ;
212223 var options = arc . _options || { } ;
@@ -272,7 +283,7 @@ module.exports = DatasetController.extend({
272283 calculateCircumference : function ( value ) {
273284 var total = this . getMeta ( ) . total ;
274285 if ( total > 0 && ! isNaN ( value ) ) {
275- return ( Math . PI * 2.0 ) * ( Math . abs ( value ) / total ) ;
286+ return DOUBLE_PI * ( Math . abs ( value ) / total ) ;
276287 }
277288 return 0 ;
278289 } ,
0 commit comments