Skip to content

Commit ebcab9f

Browse files
committed
Extract identical code into shared function
1 parent 88b16e8 commit ebcab9f

File tree

5 files changed

+88
-152
lines changed

5 files changed

+88
-152
lines changed

src/examples/ReactiveExample.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Bar } from '../BaseCharts'
2-
import reactiveData from '../mixins/reactiveData'
2+
import { reactiveData } from '../mixins'
33

44
export default {
55
extends: Bar,

src/examples/ReactivePropExample.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Bar } from '../BaseCharts'
2-
import reactiveProp from '../mixins/reactiveProp'
2+
import { reactiveProp } from '../mixins'
33

44
export default {
55
extends: Bar,

src/mixins/index.js

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,89 @@
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+
}
387

488
export default {
589
reactiveData,

src/mixins/reactiveData.js

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/mixins/reactiveProp.js

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)