7
7
8
8
var React = require ( 'react' ) ;
9
9
var ReactDOM = require ( 'react-dom' ) ;
10
+ var Chart = require ( 'chart.js' ) ;
10
11
11
12
module . exports = {
12
13
createClass : function ( chartType , methodNames , dataKey ) {
@@ -48,51 +49,37 @@ module.exports = {
48
49
classData . componentWillReceiveProps = function ( nextProps ) {
49
50
var chart = this . state . chart ;
50
51
51
- // // Reset the array of datasets
52
- chart . data . datasets . forEach ( function ( set , setIndex ) {
53
- set . data . forEach ( function ( val , pointIndex ) {
54
- set . data = [ ] ;
55
- } ) ;
56
- } ) ;
57
-
58
- // // Reset the array of labels
59
- chart . data . labels = [ ] ;
52
+ if ( nextProps . redraw ) {
53
+ chart . destroy ( ) ; // Reset the array of datasets
54
+ this . initializeChart ( nextProps ) ;
55
+ } else {
56
+ // assign all of the properites from the next datasets to the current chart
57
+ nextProps . data . datasets . forEach ( function ( set , setIndex ) {
58
+ var chartDataset = chart . data . datasets [ setIndex ] ;
60
59
61
- // Adds the datapoints from nextProps
62
- nextProps . data . datasets . forEach ( function ( set , setIndex ) {
63
- set . data . forEach ( function ( val , pointIndex ) {
64
- chart . data . datasets [ setIndex ] . data [ pointIndex ] = nextProps . data . datasets [ setIndex ] . data [ pointIndex ] ;
60
+ for ( var property in set ) {
61
+ if ( set . hasOwnProperty ( property ) ) {
62
+ chartDataset [ property ] = set [ property ] ;
63
+ }
64
+ }
65
65
} ) ;
66
- } ) ;
67
66
68
- // Sets the labels from nextProps
69
- nextProps . data . labels . forEach ( function ( val , labelIndex ) {
70
- chart . data . labels [ labelIndex ] = nextProps . data . labels [ labelIndex ] ;
71
- } ) ;
67
+ chart . data . labels = nextProps . data . labels ;
72
68
73
- // Updates Chart with new data
74
- chart . update ( ) ;
69
+ chart . update ( ) ;
70
+ }
75
71
} ;
76
72
77
73
classData . initializeChart = function ( nextProps ) {
78
- var Chart = require ( 'chart.js' ) ;
79
74
var el = ReactDOM . findDOMNode ( this ) ;
80
75
var ctx = el . getContext ( "2d" ) ;
76
+ var type = ( chartType === 'PolarArea' ) ? 'polarArea' :chartType . toLowerCase ( ) ;
81
77
82
- if ( chartType === 'PolarArea' ) {
83
- var chart = new Chart ( ctx , {
84
- type : 'polarArea' ,
85
- data : nextProps . data ,
86
- options : nextProps . options
87
- } ) ;
88
- } else {
89
- var chart = new Chart ( ctx , {
90
- type : chartType . toLowerCase ( ) ,
91
- data : nextProps . data ,
92
- options : nextProps . options
93
- } ) ;
94
- }
95
- this . state . chart = chart ;
78
+ this . state . chart = new Chart ( ctx , {
79
+ type : type ,
80
+ data : nextProps . data ,
81
+ options : nextProps . options
82
+ } ) ;
96
83
} ;
97
84
98
85
@@ -118,4 +105,4 @@ module.exports = {
118
105
119
106
return React . createClass ( classData ) ;
120
107
}
121
- } ;
108
+ } ;
0 commit comments