mirror of
https://github.com/KevinMidboe/vue-chartjs.git
synced 2025-10-29 18:00:20 +00:00
✨ Add reactiveProperty mixin
This commit is contained in:
35
src/mixins/reactiveProp.js
Normal file
35
src/mixins/reactiveProp.js
Normal file
@@ -0,0 +1,35 @@
|
||||
module.exports = {
|
||||
props: {
|
||||
chartData: {
|
||||
required: true
|
||||
}
|
||||
},
|
||||
|
||||
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