InvestWeb/components/userpage/RankChart.tsx

84 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-01-28 23:14:42 -05:00
import RankHistoryJson from "../../interfaces/ChartRankHistoryJSON";
import { Line } from "react-chartjs-2";
import {
Chart,
ChartData,
ChartOptions,
CategoryScale,
LinearScale,
PointElement,
LineElement,
Tooltip,
} from "chart.js";
interface RankChartProps {
rankHistory: RankHistoryJson;
}
Chart.register(CategoryScale, LinearScale, PointElement, LineElement, Tooltip);
function RankChart(props: RankChartProps) {
2023-01-30 01:55:49 -05:00
let delayed: boolean;
2023-01-28 23:14:42 -05:00
const options: ChartOptions<"line"> = {
2023-01-30 01:55:49 -05:00
animation: {
onComplete: () => {
delayed = true;
},
delay: (context) => {
let delay = 0;
if (context.type === "data" && context.mode === "default" && !delayed) {
delay = context.dataIndex * 9;
}
return delay;
},
},
2023-01-28 23:14:42 -05:00
plugins: {
tooltip: {
mode: "index",
intersect: false,
displayColors: false,
callbacks: {
label: (context) => {
const daysAgo = context.dataset.data.length - context.dataIndex - 1;
if (daysAgo === 0) {
return `Today`;
}
return `${daysAgo} days ago`;
},
},
},
},
scales: {
x: {
display: false,
},
y: {
display: false,
reverse: true,
},
},
responsive: true,
maintainAspectRatio: false,
};
const data: ChartData<"line"> = {
// make labels size dynamic
labels: props.rankHistory.rank.map((rank, i) => {
return "Rank " + rank;
}),
datasets: [
{
label: "Rank",
data: props.rankHistory.rank,
fill: false,
borderColor: "rgb(244, 114, 182)",
2023-01-29 01:40:07 -05:00
pointBackgroundColor: "rgba(0, 0, 0, 0)",
pointBorderColor: "rgba(0, 0, 0, 0)",
2023-01-28 23:14:42 -05:00
tension: 0,
},
],
};
return <Line options={options} data={data} />;
}
export default RankChart;