-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
91 lines (82 loc) · 2.71 KB
/
code.js
File metadata and controls
91 lines (82 loc) · 2.71 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
function loadJSON(data, chart_id, config, callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', data, true);
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
// Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
callback(config, chart_id, xobj.responseText);
}
};
xobj.send(null);
}
function myCallback(config, chart_id, response) {
// Parse JSON string into object
var jsonfile = JSON.parse(response);
// Map labels and values from json to form the chart expects.
var datamodel = config.data_model;
// Get values
var datasets = [];
for (let i = 0; i < datamodel["values"].length; i++) {
var dataset = {};
var name = datamodel.values[i];
// this is the format chart wants
dataset.label = name;
dataset.data = jsonfile.map(function (e) {return e[name];});
dataset.backgroundColor = datamodel.colors[i];
datasets.push(dataset);
}
var labels = jsonfile.map(function (e) {return e[datamodel["label"]];});
var ctx = document.getElementById(chart_id);
var config = {
type: 'line',
data: {
labels: labels,
datasets: datasets
},
options: {
responsive: true,
spanGaps: true,
title: {
display: true,
text: config.chart_title
},
scales: {
xAxes: [{
type: 'time',
display: true,
time: {
unit: time_interval,
displayFormats: display_formats
},
scaleLabel: {
display: true,
labelString: x_label
},
ticks: {
major: {
fontStyle: 'bold',
fontColor: '#FFFF00'
}
}
}],
yAxes: [{
display: true,
stacked: stacked,
scaleLabel: {
display: true,
labelString: y_label
}
}]
}
}
};
var chart = new Chart(ctx, config);
}
for (var key in configx) {
let chart_id = key;
let data_file = configx[key]["data_file"];
let config = configx[chart_id];
console.log(data_file)
loadJSON(data_file, chart_id, config, myCallback);
}