2023-07-27 07:31:52 +02:00
|
|
|
<!--
|
2024-02-13 16:50:11 +01:00
|
|
|
SPDX-FileCopyrightText: syuilo and other misskey contributors
|
2023-07-27 07:31:52 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2023-05-12 03:29:27 +02:00
|
|
|
<template>
|
|
|
|
<canvas ref="chartEl"></canvas>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
|
|
|
import { onMounted, shallowRef } from 'vue';
|
|
|
|
import { Chart } from 'chart.js';
|
|
|
|
import tinycolor from 'tinycolor2';
|
2023-09-19 09:37:43 +02:00
|
|
|
import { defaultStore } from '@/store.js';
|
|
|
|
import { useChartTooltip } from '@/scripts/use-chart-tooltip.js';
|
|
|
|
import { chartVLine } from '@/scripts/chart-vline.js';
|
|
|
|
import { alpha } from '@/scripts/color.js';
|
|
|
|
import { initChart } from '@/scripts/init-chart.js';
|
2024-01-04 10:32:46 +01:00
|
|
|
import { misskeyApi } from '@/scripts/misskey-api.js';
|
2023-05-12 03:29:27 +02:00
|
|
|
|
|
|
|
initChart();
|
|
|
|
|
2024-01-30 11:53:53 +01:00
|
|
|
const chartEl = shallowRef<HTMLCanvasElement | null>(null);
|
2023-05-12 03:29:27 +02:00
|
|
|
|
|
|
|
const { handler: externalTooltipHandler } = useChartTooltip();
|
|
|
|
|
2024-01-30 11:53:53 +01:00
|
|
|
let chartInstance: Chart | null = null;
|
2023-05-12 03:29:27 +02:00
|
|
|
|
|
|
|
const getYYYYMMDD = (date: Date) => {
|
|
|
|
const y = date.getFullYear().toString().padStart(2, '0');
|
|
|
|
const m = (date.getMonth() + 1).toString().padStart(2, '0');
|
|
|
|
const d = date.getDate().toString().padStart(2, '0');
|
|
|
|
return `${y}/${m}/${d}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getDate = (ymd: string) => {
|
|
|
|
const [y, m, d] = ymd.split('-').map(x => parseInt(x, 10));
|
|
|
|
const date = new Date(y, m + 1, d, 0, 0, 0, 0);
|
|
|
|
return date;
|
|
|
|
};
|
|
|
|
|
|
|
|
onMounted(async () => {
|
2024-01-04 10:32:46 +01:00
|
|
|
let raw = await misskeyApi('retention', { });
|
2023-05-12 03:29:27 +02:00
|
|
|
|
|
|
|
const vLineColor = defaultStore.state.darkMode ? 'rgba(255, 255, 255, 0.2)' : 'rgba(0, 0, 0, 0.2)';
|
|
|
|
|
|
|
|
const accent = tinycolor(getComputedStyle(document.documentElement).getPropertyValue('--accent'));
|
|
|
|
const color = accent.toHex();
|
|
|
|
|
2024-01-30 11:53:53 +01:00
|
|
|
if (chartEl.value == null) return;
|
|
|
|
|
2023-05-12 03:29:27 +02:00
|
|
|
chartInstance = new Chart(chartEl.value, {
|
|
|
|
type: 'line',
|
|
|
|
data: {
|
|
|
|
labels: [],
|
|
|
|
datasets: raw.map((record, i) => ({
|
|
|
|
label: getYYYYMMDD(new Date(record.createdAt)),
|
|
|
|
pointRadius: 0,
|
|
|
|
borderWidth: 2,
|
|
|
|
borderJoinStyle: 'round',
|
|
|
|
borderColor: alpha(color, Math.min(1, (raw.length - (i - 1)) / raw.length)),
|
|
|
|
fill: false,
|
|
|
|
tension: 0.4,
|
|
|
|
data: [{
|
|
|
|
x: '0',
|
|
|
|
y: 100,
|
|
|
|
d: getYYYYMMDD(new Date(record.createdAt)),
|
|
|
|
}, ...Object.entries(record.data).sort((a, b) => getDate(a[0]) > getDate(b[0]) ? 1 : -1).map(([k, v], i) => ({
|
|
|
|
x: (i + 1).toString(),
|
|
|
|
y: (v / record.users) * 100,
|
|
|
|
d: getYYYYMMDD(new Date(record.createdAt)),
|
2024-01-30 11:53:53 +01:00
|
|
|
}))] as any,
|
2023-05-12 03:29:27 +02:00
|
|
|
})),
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
aspectRatio: 2.5,
|
|
|
|
layout: {
|
|
|
|
padding: {
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
top: 0,
|
|
|
|
bottom: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
scales: {
|
|
|
|
x: {
|
|
|
|
title: {
|
|
|
|
display: true,
|
|
|
|
text: 'Days later',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
y: {
|
|
|
|
title: {
|
|
|
|
display: true,
|
|
|
|
text: 'Rate (%)',
|
|
|
|
},
|
|
|
|
ticks: {
|
|
|
|
callback: (value, index, values) => value + '%',
|
|
|
|
},
|
2023-07-03 07:35:07 +02:00
|
|
|
min: 0,
|
2023-05-12 03:29:27 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
interaction: {
|
|
|
|
intersect: false,
|
|
|
|
},
|
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
display: false,
|
|
|
|
},
|
|
|
|
tooltip: {
|
|
|
|
enabled: false,
|
|
|
|
callbacks: {
|
|
|
|
title(context) {
|
2024-01-30 11:53:53 +01:00
|
|
|
const v = context[0].dataset.data[context[0].dataIndex] as unknown as { x: string, y: number, d: string };
|
2023-05-12 03:29:27 +02:00
|
|
|
return `${v.x} days later`;
|
|
|
|
},
|
|
|
|
label(context) {
|
2024-01-30 11:53:53 +01:00
|
|
|
const v = context.dataset.data[context.dataIndex] as unknown as { x: string, y: number, d: string };
|
2023-05-12 03:29:27 +02:00
|
|
|
const p = Math.round(v.y) + '%';
|
|
|
|
return `${v.d} ${p}`;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
mode: 'index',
|
|
|
|
animation: {
|
|
|
|
duration: 0,
|
|
|
|
},
|
|
|
|
external: externalTooltipHandler,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
plugins: [chartVLine(vLineColor)],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
</script>
|