mirror of
https://github.com/KevinMidboe/vue-chartjs.git
synced 2025-10-29 18:00:20 +00:00
✨ Add reactiveData mixin
This commit is contained in:
34
src/mixins/reactiveData.js
Normal file
34
src/mixins/reactiveData.js
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
module.exports = {
|
||||||
|
data () {
|
||||||
|
return {
|
||||||
|
chartData: null
|
||||||
|
}
|
||||||
|
},
|
||||||
|
watch: {
|
||||||
|
'chartData': {
|
||||||
|
handler (newData, oldData) {
|
||||||
|
if (oldData) {
|
||||||
|
let chart = this._chart
|
||||||
|
|
||||||
|
let newDataLabels = newData.datasets.map((dataset) => {
|
||||||
|
return dataset.label
|
||||||
|
})
|
||||||
|
|
||||||
|
let oldDataLabels = oldData.datasets.map((dataset) => {
|
||||||
|
return dataset.label
|
||||||
|
})
|
||||||
|
|
||||||
|
if (JSON.stringify(newDataLabels) === JSON.stringify(oldDataLabels)) {
|
||||||
|
newData.datasets.forEach((dataset, i) => {
|
||||||
|
chart.data.datasets[i].data = dataset.data
|
||||||
|
})
|
||||||
|
chart.data.labels = newData.labels
|
||||||
|
chart.update()
|
||||||
|
} else {
|
||||||
|
this.renderChart(this.chartData, this.options)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user