Skip to content

Commit 6bdf66f

Browse files
authored
Keep legend length in sync with data length
If you have a line chart that's data prop has a dataset value with a variable length of objects (eg a chart with a non-constant number of legend items), the original updateChart can only account for datasets that are growing in size, not shrinking. I added a quick check to the updateChart function that pops out the extraneous array values, if necessary. This saves you from having to call redraw over and over and can make your app run much smoother.
1 parent c8e84aa commit 6bdf66f

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ class ChartComponent extends React.Component {
117117
// seamless transitions
118118
let currentDatasets = (this.chart_instance.config.data && this.chart_instance.config.data.datasets) || [];
119119
const nextDatasets = data.datasets || [];
120+
121+
// Prevent charting of legend items that no longer exist
122+
while (currentDatasets.length > nextDatasets.length) {
123+
currentDatasets.pop();
124+
}
120125

121126
nextDatasets.forEach((dataset, sid) => {
122127
if (currentDatasets[sid] && currentDatasets[sid].data) {

0 commit comments

Comments
 (0)