iceshrimp-legacy/packages/client/src/pages/admin/queue.chart.chart.vue

194 lines
3.5 KiB
Vue
Raw Normal View History

2022-06-25 16:01:40 +02:00
<template>
2023-04-08 02:01:42 +02:00
<canvas ref="chartEl"></canvas>
2022-06-25 16:01:40 +02:00
</template>
<script lang="ts" setup>
2023-04-08 02:01:42 +02:00
import { watch, onMounted, onUnmounted, ref } from "vue";
2022-06-25 16:01:40 +02:00
import {
Chart,
ArcElement,
LineElement,
BarElement,
PointElement,
BarController,
LineController,
CategoryScale,
LinearScale,
TimeScale,
Legend,
Title,
Tooltip,
SubTitle,
Filler,
2023-04-08 02:01:42 +02:00
} from "chart.js";
import number from "@/filters/number";
import * as os from "@/os";
import { defaultStore } from "@/store";
import { useChartTooltip } from "@/scripts/use-chart-tooltip";
2022-06-25 16:01:40 +02:00
Chart.register(
ArcElement,
LineElement,
BarElement,
PointElement,
BarController,
LineController,
CategoryScale,
LinearScale,
TimeScale,
Legend,
Title,
Tooltip,
SubTitle,
2023-04-08 02:01:42 +02:00
Filler
2022-06-25 16:01:40 +02:00
);
const props = defineProps<{
type: string;
}>();
const alpha = (hex, a) => {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)!;
const r = parseInt(result[1], 16);
const g = parseInt(result[2], 16);
const b = parseInt(result[3], 16);
return `rgba(${r}, ${g}, ${b}, ${a})`;
};
const chartEl = ref<HTMLCanvasElement>(null);
2023-04-08 02:01:42 +02:00
const gridColor = defaultStore.state.darkMode
? "rgba(255, 255, 255, 0.1)"
: "rgba(0, 0, 0, 0.1)";
2022-06-25 16:01:40 +02:00
// フォントカラー
2023-04-08 02:01:42 +02:00
Chart.defaults.color = getComputedStyle(
document.documentElement
).getPropertyValue("--fg");
2022-06-25 16:01:40 +02:00
const { handler: externalTooltipHandler } = useChartTooltip();
let chartInstance: Chart;
function setData(values) {
if (chartInstance == null) return;
for (const value of values) {
2023-04-08 02:01:42 +02:00
chartInstance.data.labels.push("");
2022-06-25 16:01:40 +02:00
chartInstance.data.datasets[0].data.push(value);
if (chartInstance.data.datasets[0].data.length > 200) {
chartInstance.data.labels.shift();
chartInstance.data.datasets[0].data.shift();
}
}
chartInstance.update();
}
function pushData(value) {
if (chartInstance == null) return;
2023-04-08 02:01:42 +02:00
chartInstance.data.labels.push("");
2022-06-25 16:01:40 +02:00
chartInstance.data.datasets[0].data.push(value);
if (chartInstance.data.datasets[0].data.length > 200) {
chartInstance.data.labels.shift();
chartInstance.data.datasets[0].data.shift();
}
chartInstance.update();
}
const label =
2023-04-08 02:01:42 +02:00
props.type === "process"
? "Process"
: props.type === "active"
? "Active"
: props.type === "delayed"
? "Delayed"
: props.type === "waiting"
? "Waiting"
: ("?" as never);
2022-06-25 16:01:40 +02:00
const color =
2023-04-08 02:01:42 +02:00
props.type === "process"
? "#9ccfd8"
: props.type === "active"
? "#31748f"
: props.type === "delayed"
? "#eb6f92"
: props.type === "waiting"
? "#f6c177"
: ("?" as never);
2022-06-25 16:01:40 +02:00
onMounted(() => {
chartInstance = new Chart(chartEl.value, {
2023-04-08 02:01:42 +02:00
type: "line",
2022-06-25 16:01:40 +02:00
data: {
labels: [],
2023-04-08 02:01:42 +02:00
datasets: [
{
label: label,
pointRadius: 0,
tension: 0.3,
borderWidth: 2,
borderJoinStyle: "round",
borderColor: color,
backgroundColor: alpha(color, 0.1),
data: [],
},
],
2022-06-25 16:01:40 +02:00
},
options: {
aspectRatio: 2.5,
layout: {
padding: {
left: 0,
2022-06-28 03:42:54 +02:00
right: 0,
2022-06-25 16:01:40 +02:00
top: 0,
bottom: 0,
},
},
scales: {
x: {
grid: {
display: true,
color: gridColor,
2023-04-08 02:01:42 +02:00
borderColor: "rgb(0, 0, 0, 0)",
2022-06-25 16:01:40 +02:00
},
ticks: {
display: false,
maxTicksLimit: 10,
},
},
y: {
min: 0,
grid: {
color: gridColor,
2023-04-08 02:01:42 +02:00
borderColor: "rgb(0, 0, 0, 0)",
2022-06-25 16:01:40 +02:00
},
},
},
interaction: {
intersect: false,
},
plugins: {
legend: {
display: false,
},
tooltip: {
enabled: false,
2023-04-08 02:01:42 +02:00
mode: "index",
2022-06-25 16:01:40 +02:00
animation: {
duration: 0,
},
external: externalTooltipHandler,
},
},
},
});
});
defineExpose({
setData,
pushData,
});
</script>
2023-04-08 02:01:42 +02:00
<style lang="scss" scoped></style>