From d6e5d0334c06811a37722012c64db5a1ce954f28 Mon Sep 17 00:00:00 2001 From: julius Date: Mon, 24 Feb 2025 17:53:01 +0100 Subject: [PATCH] feat: add RaceChart --- src/MVPChart.tsx | 29 +++++++++++++++++++ src/RaceChart.tsx | 71 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 src/MVPChart.tsx create mode 100644 src/RaceChart.tsx diff --git a/src/MVPChart.tsx b/src/MVPChart.tsx new file mode 100644 index 0000000..d87a635 --- /dev/null +++ b/src/MVPChart.tsx @@ -0,0 +1,29 @@ +import { useEffect, useState } from "react"; +import { apiAuth } from "./api"; +import BarChart from "./BarChart"; +import { PlayerRanking } from "./types"; +import RaceChart from "./RaceChart"; + + +const MVPChart = () => { + const [data, setData] = useState({} as PlayerRanking[]); + const [loading, setLoading] = useState(true); + const [showStd, setShowStd] = useState(false); + + async function loadData() { + setLoading(true); + await apiAuth("analysis/mvp", null) + .then(json => json as Promise).then(json => { setData(json.sort((a, b) => a.rank - b.rank)) }) + setLoading(false); + } + + useEffect(() => { loadData() }, []) + + return ( + <> + {loading ? : + } + ) +} + +export default MVPChart; diff --git a/src/RaceChart.tsx b/src/RaceChart.tsx new file mode 100644 index 0000000..c9ae49a --- /dev/null +++ b/src/RaceChart.tsx @@ -0,0 +1,71 @@ +import { FC, useEffect, useState } from 'react'; +import { PlayerRanking } from './types'; + +interface RaceChartProps { + players: PlayerRanking[]; + std: boolean; +} + +const determineNiceWidth = (width: number) => { + const max = 1080; + if (width >= max) + return max + else if (width > 768) + return width * 0.8 + else + return width * 0.96 +} + +const RaceChart: FC = ({ players, std }) => { + // State to store window's width and height + const [width, setWidth] = useState(determineNiceWidth(window.innerWidth)); + const [height, setHeight] = useState(window.innerHeight); + + // Update state on resize + useEffect(() => { + const handleResize = () => { + setWidth(determineNiceWidth(window.innerWidth)); + setHeight(window.innerHeight); + }; + window.addEventListener('resize', handleResize); + return () => { + window.removeEventListener('resize', handleResize); + }; + }, []); + const padding = 24; + const gap = 8 + const maxValue = Math.max(...players.map((player) => player.rank)) + 1; + const barHeight = (height - 2 * padding) / players.length; + + return ( + + + {players.map((player, index) => ( + ))} + + {players.map((player, index) => ( + {player.name} + ))} + + + ) +} +export default RaceChart;