2022-06-28 01:42:26 +00:00
|
|
|
<template>
|
|
|
|
<canvas ref="chartEl"></canvas>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2023-01-03 01:12:37 +00:00
|
|
|
import { onMounted, onUnmounted, ref, shallowRef } from 'vue';
|
2023-01-02 01:18:47 +00:00
|
|
|
import { Chart } from 'chart.js';
|
2022-06-28 01:42:26 +00:00
|
|
|
import number from '@/filters/number';
|
|
|
|
import { defaultStore } from '@/store';
|
|
|
|
import { useChartTooltip } from '@/scripts/use-chart-tooltip';
|
2023-01-02 01:18:47 +00:00
|
|
|
import { initChart } from '@/scripts/init-chart';
|
2022-06-28 01:42:26 +00:00
|
|
|
|
2023-01-02 01:18:47 +00:00
|
|
|
initChart();
|
2022-06-28 01:42:26 +00:00
|
|
|
|
|
|
|
const props = defineProps<{
|
2022-06-30 13:02:08 +00:00
|
|
|
data: { name: string; value: number; color: string; onClick?: () => void }[];
|
2022-06-28 01:42:26 +00:00
|
|
|
}>();
|
|
|
|
|
2023-01-03 01:12:37 +00:00
|
|
|
const chartEl = shallowRef<HTMLCanvasElement>(null);
|
2022-06-28 01:42:26 +00:00
|
|
|
|
2022-12-26 04:26:21 +00:00
|
|
|
const { handler: externalTooltipHandler } = useChartTooltip({
|
|
|
|
position: 'middle',
|
|
|
|
});
|
2022-06-28 01:42:26 +00:00
|
|
|
|
|
|
|
let chartInstance: Chart;
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
chartInstance = new Chart(chartEl.value, {
|
|
|
|
type: 'doughnut',
|
|
|
|
data: {
|
|
|
|
labels: props.data.map(x => x.name),
|
|
|
|
datasets: [{
|
|
|
|
backgroundColor: props.data.map(x => x.color),
|
2022-06-30 12:38:34 +00:00
|
|
|
borderColor: getComputedStyle(document.documentElement).getPropertyValue('--panel'),
|
|
|
|
borderWidth: 2,
|
|
|
|
hoverOffset: 0,
|
2022-06-28 01:42:26 +00:00
|
|
|
data: props.data.map(x => x.value),
|
|
|
|
}],
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
layout: {
|
|
|
|
padding: {
|
2022-06-30 11:15:14 +00:00
|
|
|
left: 16,
|
|
|
|
right: 16,
|
|
|
|
top: 16,
|
|
|
|
bottom: 16,
|
2022-06-28 01:42:26 +00:00
|
|
|
},
|
|
|
|
},
|
2022-06-30 13:02:08 +00:00
|
|
|
onClick: (ev) => {
|
|
|
|
const hit = chartInstance.getElementsAtEventForMode(ev, 'nearest', { intersect: true }, false)[0];
|
|
|
|
if (hit && props.data[hit.index].onClick) {
|
|
|
|
props.data[hit.index].onClick();
|
|
|
|
}
|
|
|
|
},
|
2022-06-28 01:42:26 +00:00
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
display: false,
|
|
|
|
},
|
|
|
|
tooltip: {
|
|
|
|
enabled: false,
|
|
|
|
mode: 'index',
|
|
|
|
animation: {
|
|
|
|
duration: 0,
|
|
|
|
},
|
|
|
|
external: externalTooltipHandler,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2022-12-27 09:29:39 +00:00
|
|
|
<style lang="scss" scoped>
|
2022-06-28 01:42:26 +00:00
|
|
|
|
|
|
|
</style>
|