Skip to content

Commit

Permalink
Extract identical code into shared function
Browse files Browse the repository at this point in the history
  • Loading branch information
nickknissen committed Feb 5, 2018
1 parent 88b16e8 commit ebcab9f
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 152 deletions.
2 changes: 1 addition & 1 deletion src/examples/ReactiveExample.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Bar } from '../BaseCharts'
import reactiveData from '../mixins/reactiveData'
import { reactiveData } from '../mixins'

export default {
extends: Bar,
Expand Down
2 changes: 1 addition & 1 deletion src/examples/ReactivePropExample.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Bar } from '../BaseCharts'
import reactiveProp from '../mixins/reactiveProp'
import { reactiveProp } from '../mixins'

export default {
extends: Bar,
Expand Down
88 changes: 86 additions & 2 deletions src/mixins/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,89 @@
import reactiveData from './reactiveData.js'
import reactiveProp from './reactiveProp.js'
function dataHandler (newData, oldData) {
if (oldData) {
let chart = this.$data._chart

// Get new and old DataSet Labels
let newDatasetLabels = newData.datasets.map((dataset) => {
return dataset.label
})

let oldDatasetLabels = oldData.datasets.map((dataset) => {
return dataset.label
})

// 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) => {
// Get new and old dataset keys
const oldDatasetKeys = Object.keys(oldData.datasets[i])
const newDatasetKeys = Object.keys(dataset)

// Get keys that aren't present in the new data
const deletionKeys = oldDatasetKeys.filter((key) => {
return key !== '_meta' && newDatasetKeys.indexOf(key) === -1
})

// Remove outdated key-value pairs
deletionKeys.forEach((deletionKey) => {
delete chart.data.datasets[i][deletionKey]
})

// Update attributes individually to avoid re-rendering the entire chart
for (const attribute in dataset) {
if (dataset.hasOwnProperty(attribute)) {
chart.data.datasets[i][attribute] = dataset[attribute]
}
}
})

if (newData.hasOwnProperty('labels')) {
chart.data.labels = newData.labels
}
if (newData.hasOwnProperty('xLabels')) {
chart.data.xLabels = newData.xLabels
}
if (newData.hasOwnProperty('yLabels')) {
chart.data.yLabels = newData.yLabels
}
chart.update()
} else {
chart.destroy()
this.renderChart(this.chartData, this.options)
}
} else {
if (this.$data._chart) {
this.$data._chart.destroy()
}
this.renderChart(this.chartData, this.options)
}
}

export const reactiveData = {
data () {
return {
chartData: null
}
},

watch: {
'chartData': dataHandler
}
}

export const reactiveProp = {
props: {
chartData: {
required: true
}
},
watch: {
'chartData': dataHandler
}
}

export default {
reactiveData,
Expand Down
74 changes: 0 additions & 74 deletions src/mixins/reactiveData.js

This file was deleted.

74 changes: 0 additions & 74 deletions src/mixins/reactiveProp.js

This file was deleted.

0 comments on commit ebcab9f

Please sign in to comment.