Skip to content

Commit aa76f51

Browse files
committed
🔨 Change reactiveMixins
Destroy chart instance on render() Add additional check for dataset length Add comments Refactor reactiveData to match reactiveProp style
1 parent 53fb0d8 commit aa76f51

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

‎src/mixins/reactiveData.js‎

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,33 @@ module.exports = {
1010
if (oldData) {
1111
let chart = this._chart
1212

13-
let newDataLabels = newData.datasets.map((dataset) => {
13+
// Get new and old DataSet Labels
14+
let newDatasetLabels = newData.datasets.map((dataset) => {
1415
return dataset.label
1516
})
1617

17-
let oldDataLabels = oldData.datasets.map((dataset) => {
18+
let oldDatasetLabels = oldData.datasets.map((dataset) => {
1819
return dataset.label
1920
})
2021

21-
if (JSON.stringify(newDataLabels) === JSON.stringify(oldDataLabels)) {
22-
this.forceUpdate(newData, chart)
22+
// Stringify 'em for easier compare
23+
const oldLabels = JSON.stringify(oldDatasetLabels)
24+
const newLabels = JSON.stringify(newDatasetLabels)
25+
26+
// Check if Labels are equal and if dataset length is equal
27+
if (newLabels === oldLabels && oldData.datasets.length === newData.datasets.length) {
28+
newData.datasets.forEach((dataset, i) => {
29+
chart.data.datasets[i].data = dataset.data
30+
})
31+
32+
chart.data.labels = newData.labels
33+
chart.update()
2334
} else {
24-
this.forceRender()
35+
chart.destroy()
36+
this.renderChart(this.chartData, this.options)
2537
}
2638
}
2739
}
2840
}
2941
},
30-
methods: {
31-
forceUpdate (newData, chart) {
32-
newData.datasets.forEach((dataset, i) => {
33-
chart.data.datasets[i].data = dataset.data
34-
})
35-
36-
chart.data.labels = newData.labels
37-
chart.update()
38-
},
39-
40-
forceRender () {
41-
this.renderChart(this.chartData, this.options)
42-
}
43-
}
4442
}

‎src/mixins/reactiveProp.js‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,33 @@ module.exports = {
1111
if (oldData) {
1212
let chart = this._chart
1313

14-
let newDataLabels = newData.datasets.map((dataset) => {
14+
// Get new and old DataSet Labels
15+
let newDatasetLabels = newData.datasets.map((dataset) => {
1516
return dataset.label
1617
})
1718

18-
let oldDataLabels = oldData.datasets.map((dataset) => {
19+
let oldDatasetLabels = oldData.datasets.map((dataset) => {
1920
return dataset.label
2021
})
2122

22-
if (JSON.stringify(newDataLabels) === JSON.stringify(oldDataLabels)) {
23+
// Stringify 'em for easier compare
24+
const oldLabels = JSON.stringify(oldDatasetLabels)
25+
const newLabels = JSON.stringify(newDatasetLabels)
26+
27+
// Check if Labels are equal and if dataset length is equal
28+
if (newLabels === oldLabels && oldData.datasets.length === newData.datasets.length) {
2329
newData.datasets.forEach((dataset, i) => {
2430
chart.data.datasets[i].data = dataset.data
2531
})
32+
2633
chart.data.labels = newData.labels
2734
chart.update()
2835
} else {
36+
chart.destroy()
2937
this.renderChart(this.chartData, this.options)
3038
}
3139
}
3240
}
3341
}
34-
}
42+
},
3543
}

0 commit comments

Comments
 (0)