|
1 |
| -import reactiveData from './reactiveData.js' |
2 |
| -import reactiveProp from './reactiveProp.js' |
| 1 | +function dataHandler (newData, oldData) { |
| 2 | + if (oldData) { |
| 3 | + let chart = this.$data._chart |
| 4 | + |
| 5 | + // Get new and old DataSet Labels |
| 6 | + let newDatasetLabels = newData.datasets.map((dataset) => { |
| 7 | + return dataset.label |
| 8 | + }) |
| 9 | + |
| 10 | + let oldDatasetLabels = oldData.datasets.map((dataset) => { |
| 11 | + return dataset.label |
| 12 | + }) |
| 13 | + |
| 14 | + // Stringify 'em for easier compare |
| 15 | + const oldLabels = JSON.stringify(oldDatasetLabels) |
| 16 | + const newLabels = JSON.stringify(newDatasetLabels) |
| 17 | + |
| 18 | + // Check if Labels are equal and if dataset length is equal |
| 19 | + if (newLabels === oldLabels && oldData.datasets.length === newData.datasets.length) { |
| 20 | + newData.datasets.forEach((dataset, i) => { |
| 21 | + // Get new and old dataset keys |
| 22 | + const oldDatasetKeys = Object.keys(oldData.datasets[i]) |
| 23 | + const newDatasetKeys = Object.keys(dataset) |
| 24 | + |
| 25 | + // Get keys that aren't present in the new data |
| 26 | + const deletionKeys = oldDatasetKeys.filter((key) => { |
| 27 | + return key !== '_meta' && newDatasetKeys.indexOf(key) === -1 |
| 28 | + }) |
| 29 | + |
| 30 | + // Remove outdated key-value pairs |
| 31 | + deletionKeys.forEach((deletionKey) => { |
| 32 | + delete chart.data.datasets[i][deletionKey] |
| 33 | + }) |
| 34 | + |
| 35 | + // Update attributes individually to avoid re-rendering the entire chart |
| 36 | + for (const attribute in dataset) { |
| 37 | + if (dataset.hasOwnProperty(attribute)) { |
| 38 | + chart.data.datasets[i][attribute] = dataset[attribute] |
| 39 | + } |
| 40 | + } |
| 41 | + }) |
| 42 | + |
| 43 | + if (newData.hasOwnProperty('labels')) { |
| 44 | + chart.data.labels = newData.labels |
| 45 | + } |
| 46 | + if (newData.hasOwnProperty('xLabels')) { |
| 47 | + chart.data.xLabels = newData.xLabels |
| 48 | + } |
| 49 | + if (newData.hasOwnProperty('yLabels')) { |
| 50 | + chart.data.yLabels = newData.yLabels |
| 51 | + } |
| 52 | + chart.update() |
| 53 | + } else { |
| 54 | + chart.destroy() |
| 55 | + this.renderChart(this.chartData, this.options) |
| 56 | + } |
| 57 | + } else { |
| 58 | + if (this.$data._chart) { |
| 59 | + this.$data._chart.destroy() |
| 60 | + } |
| 61 | + this.renderChart(this.chartData, this.options) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +export const reactiveData = { |
| 66 | + data () { |
| 67 | + return { |
| 68 | + chartData: null |
| 69 | + } |
| 70 | + }, |
| 71 | + |
| 72 | + watch: { |
| 73 | + 'chartData': dataHandler |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +export const reactiveProp = { |
| 78 | + props: { |
| 79 | + chartData: { |
| 80 | + required: true |
| 81 | + } |
| 82 | + }, |
| 83 | + watch: { |
| 84 | + 'chartData': dataHandler |
| 85 | + } |
| 86 | +} |
3 | 87 |
|
4 | 88 | export default {
|
5 | 89 | reactiveData,
|
|
0 commit comments