mirror of
https://github.com/KevinMidboe/vue-chartjs.git
synced 2025-10-29 18:00:20 +00:00
* ➕ Add dependency lodash * ✨ Add helper function to merge chartOptions * Change Charts to merge options * And renamed options data to defaultOptions
63 lines
1.1 KiB
JavaScript
63 lines
1.1 KiB
JavaScript
import Vue from 'vue'
|
|
import Chart from 'chart.js'
|
|
import { mergeOptions } from '../helpers/options'
|
|
|
|
export default Vue.extend({
|
|
template: `
|
|
<div>
|
|
<canvas id="line-chart" width=width height=height v-el:canvas></canvas>
|
|
</div>
|
|
`,
|
|
|
|
props: {
|
|
width: {
|
|
default: 400,
|
|
type: Number
|
|
},
|
|
height: {
|
|
default: 400,
|
|
type: Number
|
|
}
|
|
},
|
|
|
|
data () {
|
|
return {
|
|
defaultOptions: {
|
|
scales: {
|
|
yAxes: [{
|
|
ticks: {
|
|
beginAtZero: true
|
|
},
|
|
gridLines: {
|
|
display: false
|
|
}
|
|
}],
|
|
xAxes: [ {
|
|
gridLines: {
|
|
display: false
|
|
}
|
|
}]
|
|
}
|
|
}
|
|
}
|
|
},
|
|
|
|
methods: {
|
|
render (data, options) {
|
|
let chartOptions = mergeOptions(this.defaultOptions, options)
|
|
|
|
this._chart = new Chart(
|
|
this.$els.canvas.getContext('2d'), {
|
|
type: 'line',
|
|
data: data,
|
|
options: chartOptions
|
|
}
|
|
)
|
|
this._chart.generateLegend()
|
|
}
|
|
},
|
|
beforeDestroy () {
|
|
this._chart.destroy()
|
|
}
|
|
})
|