2023-07-27 08:31:52 +03:00
|
|
|
<!--
|
|
|
|
SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2022-06-28 04:42:26 +03:00
|
|
|
<template>
|
|
|
|
<canvas ref="chartEl"></canvas>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2023-02-16 16:09:41 +02:00
|
|
|
import { onMounted, shallowRef } from 'vue';
|
2023-01-02 03:18:47 +02:00
|
|
|
import { Chart } from 'chart.js';
|
2023-09-19 10:37:43 +03:00
|
|
|
import { useChartTooltip } from '@/scripts/use-chart-tooltip.js';
|
|
|
|
import { initChart } from '@/scripts/init-chart.js';
|
2022-06-28 04:42:26 +03:00
|
|
|
|
2023-12-26 07:19:35 +02:00
|
|
|
export type InstanceForPie = {
|
|
|
|
name: string,
|
|
|
|
color: string | null,
|
|
|
|
value: number,
|
|
|
|
onClick?: () => void
|
|
|
|
};
|
|
|
|
|
2023-01-02 03:18:47 +02:00
|
|
|
initChart();
|
2022-06-28 04:42:26 +03:00
|
|
|
|
|
|
|
const props = defineProps<{
|
2023-12-26 07:19:35 +02:00
|
|
|
data: InstanceForPie[];
|
2022-06-28 04:42:26 +03:00
|
|
|
}>();
|
|
|
|
|
2023-01-03 03:12:37 +02:00
|
|
|
const chartEl = shallowRef<HTMLCanvasElement>(null);
|
2022-06-28 04:42:26 +03:00
|
|
|
|
2022-12-26 06:26:21 +02:00
|
|
|
const { handler: externalTooltipHandler } = useChartTooltip({
|
|
|
|
position: 'middle',
|
|
|
|
});
|
2022-06-28 04:42:26 +03: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 15:38:34 +03:00
|
|
|
borderColor: getComputedStyle(document.documentElement).getPropertyValue('--panel'),
|
|
|
|
borderWidth: 2,
|
|
|
|
hoverOffset: 0,
|
2022-06-28 04:42:26 +03:00
|
|
|
data: props.data.map(x => x.value),
|
|
|
|
}],
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
layout: {
|
|
|
|
padding: {
|
2022-06-30 14:15:14 +03:00
|
|
|
left: 16,
|
|
|
|
right: 16,
|
|
|
|
top: 16,
|
|
|
|
bottom: 16,
|
2022-06-28 04:42:26 +03:00
|
|
|
},
|
|
|
|
},
|
2022-06-30 16:02:08 +03: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 04:42:26 +03:00
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
display: false,
|
|
|
|
},
|
|
|
|
tooltip: {
|
|
|
|
enabled: false,
|
|
|
|
mode: 'index',
|
|
|
|
animation: {
|
|
|
|
duration: 0,
|
|
|
|
},
|
|
|
|
external: externalTooltipHandler,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
</script>
|