Skip to content

Commit

Permalink
Revert "wip: Refactor mixin in seperate functions"
Browse files Browse the repository at this point in the history
This reverts commit 8669452.
  • Loading branch information
apertureless committed Mar 20, 2018
1 parent 8669452 commit bfa1fb8
Showing 1 changed file with 49 additions and 56 deletions.
105 changes: 49 additions & 56 deletions src/mixins/index.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,65 @@
/* eslint-disable */

function dataHandler (newData, oldData) {
if (oldData) {
const chart = this.$data._chart
_checkChanges(newData, oldData, chart)
} else {
_destroyAndRenderChart(chart)
}
let chart = this.$data._chart

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

let oldDatasetLabels = oldData.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)
// 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)
// 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
})
// 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]
})
// 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]
// 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
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)
}
chart.update()
} else {
_destroyAndRenderChart(chart)
}
}

function _destroyAndRenderChart (chart) {
if (chart) {
chart.destroy()
if (this.$data._chart) {
this.$data._chart.destroy()
}
this.renderChart(this.chartData, this.options)
}
this.renderChart(this.chartData, this.options)
}

export const reactiveData = {
Expand All @@ -84,6 +77,7 @@ export const reactiveData = {
export const reactiveProp = {
props: {
chartData: {
type: Object,
required: true,
default: () => {}
}
Expand All @@ -97,4 +91,3 @@ export default {
reactiveData,
reactiveProp
}

0 comments on commit bfa1fb8

Please sign in to comment.