@@ -2,91 +2,100 @@ var React = require('react');
22var ReactDOM = require ( 'react-dom' ) ;
33
44module . exports = {
5- createClass : function ( chartType , methodNames , dataKey ) {
6- var excludedProps = [ 'data' , 'options' , 'redraw' ] ;
7- var classData = {
8- displayName : chartType + 'Chart' ,
9- getInitialState : function ( ) { return { } ; } ,
10- render : function ( ) {
11- var _props = {
12- ref : 'canvass'
13- } ;
14- for ( var name in this . props ) {
15- if ( this . props . hasOwnProperty ( name ) ) {
16- if ( excludedProps . indexOf ( name ) === - 1 ) {
17- _props [ name ] = this . props [ name ] ;
5+ createClass : function ( chartType , methodNames , dataKey ) {
6+ var excludedProps = [ 'data' , 'options' , 'redraw' ] ;
7+
8+ class ChartComponent extends React . Component {
9+ constructor ( props ) {
10+ super ( props ) ;
11+ this . displayName = chartType + 'Chart' ;
12+ this . chart = { } ;
13+ this . canvas = null ;
1814 }
19- }
20- }
21- return React . createElement ( 'canvas' , _props ) ;
22- }
23- } ;
2415
25- var extras = [ 'clear' , 'stop' , 'resize' , 'toBase64Image' , 'generateLegend' , 'update' , 'addData' , 'removeData' ] ;
26- function extra ( type ) {
27- classData [ type ] = function ( ) {
28- return this . state . chart [ type ] . apply ( this . state . chart , arguments ) ;
29- } ;
30- }
16+ componentDidMount ( ) {
17+ this . initializeChart ( ) ;
18+
19+ this . canvas = ReactDOM . findDOMNode ( this ) ;
20+
21+ var extras = [ 'clear' , 'stop' , 'resize' , 'toBase64Image' , 'generateLegend' , 'update' , 'addData' , 'removeData' ] ,
22+ i ;
23+ for ( i = 0 ; i < extras . length ; i ++ ) {
24+ this . extra ( extras [ i ] ) ;
25+ }
26+ for ( i = 0 ; i < methodNames . length ; i ++ ) {
27+ this . extra ( methodNames [ i ] ) ;
28+ }
29+ } ;
30+
31+ componentWillUnmount ( ) {
32+ var chart = this . chart ;
33+ if ( chart ) {
34+ chart . destroy ( ) ;
35+ }
36+ } ;
37+
38+ componentWillReceiveProps ( nextProps ) {
39+ var chart = this . chart ;
40+ if ( nextProps . redraw ) {
41+ chart . destroy ( ) ;
42+ this . initializeChart ( nextProps ) ;
43+ } else {
44+ dataKey = dataKey || dataKeys [ chart . name ] ;
45+ updatePoints ( nextProps , chart , dataKey ) ;
46+ if ( chart . scale ) {
47+ chart . scale . xLabels = nextProps . data . labels ;
48+
49+ if ( chart . scale . calculateXLabelRotation ) {
50+ chart . scale . calculateXLabelRotation ( ) ;
51+ }
52+ }
53+ chart . update ( ) ;
54+ }
55+ } ;
56+
57+ initializeChart ( ) {
58+ var Chart = require ( 'chart.js' ) ;
59+ var el = ReactDOM . findDOMNode ( this ) ;
60+ var ctx = el . getContext ( "2d" ) ;
61+ var chart = new Chart ( ctx ) [ chartType ] ( this . props . data , this . props . options || { } ) ;
62+ this . chart = chart ;
63+ } ;
64+
65+ extra ( type ) {
66+ this [ type ] = function ( ) {
67+ return this . chart [ type ] . apply ( this . chart , arguments ) ;
68+ } ;
69+ } ;
70+
71+ // return the chartjs instance
72+ getChart ( ) {
73+ return this . chart ;
74+ }
3175
32- classData . componentDidMount = function ( ) {
33- this . initializeChart ( this . props ) ;
34- } ;
76+ // return the canvass element that contains the chart
77+ getCanvass ( ) {
78+ return this . canvas ;
79+ } ;
3580
36- classData . componentWillUnmount = function ( ) {
37- var chart = this . state . chart ;
38- chart . destroy ( ) ;
39- } ;
81+ getCanvas ( ) { return this . getCanvass ( ) } ;
4082
41- classData . componentWillReceiveProps = function ( nextProps ) {
42- var chart = this . state . chart ;
43- if ( nextProps . redraw ) {
44- chart . destroy ( ) ;
45- this . initializeChart ( nextProps ) ;
46- } else {
47- dataKey = dataKey || dataKeys [ chart . name ] ;
48- updatePoints ( nextProps , chart , dataKey ) ;
49- if ( chart . scale ) {
50- chart . scale . xLabels = nextProps . data . labels ;
51-
52- if ( chart . scale . calculateXLabelRotation ) {
53- chart . scale . calculateXLabelRotation ( ) ;
83+ render ( ) {
84+ var _props = { } ;
85+ for ( var name in this . props ) {
86+ if ( this . props . hasOwnProperty ( name ) ) {
87+ if ( excludedProps . indexOf ( name ) === - 1 ) {
88+ _props [ name ] = this . props [ name ] ;
89+ }
90+ }
91+ }
92+
93+ return React . createElement ( 'canvas' , _props )
5494 }
5595 }
56- chart . update ( ) ;
57- }
58- } ;
59-
60- classData . initializeChart = function ( nextProps ) {
61- var Chart = require ( 'chart.js' ) ;
62- var el = ReactDOM . findDOMNode ( this ) ;
63- var ctx = el . getContext ( "2d" ) ;
64- var chart = new Chart ( ctx ) [ chartType ] ( nextProps . data , nextProps . options || { } ) ;
65- this . state . chart = chart ;
66- } ;
67-
68- // return the chartjs instance
69- classData . getChart = function ( ) {
70- return this . state . chart ;
71- } ;
72-
73- // return the canvass element that contains the chart
74- classData . getCanvass = function ( ) {
75- return this . refs . canvass ;
76- } ;
77-
78- classData . getCanvas = classData . getCanvass ;
79-
80- var i ;
81- for ( i = 0 ; i < extras . length ; i ++ ) {
82- extra ( extras [ i ] ) ;
83- }
84- for ( i = 0 ; i < methodNames . length ; i ++ ) {
85- extra ( methodNames [ i ] ) ;
86- }
8796
88- return React . createClass ( classData ) ;
89- }
97+ return ChartComponent ;
98+ }
9099} ;
91100
92101var dataKeys = {
0 commit comments