This repository was archived by the owner on Jul 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathcore.js
117 lines (95 loc) · 3.33 KB
/
core.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Designed to be used with the current v2.0-dev version of Chart.js
// It's not on NPM, but if you'd like to use it you can, install it
// by setting the chart.js version in your package.json to:
// "chart.js": "git://github.com/danmolitor/Chart.js.git#v2.0-dev"
// I'll try to rework this for their 2.0.0 beta as well.
var React = require('react');
var ReactDOM = require('react-dom');
var Chart = require('chart.js');
module.exports = {
createClass: function(chartType, methodNames, dataKey) {
var classData = {
displayName: chartType + 'Chart',
getInitialState: function() { return {}; },
render: function() {
var _props = {
ref: 'canvass'
};
for (var name in this.props) {
if (this.props.hasOwnProperty(name)) {
if (name !== 'data' && name !== 'options') {
_props[name] = this.props[name];
}
}
}
return React.createElement('canvas', _props);
}
};
var extras = ['clear', 'stop', 'resize', 'toBase64Image', 'generateLegend', 'update', 'addData', 'removeData'];
function extra(type) {
classData[type] = function() {
return this.state.chart[type].apply(this.state.chart, arguments);
};
}
classData.componentDidMount = function() {
this.initializeChart(this.props);
};
classData.componentWillUnmount = function() {
var chart = this.state.chart;
chart.destroy();
};
classData.componentWillReceiveProps = function(nextProps) {
var chart = this.state.chart;
var optsChange = JSON.stringify(nextProps.options) !== JSON.stringify(this.props.options);
if (nextProps.redraw || optsChange) {
chart.destroy(); // Reset the array of datasets
this.initializeChart(nextProps);
} else {
// assign all of the properites from the next datasets to the current chart
nextProps.data.datasets.forEach(function(set, setIndex) {
var chartDataset = {};
for (var property in set) {
if (set.hasOwnProperty(property)) {
chartDataset[property] = set[property];
}
}
chart.data.datasets[setIndex] = chartDataset;
});
chart.data.labels = nextProps.data.labels;
chart.update();
}
};
classData.initializeChart = function(nextProps) {
var el = ReactDOM.findDOMNode(this);
var ctx = el.getContext("2d");
var convertToType = function(string) {
if(string === 'PolarArea') { return 'polarArea'; }
if(string === 'HorizontalBar') { return 'horizontalBar'; }
return string.toLowerCase();
};
var type = convertToType(chartType);
this.state.chart = new Chart(ctx, {
type: type,
data: nextProps.data,
options: nextProps.options
});
};
// return the chartjs instance
classData.getChart = function() {
return this.state.chart;
};
// return the canvass element that contains the chart
classData.getCanvass = function() {
return this.refs.canvass;
};
classData.getCanvas = classData.getCanvass;
var i;
for (i=0; i<extras.length; i++) {
extra(extras[i]);
}
for (i=0; i<methodNames.length; i++) {
extra(methodNames[i]);
}
return React.createClass(classData);
}
};