62 lines
1.1 KiB
Vue
62 lines
1.1 KiB
Vue
<template>
|
|
<div class="ui-charts-container">
|
|
<canvas ref="chartCanvas"></canvas>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { ref, onMounted, onUnmounted } from 'vue'
|
|
import { Chart, registerables } from 'chart.js'
|
|
import { Line, Bar, Pie } from 'vue-chartjs'
|
|
|
|
// 注册Chart.js组件
|
|
Chart.register(...registerables)
|
|
|
|
const props = defineProps({
|
|
type: {
|
|
type: String,
|
|
default: 'line',
|
|
validator: (value) => ['line', 'bar', 'pie'].includes(value)
|
|
},
|
|
data: {
|
|
type: Object,
|
|
required: true
|
|
},
|
|
options: {
|
|
type: Object,
|
|
default: () => ({})
|
|
}
|
|
})
|
|
|
|
const chartCanvas = ref(null)
|
|
let chartInstance = null
|
|
|
|
onMounted(() => {
|
|
if (chartCanvas.value) {
|
|
const ctx = chartCanvas.value.getContext('2d')
|
|
chartInstance = new Chart(ctx, {
|
|
type: props.type,
|
|
data: props.data,
|
|
options: {
|
|
responsive: true,
|
|
maintainAspectRatio: false,
|
|
...props.options
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
onUnmounted(() => {
|
|
if (chartInstance) {
|
|
chartInstance.destroy()
|
|
}
|
|
})
|
|
</script>
|
|
|
|
<style scoped>
|
|
.ui-charts-container {
|
|
width: 100%;
|
|
height: 300px;
|
|
position: relative;
|
|
}
|
|
</style> |