Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #324: Re-using the same dataset in another chart (for example, in tabs) causes a failure. #325

Merged
merged 1 commit into from
Mar 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ChartComponent extends React.Component {

componentDidUpdate() {
if (this.props.redraw) {
this.chartInstance.destroy();
this.destroyChart();
this.renderChart();
return;
}
Expand Down Expand Up @@ -109,7 +109,7 @@ class ChartComponent extends React.Component {
}

componentWillUnmount() {
this.chartInstance.destroy();
this.destroyChart();
}

transformDataProp(props) {
Expand Down Expand Up @@ -142,6 +142,8 @@ class ChartComponent extends React.Component {
})
};

this.saveCurrentDatasets(); // to remove the dataset metadata from this chart when the chart is destroyed

return data;
}

Expand All @@ -164,6 +166,18 @@ class ChartComponent extends React.Component {
}
}

getCurrentDatasets() {
return (this.chartInstance && this.chartInstance.config.data && this.chartInstance.config.data.datasets) || [];
}

saveCurrentDatasets() {
this.datasets = this.datasets || {};
var currentDatasets = this.getCurrentDatasets();
currentDatasets.forEach(d => {
this.datasets[this.props.datasetKeyProvider(d)] = d;
})
}

updateChart() {
const {options} = this.props;

Expand All @@ -177,7 +191,7 @@ class ChartComponent extends React.Component {

// Pipe datasets to chart instance datasets enabling
// seamless transitions
let currentDatasets = (this.chartInstance.config.data && this.chartInstance.config.data.datasets) || [];
let currentDatasets = this.getCurrentDatasets();
const nextDatasets = data.datasets || [];
this.checkDatasets(currentDatasets);

Expand Down Expand Up @@ -238,6 +252,19 @@ class ChartComponent extends React.Component {
});
}

destroyChart() {
// Put all of the datasets that have existed in the chart back on the chart
// so that the metadata associated with this chart get destroyed.
// This allows the datasets to be used in another chart. This can happen,
// for example, in a tabbed UI where the chart gets created each time the
// tab gets switched to the chart and uses the same data).
this.saveCurrentDatasets();
const datasets = Object.values(this.datasets);
this.chartInstance.config.data.datasets = datasets;

this.chartInstance.destroy();
}

handleOnClick = (event) => {
const instance = this.chartInstance;

Expand Down