-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchart.js
94 lines (86 loc) · 2.41 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
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
function makeTable(table) {
let labels = [];
let series = [];
let rows = Array.from(table.querySelectorAll(":scope tbody tr"));
let minY = 90;
let maxY = 100;
rows = rows.reverse();
for(let row of rows) {
let label = row.children[0].innerText.split(" ");
labels.push(label.slice(0,2).join(" "));
let childCount = row.children.length - 1;
let seriesIndex = 0;
for(let j = 0, k = childCount; j<k; j++) {
let data = row.children[j + 1].dataset;
if(data && data.numericValue) {
minY = Math.min(data.numericValue, minY);
maxY = Math.max(data.numericValue, maxY);
if(!series[seriesIndex]) {
series[seriesIndex] = [];
}
series[seriesIndex].push(data.numericValue);
seriesIndex++;
}
}
}
let options = {
high: Math.max(maxY, 100),
low: Math.max(0, minY - 5),
fullWidth: true,
onlyInteger: true,
showPoint: false,
lineSmooth: true,
axisX: {
showGrid: true,
showLabel: true
},
chartPadding: {
right: 40
}
};
new Chartist.Line(table.parentNode.nextElementSibling, {
labels: labels,
series: series
}, options);
}
function initializeAllTables(scope) {
let tables = scope.querySelectorAll("[data-make-chart]");
for(let table of tables) {
// make sure not in a closed details
if(table.closest("details[open]") || !table.closest("details")) {
makeTable(table);
}
}
}
initializeAllTables(document);
let details = document.querySelectorAll("details");
// let first = true;
for(let detail of details) {
// open the first details by default
// if(first) {
// detail.open = true;
// first = false;
// }
detail.addEventListener("toggle", function(e) {
let open = e.target.hasAttribute("open");
if(open) {
initializeAllTables(e.target);
}
let row = e.target.closest(".leaderboard-list-entry-details");
row.classList.toggle("expanded", open);
row.previousElementSibling.classList.toggle("expanded", open);
});
}
let expandAliases = document.querySelectorAll("[data-expand-alias]");
for(let alias of expandAliases) {
alias.addEventListener("click", function(e) {
e.preventDefault();
let href = e.target.closest("a[href]").getAttribute("href");
if(href) {
let details = document.querySelector(href);
if(details) {
details.open = !details.hasAttribute("open");
}
}
}, false);
}