-
Notifications
You must be signed in to change notification settings - Fork 295
v2: Assign all nextProps dataset properties to chart #109
Changes from 2 commits
7f6c340
15059bc
1da6721
ad46cca
0cedffb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,51 +48,36 @@ module.exports = { | |
| classData.componentWillReceiveProps = function(nextProps) { | ||
| var chart = this.state.chart; | ||
|
|
||
| // // Reset the array of datasets | ||
| chart.data.datasets.forEach(function(set, setIndex) { | ||
| set.data.forEach(function(val, pointIndex) { | ||
| set.data = []; | ||
| }); | ||
| }); | ||
|
|
||
| // // Reset the array of labels | ||
| chart.data.labels = []; | ||
| if (nextProps.redraw) { | ||
| 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 = chart.data.datasets[setIndex]; | ||
|
|
||
| // Adds the datapoints from nextProps | ||
| nextProps.data.datasets.forEach(function(set, setIndex) { | ||
| set.data.forEach(function(val, pointIndex) { | ||
| chart.data.datasets[setIndex].data[pointIndex] = nextProps.data.datasets[setIndex].data[pointIndex]; | ||
| for (var property in set) { | ||
| if (set.hasOwnProperty(property)) { | ||
| chartDataset[property] = set[property]; | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| // Sets the labels from nextProps | ||
| nextProps.data.labels.forEach(function(val, labelIndex) { | ||
| chart.data.labels[labelIndex] = nextProps.data.labels[labelIndex]; | ||
| }); | ||
| chart.data.labels = nextProps.data.labels; | ||
|
|
||
| // Updates Chart with new data | ||
| chart.update(); | ||
| chart.update(); | ||
| } | ||
| }; | ||
|
|
||
| classData.initializeChart = function(nextProps) { | ||
| var Chart = require('chart.js'); | ||
| var el = ReactDOM.findDOMNode(this); | ||
| var ctx = el.getContext("2d"); | ||
|
|
||
| if (chartType === 'PolarArea'){ | ||
| var chart = new Chart(ctx, { | ||
| type: 'polarArea', | ||
| data: nextProps.data, | ||
| options: nextProps.options | ||
| }); | ||
| } else { | ||
| var chart = new Chart(ctx, { | ||
| type: chartType.toLowerCase(), | ||
| data: nextProps.data, | ||
| options: nextProps.options | ||
| }); | ||
| } | ||
| this.state.chart = chart; | ||
| this.state.chart = Chart[chartType](ctx, { | ||
| data: nextProps.data, | ||
| options: nextProps.options | ||
| }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be better to use
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dougmolineux I would agree.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is possible since initializeChart relies on the chart being mounted.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It also might not be possible to use setState since it is asynchronous, and the chart being available is required to be sync the way the code is currently written. I'd rather not turn this PR into a full re-factor. |
||
| }; | ||
|
|
||
|
|
||
|
|
@@ -118,4 +103,4 @@ module.exports = { | |
|
|
||
| return React.createClass(classData); | ||
| } | ||
| }; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there are certain penalty of doing this require as the initializeChart method is called on componentWillReceiveProps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm doubtful, but for the sake of clarity I'll move it up