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

100 lines
2.7 KiB
Vue
Raw Permalink Normal View History

<script lang="ts" setup>
import { ref, watch, computed, onMounted } from 'vue';
import { Chart, LineElement, PointElement, LineController, LinearScale, CategoryScale, Tooltip, registerables } from 'chart.js';
import { Ref } from 'vue';
import { ChartDataCustomTypesPerDataset } from 'chart.js';
2023-03-03 15:54:28 +00:00
// interface Dataset {
// borderColor: string;
// // borderDash:
// // (0) []
// borderDashOffset: number;
// borderWidth: number;
// cubicInterpolationMode: string;
// data: Array<number>;
// fill: boolean;
// pointBackgroundColor: string;
// pointBorderColor: string;
// pointBorderWidth: number;
// pointHoverBackgroundColor: string;
// pointHoverBorderWidth: number;
// pointHoverRadius: number;
// pointRadius: number;
// tension: number;
// }
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<HTMLCanvasElement | null> = ref<HTMLCanvasElement | null>(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.register(...registerables);
// https://gitea.geologie.ac.at/kaiarn/geomon.viewer/src/branch/master/src/common/graphjs/geomon-timeseries-chart/geomon-timeseries-chart.component.ts
chart = new Chart(root.value, {
type: 'line',
data: props.data as ChartDataCustomTypesPerDataset<"line", number[], string>,
// data: {
// labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
// // labels: [],
// datasets: [
// {
// data: [86, 114, 106, 106, 107, 111, 133, 221, 783, 2478, 2600, 2700],
// label: "Africa",
// borderColor: "#3e95cd",
// fill: false
// }, {
// data: [282, 350, 411, 502, 635, 809, 947, 1402, 3700, 5267, 444, 555],
// label: "Asia",
// borderColor: "#8e5ea2",
// fill: false
// }
// ]
// },
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
display: false,
},
x: {
display: true,
},
},
plugins: {
legend: {
display: true,
},
},
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>