-
Notifications
You must be signed in to change notification settings - Fork 0
/
chart.js
67 lines (53 loc) · 1.89 KB
/
chart.js
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
const ctx1 = document.getElementById("chart-p1");
const ctx2 = document.getElementById("chart-p2");
(async function () {
const response = await fetch("/adventofcode/members");
if (!response.ok) return;
const members = await response.json();
if (members.length === 0) {
return;
}
const labels = [];
const datasetsPartOne = [];
const datasetsPartTwo = [];
const datasetConfig = {
pointStyle: "circle",
pointRadius: 5,
pointHoverRadius: 10,
};
for (const member of members) {
const name = member.name;
const completion_day_level = member.completion_day_level;
Object.keys(completion_day_level)
.filter((day) => !labels.includes(day))
.forEach((day) => labels.push(day));
const partOne = new Array(25);
const partTwo = new Array(25);
for (const [day, stars] of Object.entries(completion_day_level)) {
const dayNumber = parseInt(day);
const dayDate = new Date(2023, 11, dayNumber).getTime() / 1000 / 60;
const partOneDate = parseInt(stars["1"]?.get_star_ts) / 60;
const partTwoDate = parseInt(stars["2"]?.get_star_ts) / 60;
partOne[dayNumber - 1] = partOneDate - dayDate;
partTwo[dayNumber - 1] = partTwoDate - partOneDate;
}
datasetsPartOne.push({ label: name, data: partOne, ...datasetConfig });
datasetsPartTwo.push({ label: name, data: partTwo, ...datasetConfig });
}
const dataPartOne = { labels, datasets: datasetsPartOne };
const dataPartTwo = { labels, datasets: datasetsPartTwo };
const mkConfig = (data, title) => ({
type: "line",
data: data,
options: {
plugins: {
legend: { position: "top" },
title: { display: true, text: title },
tooltip: true,
},
scales: { y: { display: true, type: "logarithmic" } },
},
});
new Chart(ctx1, mkConfig(dataPartOne, "Part One"));
new Chart(ctx2, mkConfig(dataPartTwo, "Part Two"));
})();