iceshrimp-legacy/packages/client/src/widgets/activity.chart.vue

121 lines
2.5 KiB
Vue
Raw Normal View History

2020-02-18 11:31:11 +01:00
<template>
2023-04-08 02:01:42 +02:00
<svg
:viewBox="`0 0 ${viewBoxX} ${viewBoxY}`"
@mousedown.prevent="onMousedown"
>
<polyline
:points="pointsNote"
fill="none"
stroke-width="1"
stroke="#c4a7e7"
/>
<polyline
:points="pointsReply"
fill="none"
stroke-width="1"
stroke="#eb6f92"
/>
<polyline
:points="pointsRenote"
fill="none"
stroke-width="1"
stroke="#ebbcba"
/>
<polyline
:points="pointsTotal"
fill="none"
stroke-width="1"
stroke="#6e6a86"
stroke-dasharray="2 2"
/>
</svg>
2020-02-18 11:31:11 +01:00
</template>
<script lang="ts" setup>
const props = defineProps<{
2023-04-08 02:01:42 +02:00
activity: any[];
}>();
2023-07-17 00:32:32 +02:00
const viewBoxX: number = $ref(147);
const viewBoxY: number = $ref(60);
let zoom: number = $ref(1),
pos: number = $ref(0),
pointsNote: any = $ref(null),
pointsReply: any = $ref(null),
pointsRenote: any = $ref(null),
pointsTotal: any = $ref(null);
2020-02-18 11:31:11 +01:00
function dragListen(fn) {
2023-04-08 02:01:42 +02:00
window.addEventListener("mousemove", fn);
window.addEventListener("mouseleave", dragClear.bind(null, fn));
window.addEventListener("mouseup", dragClear.bind(null, fn));
2020-02-18 11:31:11 +01:00
}
function dragClear(fn) {
2023-04-08 02:01:42 +02:00
window.removeEventListener("mousemove", fn);
window.removeEventListener("mouseleave", dragClear);
window.removeEventListener("mouseup", dragClear);
2020-02-18 11:31:11 +01:00
}
function onMousedown(ev) {
const clickX = ev.clientX;
const clickY = ev.clientY;
const baseZoom = zoom;
const basePos = pos;
2020-02-18 11:31:11 +01:00
// 動かした時
2023-04-08 02:01:42 +02:00
dragListen((me) => {
2023-07-17 00:32:32 +02:00
const moveLeft = me.clientX - clickX;
const moveTop = me.clientY - clickY;
2020-02-18 11:31:11 +01:00
2023-04-08 02:01:42 +02:00
zoom = Math.max(1, baseZoom + -moveTop / 20);
pos = Math.min(0, basePos + moveLeft);
2023-04-08 02:01:42 +02:00
if (pos < -((props.activity.length - 1) * zoom - viewBoxX))
pos = -((props.activity.length - 1) * zoom - viewBoxX);
2020-02-18 11:31:11 +01:00
render();
});
}
2020-02-18 11:31:11 +01:00
function render() {
2023-04-08 02:01:42 +02:00
const peak = Math.max(...props.activity.map((d) => d.total));
if (peak !== 0) {
const activity = props.activity.slice().reverse();
2023-04-08 02:01:42 +02:00
pointsNote = activity
.map(
2023-07-06 03:28:27 +02:00
(d, i) =>
`${i * zoom + pos},${(1 - d.notes / peak) * viewBoxY}`,
2023-04-08 02:01:42 +02:00
)
.join(" ");
pointsReply = activity
.map(
(d, i) =>
2023-07-06 03:28:27 +02:00
`${i * zoom + pos},${(1 - d.replies / peak) * viewBoxY}`,
2023-04-08 02:01:42 +02:00
)
.join(" ");
pointsRenote = activity
.map(
(d, i) =>
2023-07-06 03:28:27 +02:00
`${i * zoom + pos},${(1 - d.renotes / peak) * viewBoxY}`,
2023-04-08 02:01:42 +02:00
)
.join(" ");
pointsTotal = activity
.map(
2023-07-06 03:28:27 +02:00
(d, i) =>
`${i * zoom + pos},${(1 - d.total / peak) * viewBoxY}`,
2023-04-08 02:01:42 +02:00
)
.join(" ");
2020-02-18 11:31:11 +01:00
}
}
2020-02-18 11:31:11 +01:00
</script>
<style lang="scss" scoped>
svg {
display: block;
padding: 16px;
width: 100%;
box-sizing: border-box;
cursor: all-scroll;
}
</style>