forked from gbtami/pychess-variants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.ts
79 lines (71 loc) · 2.71 KB
/
stats.ts
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
import Highcharts from 'highcharts';
import { h, VNode } from 'snabbdom';
function createPeriods() {
const periodList: string[] = [];
const date = new Date(2019, 6, 1, 0, 0, 0); // (2019-07-01) the month is 0-indexed
const endDate = new Date();
const months = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"];
while (date <= endDate) {
const year = date.getFullYear().toString();
const month = months[date.getMonth()];
periodList.push(year + '-' + month);
date.setMonth(date.getMonth() + 1);
}
return periodList;
}
function buildChart() {
const axisTypeEl = document.getElementById("linear") as HTMLInputElement;
const humanGamesEl = document.getElementById("humans") as HTMLInputElement;
const xmlhttp = new XMLHttpRequest();
const url = humanGamesEl.checked ? "/api/stats/humans" : "/api/stats";
xmlhttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
const response = JSON.parse(this.responseText);
if (!response.length) {
return;
}
Highcharts.chart('stats-chart', {
chart: { type: 'line' },
credits: { enabled: false },
title: { text: undefined },
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
},
xAxis: {
categories: createPeriods(),
},
yAxis: {
type: axisTypeEl.checked ? 'linear' : 'logarithmic',
},
responsive: {
rules: [{
condition: {
maxWidth: 1200
},
chartOptions: {
legend: {
align: 'center',
verticalAlign: 'bottom',
layout: 'horizontal'
}
}
}]
},
series: response
});
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
export function statsView(): VNode[] {
return [
h('div#stats-chart', { hook: { insert: () => buildChart() } }),
h('input#linear', { props: { type: 'checkbox' }, on: { change: () => buildChart() } }),
h('label', { attrs: { for: 'linear' } }, 'Linear scale'),
h('input#humans', { props: { type: 'checkbox' }, on: { change: () => buildChart() } }),
h('label', { attrs: { for: 'humans' } }, 'Human games'),
];
}