tethys.backend/resources/js/Components/Charts/LineChart.vue

55 lines
1.1 KiB
Vue
Raw Normal View History

2023-03-03 15:54:28 +00:00
<script setup>
import { ref, watch, computed, onMounted } from 'vue';
import { Chart, LineElement, PointElement, LineController, LinearScale, CategoryScale, Tooltip } from 'chart.js';
2023-03-03 15:54:28 +00:00
const props = defineProps({
data: {
type: Object,
required: true,
},
});
2023-03-03 15:54:28 +00:00
const root = ref(null);
2023-03-03 15:54:28 +00:00
let chart;
2023-03-03 15:54:28 +00:00
Chart.register(LineElement, PointElement, LineController, LinearScale, CategoryScale, Tooltip);
2023-03-03 15:54:28 +00:00
onMounted(() => {
chart = new Chart(root.value, {
type: 'line',
data: props.data,
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
display: false,
},
x: {
display: true,
},
},
plugins: {
legend: {
display: false,
},
},
2023-03-03 15:54:28 +00:00
},
});
});
2023-03-03 15:54:28 +00:00
const chartData = computed(() => props.data);
2023-03-03 15:54:28 +00:00
watch(chartData, (data) => {
if (chart) {
chart.data = data;
chart.update();
}
});
2023-03-03 15:54:28 +00:00
</script>
<template>
<canvas ref="root" />
2023-03-03 15:54:28 +00:00
</template>