iceshrimp-legacy/packages/client/src/components/MkMiniChart.vue

75 lines
1.9 KiB
Vue
Raw Normal View History

2018-06-11 04:24:29 +02:00
<template>
2023-04-08 02:01:42 +02:00
<svg :viewBox="`0 0 ${viewBoxX} ${viewBoxY}`" style="overflow: visible">
<defs>
<linearGradient :id="gradientId" x1="0" x2="0" y1="1" y2="0">
<stop offset="0%" :stop-color="color" stop-opacity="0"></stop>
<stop
offset="100%"
:stop-color="color"
stop-opacity="0.65"
></stop>
</linearGradient>
</defs>
<polygon
:points="polygonPoints"
:style="`stroke: none; fill: url(#${gradientId});`"
/>
<polyline
:points="polylinePoints"
fill="none"
:stroke="color"
stroke-width="2"
/>
<circle :cx="headX" :cy="headY" r="3" :fill="color" />
</svg>
2018-06-11 04:24:29 +02:00
</template>
2022-06-21 07:39:23 +02:00
<script lang="ts" setup>
2023-04-08 02:01:42 +02:00
import { onUnmounted, watch } from "vue";
import { v4 as uuid } from "uuid";
import tinycolor from "tinycolor2";
import { useInterval } from "@/scripts/use-interval";
2018-06-11 04:24:29 +02:00
2022-06-21 07:39:23 +02:00
const props = defineProps<{
src: number[];
}>();
const viewBoxX = 50;
const viewBoxY = 50;
const gradientId = uuid();
2023-04-08 02:01:42 +02:00
let polylinePoints = $ref("");
let polygonPoints = $ref("");
2022-06-21 07:39:23 +02:00
let headX = $ref<number | null>(null);
let headY = $ref<number | null>(null);
let clock = $ref<number | null>(null);
2023-04-08 02:01:42 +02:00
const accent = tinycolor(
getComputedStyle(document.documentElement).getPropertyValue("--accent")
);
2022-06-24 03:51:45 +02:00
const color = accent.toRgbString();
2022-06-21 07:39:23 +02:00
function draw(): void {
const stats = props.src.slice().reverse();
const peak = Math.max.apply(null, stats) || 1;
const _polylinePoints = stats.map((n, i) => [
i * (viewBoxX / (stats.length - 1)),
2023-04-08 02:01:42 +02:00
(1 - n / peak) * viewBoxY,
2022-06-21 07:39:23 +02:00
]);
2023-04-08 02:01:42 +02:00
polylinePoints = _polylinePoints.map((xy) => `${xy[0]},${xy[1]}`).join(" ");
2018-06-11 19:18:29 +02:00
2023-04-08 02:01:42 +02:00
polygonPoints = `0,${viewBoxY} ${polylinePoints} ${viewBoxX},${viewBoxY}`;
2018-06-11 04:24:29 +02:00
2022-06-21 07:39:23 +02:00
headX = _polylinePoints[_polylinePoints.length - 1][0];
headY = _polylinePoints[_polylinePoints.length - 1][1];
}
2018-06-11 05:52:47 +02:00
2022-06-21 07:39:23 +02:00
watch(() => props.src, draw, { immediate: true });
2018-06-11 04:24:29 +02:00
2022-06-21 07:39:23 +02:00
// Vueが何故かWatchを発動させない場合があるので
useInterval(draw, 1000, {
immediate: false,
afterMounted: true,
2018-06-11 04:24:29 +02:00
});
</script>