Newer
Older
<template>
<div>
<canvas :id="id" @click="test($event)"></canvas>
</div>
</template>
<script>
export const chartTypes = [
"line",
"bar",
"doughnut",
"bubble",
"scatter"
// 'radar',
// 'polarArea'
]
export default {
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
props: {
id: {
type: String,
default: "my-chart",
required: true
},
type: {
type: String,
default: "line",
required: true,
validator: function (value) {
return chartTypes.indexOf(value) !== -1
}
},
data: {
type: Object,
default: undefined,
required: true
},
options: {
type: Object,
default: undefined
}
},
data() {
return {
chart: undefined,
chartData: {
type: this.type,
data: this.data,
options: this.options
}
}
},
methods: {
createChart() {
this.chart?.destroy()
this.chart = new Chart(document.getElementById(this.id), this.chartData)
}
},
mounted() {
this.createChart()
},
watch: {
type: {
handler(n, o) {
this.chartData.type = n
this.createChart()
}
},
data: {
handler(n, o) {
this.chartData.data = n
this.createChart()
}
},
options: {
handler(n, o) {
this.chartData.options = n
this.createChart()
}
}
}