@@ -218,7 +218,11 @@ function AwesomeChart(canvasElementId){
218
218
this . drawExplodedPieChart ( ) ;
219
219
}
220
220
} else if ( this . chartType == "horizontal bars" ) {
221
- this . drawVerticalBarChart ( ) ;
221
+ if ( this . animate ) {
222
+ this . animateVerticalBarChart ( ) ;
223
+ } else {
224
+ this . drawVerticalBarChart ( ) ;
225
+ }
222
226
} else if ( this . chartType == "pareto" ) {
223
227
this . drawParetoChart ( ) ;
224
228
} else {
@@ -232,8 +236,9 @@ function AwesomeChart(canvasElementId){
232
236
this . drawTitleAndBorders ( ) ;
233
237
234
238
}
235
-
236
-
239
+
240
+
241
+
237
242
this . drawTitleAndBorders = function ( ) {
238
243
var context = this . ctx ;
239
244
@@ -262,8 +267,9 @@ function AwesomeChart(canvasElementId){
262
267
263
268
context . globalCompositeOperation = 'source-over' ;
264
269
}
265
-
266
-
270
+
271
+
272
+
267
273
this . drawBarChart = function ( ) {
268
274
var context = this . ctx ;
269
275
@@ -372,8 +378,9 @@ function AwesomeChart(canvasElementId){
372
378
x = x + barWidth + this . barHGap ;
373
379
}
374
380
}
375
-
376
-
381
+
382
+
383
+
377
384
this . animateBarChart = function ( ) {
378
385
var aw = this ,
379
386
numFrames = this . animationFrames ,
@@ -456,8 +463,9 @@ function AwesomeChart(canvasElementId){
456
463
updateBarChart ( ) ;
457
464
458
465
}
459
-
460
-
466
+
467
+
468
+
461
469
this . drawVerticalBarChart = function ( ) {
462
470
var context = this . ctx ;
463
471
@@ -472,12 +480,12 @@ function AwesomeChart(canvasElementId){
472
480
var maxData = this . data . max ( ) ;
473
481
var minData = this . data . min ( ) ;
474
482
483
+ var marginLeft = this . marginLeft ;
475
484
if ( this . title != null ) {
476
- this . marginLeft += this . titleFontHeight + this . titleMargin ;
485
+ marginLeft += this . titleFontHeight + this . titleMargin ;
477
486
}
478
487
479
- var barWidth = ( this . width - this . marginLeft
480
- - this . marginRight - ( n - 1 ) * this . barHGap ) / n ;
488
+ var barWidth = ( this . width - marginLeft - this . marginRight - ( n - 1 ) * this . barHGap ) / n ;
481
489
482
490
context . font = this . labelFontStyle + ' ' + this . labelFontHeight + 'px ' + this . labelFont ;
483
491
var maxLabelWidth = 0 ;
@@ -517,11 +525,12 @@ function AwesomeChart(canvasElementId){
517
525
var maxBarHeight = Math . max ( Math . abs ( barBottomY - barMaxTopY ) , Math . abs ( barBottomY - barMinTopY ) ) ;
518
526
var maxBarAbsData = Math . max ( Math . abs ( minData ) , Math . abs ( maxData ) ) ;
519
527
520
- var x = this . marginLeft ;
528
+ var x = marginLeft ;
521
529
var y = barBottomY ;
522
530
var barHeight = 0 ;
523
531
524
532
var di = 0 ;
533
+
525
534
for ( var i = 0 ; i < this . data . length ; i ++ ) {
526
535
di = this . data [ i ] ;
527
536
@@ -564,7 +573,7 @@ function AwesomeChart(canvasElementId){
564
573
565
574
566
575
context . save ( ) ;
567
- context . translate ( x + barWidth / 2 , barBottomY - barHeight ) ;
576
+ context . translate ( x + barWidth / 2 , barBottomY - barHeight ) ;
568
577
context . rotate ( - Math . PI / 2 ) ;
569
578
context . textBaseline = 'top' ;
570
579
if ( this . labels [ i ] ) {
@@ -589,17 +598,138 @@ function AwesomeChart(canvasElementId){
589
598
context . textAlign = 'right' ;
590
599
context . fillText ( di , - this . labelMargin , 0 ) ;
591
600
}
592
-
593
- context . restore ( ) ;
594
601
602
+ context . restore ( ) ;
595
603
596
604
//Update x:
597
605
598
606
x = x + barWidth + this . barHGap ;
599
607
}
608
+
600
609
context . restore ( ) ;
610
+
601
611
}
602
612
613
+
614
+
615
+ this . animateVerticalBarChart = function ( ) {
616
+ var aw = this ,
617
+ numFrames = this . animationFrames ,
618
+ currentFrame = 0 ,
619
+
620
+ maxData = this . data . max ( ) ,
621
+ minData = this . data . min ( ) ,
622
+ dataLen = this . data . length ,
623
+
624
+ context = this . ctx ,
625
+
626
+ marginLeft = this . marginLeft
627
+ marginTop = this . marginTop
628
+ marginTopCurrent = 0 ;
629
+
630
+
631
+ if ( this . title != null ) {
632
+ marginLeft += this . titleFontHeight + this . titleMargin ;
633
+ }
634
+
635
+ var barWidth = ( this . width - marginLeft - this . marginRight - ( dataLen - 1 ) * this . barHGap ) / dataLen ;
636
+
637
+ context . font = this . labelFontStyle + ' ' + this . labelFontHeight + 'px ' + this . labelFont ;
638
+ var maxLabelWidth = 0 ;
639
+ var labelWidth = 0 ;
640
+ for ( var i = 0 ; i < this . labels . length ; i ++ ) {
641
+ labelWidth = context . measureText ( this . labels [ i ] ) . width ;
642
+ if ( labelWidth > maxLabelWidth ) {
643
+ maxLabelWidth = labelWidth ;
644
+ }
645
+ }
646
+
647
+ context . font = this . dataValueFontStyle + ' ' + this . dataValueFontHeight + 'px ' + this . dataValueFont ;
648
+ var maxDataValueWidth = 0 ;
649
+ var dataValueWidth = 0 ;
650
+ for ( var i = 0 ; i < dataLen ; i ++ ) {
651
+ dataValueWidth = context . measureText ( this . data [ i ] ) . width ;
652
+ if ( dataValueWidth > maxDataValueWidth ) {
653
+ maxDataValueWidth = dataValueWidth ;
654
+ }
655
+ }
656
+
657
+ var barMaxTopY = this . marginTop + Math . max ( ( this . labelMargin + maxLabelWidth ) , ( this . dataValueMargin + maxDataValueWidth ) ) ;
658
+
659
+ var barMinTopY = this . height - this . marginBottom ;
660
+
661
+ var barBottomY = this . height - this . marginBottom ;
662
+
663
+ if ( minData < 0 ) {
664
+
665
+ barMinTopY = this . height - this . marginBottom - this . labelMargin - this . labelFontHeight - this . dataValueMargin - this . dataValueFontHeight ;
666
+
667
+ barBottomY = barMinTopY + ( ( this . height - this . marginBottom - barMaxTopY - this . labelMargin - this . labelFontHeight - this . dataValueMargin - this . dataValueFontHeight ) * minData ) / ( Math . abs ( minData ) + maxData ) ;
668
+
669
+ }
670
+
671
+
672
+ var maxBarHeight = Math . max ( Math . abs ( barBottomY - barMaxTopY ) , Math . abs ( barBottomY - barMinTopY ) ) ;
673
+ var maxBarAbsData = Math . max ( Math . abs ( minData ) , Math . abs ( maxData ) ) ;
674
+
675
+
676
+ var belowZeroMaxBarHeight = 0 ;
677
+ if ( minData < 0 ) {
678
+ belowZeroMaxBarHeight = Math . abs ( minData * maxBarHeight / maxBarAbsData ) ;
679
+ }
680
+
681
+ var chartAreaHeight = maxData * maxBarHeight / maxBarAbsData + belowZeroMaxBarHeight ,
682
+ changeOfMarginBottom = 0 ,
683
+ changeOfMarginTop = 0 ;
684
+
685
+ this . marginBottom += belowZeroMaxBarHeight ;
686
+ this . marginTop += chartAreaHeight - belowZeroMaxBarHeight ;
687
+ changeOfMarginBottom = belowZeroMaxBarHeight / numFrames ;
688
+ changeOfMarginTop = ( chartAreaHeight - belowZeroMaxBarHeight ) / numFrames ;
689
+
690
+ var updateVerticalBarChart = function ( ) {
691
+ if ( currentFrame ++ < numFrames ) {
692
+
693
+ aw . marginBottom -= changeOfMarginBottom ;
694
+ aw . marginTop -= changeOfMarginTop ;
695
+
696
+ aw . ctx . clearRect ( 0 , 0 , aw . width , aw . height ) ;
697
+ aw . drawVerticalBarChart ( ) ;
698
+
699
+ marginTopCurrent = aw . marginTop ;
700
+ aw . marginTop = marginTop ;
701
+ aw . drawTitleAndBorders ( ) ;
702
+ aw . marginTop = marginTopCurrent ;
703
+
704
+ // Standard
705
+ if ( typeof ( window . requestAnimationFrame ) == 'function' ) {
706
+ window . requestAnimationFrame ( updateVerticalBarChart ) ;
707
+
708
+ // IE 10+
709
+ } else if ( typeof ( window . msRequestAnimationFrame ) == 'function' ) {
710
+ window . msRequestAnimationFrame ( updateVerticalBarChart ) ;
711
+
712
+ // Chrome
713
+ } else if ( typeof ( window . webkitRequestAnimationFrame ) == 'function' ) {
714
+ window . webkitRequestAnimationFrame ( updateVerticalBarChart ) ;
715
+
716
+ // Firefox
717
+ } else if ( window . mozRequestAnimationFrame ) { // Seems rather slow in FF6 - so disabled
718
+ window . mozRequestAnimationFrame ( updateVerticalBarChart ) ;
719
+
720
+ // Default fallback to setTimeout
721
+ } else {
722
+ setTimeout ( updateVerticalBarChart , 16.6666666 ) ;
723
+ }
724
+ }
725
+ }
726
+
727
+ updateVerticalBarChart ( ) ;
728
+
729
+ }
730
+
731
+
732
+
603
733
this . drawPieChart = function ( ring ) {
604
734
var context = this . ctx ;
605
735
context . lineWidth = this . pieBorderWidth ;
@@ -918,8 +1048,8 @@ function AwesomeChart(canvasElementId){
918
1048
}
919
1049
920
1050
}
921
-
922
-
1051
+
1052
+
923
1053
924
1054
this . animatePieChart = function ( pieType ) {
925
1055
var dataSum = 0 ,
0 commit comments