scoresaber-reloaded-v2/src/components/AnalyticsChart.tsx

111 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-10-27 15:26:23 +00:00
"use client";
2023-10-27 17:33:07 +00:00
import { ScoresaberMetricsHistory } from "@/schemas/fascinated/scoresaberMetricsHistory";
2023-10-27 15:26:23 +00:00
import { formatTimeAgo } from "@/utils/timeUtils";
import {
CategoryScale,
Chart as ChartJS,
Legend,
LineElement,
LinearScale,
PointElement,
Title,
Tooltip,
} from "chart.js";
import { Line } from "react-chartjs-2";
ChartJS.register(
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend,
);
type PlayerChartProps = {
className?: string;
2023-10-27 17:33:07 +00:00
historyData: ScoresaberMetricsHistory;
2023-10-27 15:26:23 +00:00
};
export const options: any = {
maintainAspectRatio: false,
aspectRatio: 1,
interaction: {
mode: "index",
intersect: false,
},
scales: {
y: {
ticks: {
autoSkip: true,
maxTicksLimit: 8,
stepSize: 1,
},
},
x: {
ticks: {
autoSkip: true,
},
},
},
elements: {
point: {
radius: 0,
},
},
plugins: {
legend: {
position: "top" as const,
labels: {
color: "white",
},
},
title: {
display: false,
},
},
};
export default function AnalyticsChart({
className,
2023-10-27 17:33:07 +00:00
historyData,
2023-10-27 15:26:23 +00:00
}: PlayerChartProps) {
2023-10-27 17:33:07 +00:00
const playerCountHistory = historyData.activePlayersHistory;
const scoreCountHistory = historyData.scoreCountHistory;
2023-10-27 15:26:23 +00:00
let labels = [];
for (let i = 0; i < playerCountHistory.length; i++) {
if (i == playerCountHistory.length - 1) {
2023-10-27 17:33:07 +00:00
labels.push("today");
2023-10-27 15:26:23 +00:00
continue;
}
labels.push(formatTimeAgo(playerCountHistory[i].time));
}
const data = {
labels,
datasets: [
{
lineTension: 0.5,
2023-10-27 15:28:29 +00:00
data: playerCountHistory.map((count) => count.value || "0"),
2023-10-27 15:26:23 +00:00
label: "Active Players",
borderColor: "#3e95cd",
fill: false,
color: "#fff",
},
2023-10-27 17:33:07 +00:00
{
lineTension: 0.5,
data: scoreCountHistory.map((count) => count.value || "0"),
label: "Scores Set",
borderColor: "#8e5ea2",
fill: false,
color: "#fff",
},
2023-10-27 15:26:23 +00:00
],
};
return <Line className={className} options={options} data={data} />;
}