mirror of
https://github.com/KevinMidboe/vue-chartjs.git
synced 2025-10-29 18:00:20 +00:00
🔨 Change reactiveMixins
Destroy chart instance on render() Add additional check for dataset length Add comments Refactor reactiveData to match reactiveProp style
This commit is contained in:
@@ -10,35 +10,33 @@ module.exports = {
|
||||
if (oldData) {
|
||||
let chart = this._chart
|
||||
|
||||
let newDataLabels = newData.datasets.map((dataset) => {
|
||||
// Get new and old DataSet Labels
|
||||
let newDatasetLabels = newData.datasets.map((dataset) => {
|
||||
return dataset.label
|
||||
})
|
||||
|
||||
let oldDataLabels = oldData.datasets.map((dataset) => {
|
||||
let oldDatasetLabels = oldData.datasets.map((dataset) => {
|
||||
return dataset.label
|
||||
})
|
||||
|
||||
if (JSON.stringify(newDataLabels) === JSON.stringify(oldDataLabels)) {
|
||||
this.forceUpdate(newData, chart)
|
||||
// Stringify 'em for easier compare
|
||||
const oldLabels = JSON.stringify(oldDatasetLabels)
|
||||
const newLabels = JSON.stringify(newDatasetLabels)
|
||||
|
||||
// Check if Labels are equal and if dataset length is equal
|
||||
if (newLabels === oldLabels && oldData.datasets.length === newData.datasets.length) {
|
||||
newData.datasets.forEach((dataset, i) => {
|
||||
chart.data.datasets[i].data = dataset.data
|
||||
})
|
||||
|
||||
chart.data.labels = newData.labels
|
||||
chart.update()
|
||||
} else {
|
||||
this.forceRender()
|
||||
chart.destroy()
|
||||
this.renderChart(this.chartData, this.options)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
forceUpdate (newData, chart) {
|
||||
newData.datasets.forEach((dataset, i) => {
|
||||
chart.data.datasets[i].data = dataset.data
|
||||
})
|
||||
|
||||
chart.data.labels = newData.labels
|
||||
chart.update()
|
||||
},
|
||||
|
||||
forceRender () {
|
||||
this.renderChart(this.chartData, this.options)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,25 +11,33 @@ module.exports = {
|
||||
if (oldData) {
|
||||
let chart = this._chart
|
||||
|
||||
let newDataLabels = newData.datasets.map((dataset) => {
|
||||
// Get new and old DataSet Labels
|
||||
let newDatasetLabels = newData.datasets.map((dataset) => {
|
||||
return dataset.label
|
||||
})
|
||||
|
||||
let oldDataLabels = oldData.datasets.map((dataset) => {
|
||||
let oldDatasetLabels = oldData.datasets.map((dataset) => {
|
||||
return dataset.label
|
||||
})
|
||||
|
||||
if (JSON.stringify(newDataLabels) === JSON.stringify(oldDataLabels)) {
|
||||
// Stringify 'em for easier compare
|
||||
const oldLabels = JSON.stringify(oldDatasetLabels)
|
||||
const newLabels = JSON.stringify(newDatasetLabels)
|
||||
|
||||
// Check if Labels are equal and if dataset length is equal
|
||||
if (newLabels === oldLabels && oldData.datasets.length === newData.datasets.length) {
|
||||
newData.datasets.forEach((dataset, i) => {
|
||||
chart.data.datasets[i].data = dataset.data
|
||||
})
|
||||
|
||||
chart.data.labels = newData.labels
|
||||
chart.update()
|
||||
} else {
|
||||
chart.destroy()
|
||||
this.renderChart(this.chartData, this.options)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user