forked from boa-dev/boa-dev.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
125 lines (111 loc) · 2.98 KB
/
index.tsx
File metadata and controls
125 lines (111 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { useColorMode } from "@docusaurus/theme-common";
import Heading from "@theme/Heading";
import {
BarElement,
CategoryScale,
Chart as ChartJS,
Colors,
Legend,
LineElement,
LinearScale,
PointElement,
Title,
Tooltip,
} from "chart.js";
import { useEffect, useState } from "react";
import { Bar, Line } from "react-chartjs-2";
import styles from "./styles.module.css";
ChartJS.register(
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend,
Colors,
BarElement,
);
interface BenchmarkGraphsProps {
selectedEngines: string[];
range: string;
}
const ChartOptions = {
plugins: {
colors: {
forceOverride: true,
},
},
};
export const BenchmarkGraphs: React.FC<BenchmarkGraphsProps> = ({
selectedEngines,
range,
}) => {
// Control the state of which engines are displayed using a Set
const [charts, setCharts] = useState([]);
const [data, setData] = useState({});
const colorMode = useColorMode();
ChartJS.defaults.color = colorMode.colorMode === "light" ? "#666" : "white";
useEffect(() => {
fetch(
`https://boa-api.jason-williams.co.uk/benchmarks?months=${range}&engines=${selectedEngines.join(",")}`,
)
.then((res) => res.json())
.then((respData) => {
setData(respData);
});
}, [selectedEngines, range]);
useEffect(() => {
setCharts(buildChartFromBenchmark(data));
}, [data]);
return charts && charts.map((chart) => chart);
};
const normalizeBenchmarkData = (benchmarkData: any[]) => {
const labels = benchmarkData.map((entry: any) =>
new Date(entry.date).toLocaleDateString(),
);
const engines = Object.keys(benchmarkData[0]).filter((key) => key != "date");
return {
labels,
datasets: engines.map((engine) => ({
label: engine,
data: benchmarkData.map((entry) => entry[engine]),
fill: false,
})),
};
};
const getBarChartData = (data) => {
// We only want the last value from each dataset
return {
labels: [data.labels[data.labels.length - 1]],
datasets: data.datasets.map((dataset) => {
return {
label: dataset.label,
data: [dataset.data[dataset.data.length - 1]],
};
}),
};
};
const buildChartFromBenchmark = (data: any): any[] => {
let charts = [];
for (const benchmark in data) {
const normalizedData = normalizeBenchmarkData(data[benchmark]);
const barData = getBarChartData(normalizedData);
charts.push(
<div key={benchmark}>
<div className={`card__header ${styles["benchmark-card-header"]}`}>
<Heading as="h2">{benchmark}</Heading>
</div>
<div className={styles["cards-wrap"]}>
<div className={`card ${styles["benchmark-card"]}`}>
<Line data={normalizedData} options={ChartOptions}></Line>
</div>
<div className={`card ${styles["benchmark-card"]}`}>
<Bar data={barData} options={ChartOptions}></Bar>
</div>
</div>
</div>,
);
}
return charts;
};