Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

Commit c711d75

Browse files
committed
Merge pull request #109 from ianks/label-fix-v2
v2: Assign all nextProps dataset properties to chart
2 parents 16b1022 + 0cedffb commit c711d75

File tree

2 files changed

+24
-37
lines changed

2 files changed

+24
-37
lines changed

dist/react-chartjs.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/core.js

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
var React = require('react');
99
var ReactDOM = require('react-dom');
10+
var Chart = require('chart.js');
1011

1112
module.exports = {
1213
createClass: function(chartType, methodNames, dataKey) {
@@ -48,51 +49,37 @@ module.exports = {
4849
classData.componentWillReceiveProps = function(nextProps) {
4950
var chart = this.state.chart;
5051

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];
6059

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+
}
6565
});
66-
});
6766

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;
7268

73-
// Updates Chart with new data
74-
chart.update();
69+
chart.update();
70+
}
7571
};
7672

7773
classData.initializeChart = function(nextProps) {
78-
var Chart = require('chart.js');
7974
var el = ReactDOM.findDOMNode(this);
8075
var ctx = el.getContext("2d");
76+
var type = (chartType === 'PolarArea') ? 'polarArea':chartType.toLowerCase();
8177

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+
});
9683
};
9784

9885

@@ -118,4 +105,4 @@ module.exports = {
118105

119106
return React.createClass(classData);
120107
}
121-
};
108+
};

0 commit comments

Comments
 (0)