-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-chart.js
112 lines (112 loc) · 2.74 KB
/
run-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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var A; // for viewing data in devtools
var chart;
function runChart(dataUrl, chartTitle) {
Chart.defaults.global.defaultColor = '#f8f8f8';
Chart.defaults.global.defaultFontColor = '#f8f8f8';
const nusCanvas = document.querySelector('#chart').getContext('2d');
fetch('https://cors-anywhere.herokuapp.com/' + dataUrl)
.then(res => res.text())
.then(csv => {
let data = {};
let lines = csv.split('\r\n');
let headers = lines[0].split(',');
lines.shift();
let rands = [];
let datasets = [];
for (const [i, header] of headers.entries()) {
console.log(i, header);
data[header] = lines.map(l => l.split(',')[i]);
let rand;
if (header != 'time') {
do {
rand = Math.random();
console.log(i, rand);
} while (!rands.every(e => Math.abs(e - rand) > (0.5 / (headers.length - 1))) && rands.length > 0);
// change this value for distinctness of colours ^ vs time (min = 0, max = 1, infinite loop)
rands.push(rand);
datasets.push({
label: header,
backgroundColor: `hsl(${360 * rand}, 100%, 95%)`,
borderColor: `hsl(${360 * rand}, 100%, 70%)`,
data: data[header].map((e, i) => ({x: moment(data.time[i]), y: parseFloat(e)})),
fill: false
});
}
}
console.log(datasets);
A = data;
chart = new Chart(nusCanvas, {
type: 'line',
data: {
labels: data.Time,
datasets: datasets
},
options: {
responsive: true,
maintainAspectRatio: false,
title: {
display: true,
text: chartTitle
},
legend: {
display: true,
fullWidth: true,
position: 'bottom',
labels: {
boxWidth: 10
}
},
scales: {
xAxes: [
{
type: 'time',
time: {
format: 'YYYY-MM-DDTHH:mm:ss.SSSSZ',
tooltipFormat: 'll HH:mm',
min: moment().subtract(1, 'M') // show data for past month
},
scaleLabel: {
display: true,
labelString: 'Date'
},
ticks: {
maxRotation: 0
},
gridLines: {
color: '#f8f8f8'
}
}
],
yAxes: [
{
id: 'score',
type: 'linear',
display: true,
scaleLabel: {
display: true,
labelString: 'Score'
},
gridLines: {
color: '#f8f8f8'
}
}
]
},
pan: {
enabled: true,
mode: 'xy',
speed: 10,
threshold: 10
},
zoom: {
enabled: true,
mode: 'xy',
limits: {
max: 10,
min: 5
}
}
}
})
});
}