primevue-mirror/src/components/chart/Chart.vue

87 lines
1.9 KiB
Vue
Raw Normal View History

2019-01-01 18:53:12 +00:00
<template>
<div class="p-chart">
<canvas ref="canvas" :width="width" :height="height" @click="onCanvasClick($event)"></canvas>
2019-01-01 18:53:12 +00:00
</div>
</template>
<script>
2019-06-24 21:59:35 +00:00
import * as Chart from 'chart.js';
2019-01-01 18:53:12 +00:00
export default {
props: {
type: String,
data: null,
options: null,
width: Number,
height: Number
},
chart: null,
mounted() {
this.initChart();
},
beforeUnmount() {
2019-01-01 18:53:12 +00:00
if (this.chart) {
this.chart.destroy();
this.chart = null;
}
},
watch: {
data() {
this.reinit();
},
type() {
this.reinit();
},
options() {
this.reinit();
}
},
methods: {
initChart() {
2019-06-24 21:59:35 +00:00
this.chart = new Chart(this.$refs.canvas, {
2019-01-01 18:53:12 +00:00
type: this.type,
data: this.data,
options: this.options
});
},
getCanvas() {
return this.$canvas;
},
getBase64Image() {
return this.chart.toBase64Image();
},
refresh() {
if (this.chart) {
this.chart.update();
}
},
reinit() {
if (this.chart) {
this.chart.destroy();
this.initChart();
}
},
onCanvasClick(event) {
if (this.chart) {
const element = this.chart.getElementAtEvent(event);
const dataset = this.chart.getDatasetAtEvent(event);
if (element && element[0] && dataset) {
this.$emit('select', {originalEvent: event, element: element[0], dataset: dataset});
}
}
},
generateLegend() {
if (this.chart) {
return this.chart.generateLegend();
}
2019-01-01 18:53:12 +00:00
}
}
}
</script>
<style>
.p-chart {
position: relative;
}
</style>