Skip to content

Commit

Permalink
Change private data
Browse files Browse the repository at this point in the history
Implements #182. The private chart instance is now in the vue.js data model. And can be accessed over `this.$data._chart`
Updated unit tests
  • Loading branch information
apertureless committed Oct 14, 2017
1 parent 527a411 commit 60abf58
Show file tree
Hide file tree
Showing 20 changed files with 56 additions and 47 deletions.
7 changes: 4 additions & 3 deletions src/BaseCharts/Bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default {
},
data () {
return {
_chart: null,
defaultOptions: {
scales: {
yAxes: [{
Expand Down Expand Up @@ -74,7 +75,7 @@ export default {
},
renderChart (data, options) {
let chartOptions = mergeOptions(this.defaultOptions, options)
this._chart = new Chart(
this.$data._chart = new Chart(
this.$refs.canvas.getContext('2d'), {
type: 'bar',
data: data,
Expand All @@ -85,8 +86,8 @@ export default {
}
},
beforeDestroy () {
if (this._chart) {
this._chart.destroy()
if (this.$data._chart) {
this.$data._chart.destroy()
}
}
}
7 changes: 4 additions & 3 deletions src/BaseCharts/Bubble.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default {

data () {
return {
_chart: null,
defaultOptions: {
scales: {
yAxes: [{
Expand Down Expand Up @@ -77,7 +78,7 @@ export default {
renderChart (data, options) {
let chartOptions = mergeOptions(this.defaultOptions, options)

this._chart = new Chart(
this.$data._chart = new Chart(
this.$refs.canvas.getContext('2d'), {
type: 'bubble',
data: data,
Expand All @@ -88,8 +89,8 @@ export default {
}
},
beforeDestroy () {
if (this._chart) {
this._chart.destroy()
if (this.$data._chart) {
this.$data._chart.destroy()
}
}
}
7 changes: 4 additions & 3 deletions src/BaseCharts/Doughnut.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default {

data () {
return {
_chart: null,
defaultOptions: {
},
plugins: []
Expand All @@ -60,7 +61,7 @@ export default {
renderChart (data, options) {
let chartOptions = mergeOptions(this.defaultOptions, options)

this._chart = new Chart(
this.$data._chart = new Chart(
this.$refs.canvas.getContext('2d'), {
type: 'doughnut',
data: data,
Expand All @@ -71,8 +72,8 @@ export default {
}
},
beforeDestroy () {
if (this._chart) {
this._chart.destroy()
if (this.$data._chart) {
this.$data._chart.destroy()
}
}
}
7 changes: 4 additions & 3 deletions src/BaseCharts/HorizontalBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default {

data () {
return {
_chart: null,
defaultOptions: {
scales: {
yAxes: [{
Expand Down Expand Up @@ -76,7 +77,7 @@ export default {
},
renderChart (data, options, type) {
let chartOptions = mergeOptions(this.defaultOptions, options)
this._chart = new Chart(
this.$data._chart = new Chart(
this.$refs.canvas.getContext('2d'), {
type: 'horizontalBar',
data: data,
Expand All @@ -87,8 +88,8 @@ export default {
}
},
beforeDestroy () {
if (this._chart) {
this._chart.destroy()
if (this.$data._chart) {
this.$data._chart.destroy()
}
}
}
7 changes: 4 additions & 3 deletions src/BaseCharts/Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default {

data () {
return {
_chart: null,
defaultOptions: {
scales: {
yAxes: [{
Expand Down Expand Up @@ -75,7 +76,7 @@ export default {
renderChart (data, options) {
let chartOptions = mergeOptions(this.defaultOptions, options)

this._chart = new Chart(
this.$data._chart = new Chart(
this.$refs.canvas.getContext('2d'), {
type: 'line',
data: data,
Expand All @@ -86,8 +87,8 @@ export default {
}
},
beforeDestroy () {
if (this._chart) {
this._chart.destroy()
if (this.$data._chart) {
this.$data._chart.destroy()
}
}
}
7 changes: 4 additions & 3 deletions src/BaseCharts/Pie.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default {

data () {
return {
_chart: null,
defaultOptions: {
},
plugins: []
Expand All @@ -60,7 +61,7 @@ export default {
renderChart (data, options) {
let chartOptions = mergeOptions(this.defaultOptions, options)

this._chart = new Chart(
this.$data._chart = new Chart(
this.$refs.canvas.getContext('2d'), {
type: 'pie',
data: data,
Expand All @@ -71,8 +72,8 @@ export default {
}
},
beforeDestroy () {
if (this._chart) {
this._chart.destroy()
if (this.$data._chart) {
this.$data._chart.destroy()
}
}
}
7 changes: 4 additions & 3 deletions src/BaseCharts/PolarArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default {

data () {
return {
_chart: null,
defaultOptions: {
},
plugins: []
Expand All @@ -60,7 +61,7 @@ export default {
renderChart (data, options) {
let chartOptions = mergeOptions(this.defaultOptions, options)

this._chart = new Chart(
this.$data._chart = new Chart(
this.$refs.canvas.getContext('2d'), {
type: 'polarArea',
data: data,
Expand All @@ -71,8 +72,8 @@ export default {
}
},
beforeDestroy () {
if (this._chart) {
this._chart.destroy()
if (this.$data._chart) {
this.$data._chart.destroy()
}
}
}
7 changes: 4 additions & 3 deletions src/BaseCharts/Radar.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default {

data () {
return {
_chart: null,
defaultOptions: {
},
plugins: []
Expand All @@ -60,7 +61,7 @@ export default {
renderChart (data, options) {
let chartOptions = mergeOptions(this.defaultOptions, options)

this._chart = new Chart(
this.$data._chart = new Chart(
this.$refs.canvas.getContext('2d'), {
type: 'radar',
data: data,
Expand All @@ -71,8 +72,8 @@ export default {
}
},
beforeDestroy () {
if (this._chart) {
this._chart.destroy()
if (this.$data._chart) {
this.$data._chart.destroy()
}
}
}
7 changes: 4 additions & 3 deletions src/BaseCharts/Scatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default {

data () {
return {
_chart: null,
defaultOptions: {
scales: {
xAxes: [{
Expand All @@ -66,7 +67,7 @@ export default {
renderChart (data, options) {
let chartOptions = mergeOptions(this.defaultOptions, options)

this._chart = new Chart(
this.$data._chart = new Chart(
this.$refs.canvas.getContext('2d'), {
type: 'scatter',
data: data,
Expand All @@ -77,8 +78,8 @@ export default {
}
},
beforeDestroy () {
if (this._chart) {
this._chart.destroy()
if (this.$data._chart) {
this.$data._chart.destroy()
}
}
}
2 changes: 1 addition & 1 deletion src/mixins/reactiveData.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
'chartData': {
handler (newData, oldData) {
if (oldData) {
let chart = this._chart
let chart = this.$data._chart

// Get new and old DataSet Labels
let newDatasetLabels = newData.datasets.map((dataset) => {
Expand Down
2 changes: 1 addition & 1 deletion src/mixins/reactiveProp.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
'chartData': {
handler (newData, oldData) {
if (oldData) {
let chart = this._chart
let chart = this.$data._chart

// Get new and old DataSet Labels
let newDatasetLabels = newData.datasets.map((dataset) => {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/specs/Bar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ describe('BarChart', () => {
components: { BarChart }
}).$mount(el)

expect(vm.$children[0]._chart.chart.ctx).not.to.be.null
expect(vm.$children[0].$data._chart.chart.ctx).not.to.be.null

vm.$destroy()

vm.$nextTick(() => {
vm.$forceUpdate()
expect(vm.$children[0]._chart.chart.ctx).to.equal
expect(vm.$children[0].$data._chart.chart.ctx).to.equal
done()
})
})
Expand Down
4 changes: 2 additions & 2 deletions test/unit/specs/Bubble.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ describe('BubbleChart', () => {
components: { BubbleChart }
}).$mount(el)

expect(vm.$children[0]._chart.chart.ctx).not.to.be.null
expect(vm.$children[0].$data._chart.chart.ctx).not.to.be.null

vm.$destroy()

vm.$nextTick(() => {
vm.$forceUpdate()
expect(vm.$children[0]._chart.chart.ctx).to.be.null
expect(vm.$children[0].$data._chart.chart.ctx).to.be.null
done()
})
})
Expand Down
4 changes: 2 additions & 2 deletions test/unit/specs/Doughnut.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ describe('DoughnutChart', () => {
components: { DoughnutChart }
}).$mount(el)

expect(vm.$children[0]._chart.chart.ctx).not.to.be.null
expect(vm.$children[0].$data._chart.chart.ctx).not.to.be.null

vm.$destroy()

vm.$nextTick(() => {
vm.$forceUpdate()
expect(vm.$children[0]._chart.chart.ctx).to.be.null
expect(vm.$children[0].$data._chart.chart.ctx).to.be.null
done()
})
})
Expand Down
4 changes: 2 additions & 2 deletions test/unit/specs/HorizontalBar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ describe('HorizontalBarChart', () => {
components: { HorizontalBarChart }
}).$mount(el)

expect(vm.$children[0]._chart.chart.ctx).not.to.be.null
expect(vm.$children[0].$data._chart.chart.ctx).not.to.be.null

vm.$destroy()

vm.$nextTick(() => {
vm.$forceUpdate()
expect(vm.$children[0]._chart.chart.ctx).to.be.null
expect(vm.$children[0].$data._chart.chart.ctx).to.be.null
done()
})
})
Expand Down
4 changes: 2 additions & 2 deletions test/unit/specs/Line.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ describe('LineChart', () => {
components: { LineChart }
}).$mount(el)

expect(vm.$children[0]._chart.chart.ctx).not.to.be.null
expect(vm.$children[0].$data._chart.chart.ctx).not.to.be.null

vm.$destroy()

vm.$nextTick(() => {
vm.$forceUpdate()
expect(vm.$children[0]._chart.chart.ctx).to.be.null
expect(vm.$children[0].$data._chart.chart.ctx).to.be.null
done()
})
})
Expand Down
4 changes: 2 additions & 2 deletions test/unit/specs/Pie.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ describe('PieChart', () => {
components: { PieChart }
}).$mount(el)

expect(vm.$children[0]._chart.chart.ctx).not.to.be.null
expect(vm.$children[0].$data._chart.chart.ctx).not.to.be.null

vm.$destroy()

vm.$nextTick(() => {
vm.$forceUpdate()
expect(vm.$children[0]._chart.chart.ctx).to.be.null
expect(vm.$children[0].$data._chart.chart.ctx).to.be.null
done()
})
})
Expand Down
4 changes: 2 additions & 2 deletions test/unit/specs/PolarArea.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ describe('PolarChart', () => {
components: { PolarChart }
}).$mount(el)

expect(vm.$children[0]._chart.chart.ctx).not.to.be.null
expect(vm.$children[0].$data._chart.chart.ctx).not.to.be.null

vm.$destroy()

vm.$nextTick(() => {
vm.$forceUpdate()
expect(vm.$children[0]._chart.chart.ctx).to.be.null
expect(vm.$children[0].$data._chart.chart.ctx).to.be.null
done()
})
})
Expand Down
4 changes: 2 additions & 2 deletions test/unit/specs/Radar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ describe('RadarChart', () => {
components: { RadarChart }
}).$mount(el)

expect(vm.$children[0]._chart.chart.ctx).not.to.be.null
expect(vm.$children[0].$data._chart.chart.ctx).not.to.be.null

vm.$destroy()

vm.$nextTick(() => {
vm.$forceUpdate()
expect(vm.$children[0]._chart.chart.ctx).to.be.null
expect(vm.$children[0].$data._chart.chart.ctx).to.be.null
done()
})
})
Expand Down
Loading

0 comments on commit 60abf58

Please sign in to comment.