forked from BadApple9/speedtest-x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chart.html
141 lines (127 loc) · 3.86 KB
/
chart.html
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!doctype html>
<html>
<head>
<title>ISP Speed Chart</title>
<script src="chartjs/Chart.min.js"></script>
<script src="chartjs/utils.js"></script>
<style>
canvas {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
</style>
</head>
<body>
<div style="width:75%">
<canvas id="canvas"></canvas>
</div>
<script>
var color = Chart.helpers.color;
async function mainfunc() {
var color = Chart.helpers.color;
console.log(color)
var datareq = await fetch('backend/results-api.php');
console.log(datareq)
var dataraw = await datareq.text();
console.log(dataraw)
try {
dataraw = JSON.parse(dataraw);
} catch {
alert("Can't get data");
return 0;
}
data_sorted = {};
for (const data of dataraw["data"]) {
let data_isp = data["isp"].split(" ").length > 1 ? data["isp"].split(" ")[0] + " " + data["isp"].split(" ")[1] : data["isp"];
if (data_isp in data_sorted) {
data_sorted[data_isp] = data_sorted[data_isp].concat([data])
} else {
data_sorted[data_isp] = [data]
}
}
let datasets = []
for (const isp of Object.keys(data_sorted)) {
let ispdata_chart = {
label: isp,
data: []
}
for (const ispdata of data_sorted[isp]) {
let [t_h, t_m, t_s] = ispdata["created"].split(" ")[1].split(":");
ispdata_chart["data"] = ispdata_chart["data"].concat([{
x: t_h * 1.0 + t_m / 60 + t_s / 3600,
y: ispdata["dspeed"],
label: ispdata
}]);
}
datasets = datasets.concat([ispdata_chart])
}
datasets = datasets.sort((a, b) => a["data"].length < b["data"].length ? 1 : -1);
for (const [i, data] of datasets.entries()) {
if (i === 0) {
datasets[i]["borderColor"] = window.chartColors["red"];
datasets[i]["backgroundColor"] = color(datasets[i]["borderColor"]).alpha(0.9).rgbString()
} else if (i === 1) {
datasets[i]["borderColor"] = "rgb(12, 176, 42)";
datasets[i]["backgroundColor"] = color(datasets[i]["borderColor"]).alpha(0.9).rgbString()
} else if (i === 2) {
datasets[i]["borderColor"] = "rgb(2, 104, 219)";
datasets[i]["backgroundColor"] = color(datasets[i]["borderColor"]).alpha(0.9).rgbString()
}
else{
datasets[i]["borderColor"] = window.chartColors["gray"];;
datasets[i]["backgroundColor"] = color(window.chartColors["gray"]).alpha(0.1).rgbString()
}
}
console.log(datasets);
scatterChartData = {
datasets: datasets
}
var ctx = document.getElementById('canvas').getContext('2d');
window.myScatter = Chart.Scatter(ctx, {
data: scatterChartData,
options: {
title: {
display: true,
text: 'ISP Speed Chart'
},
scales: {
yAxes: [{
type: 'logarithmic',
position: 'bottom',
ticks: {
userCallback: function(tick) {
var remain = tick / (Math.pow(10, Math.floor(Chart.helpers.log10(tick))));
if (remain === 1 || remain === 2 || remain === 5) {
return tick.toString() + 'Mbps';
}
return '';
},
},
scaleLabel: {
labelString: 'Speed',
display: true,
}
}],
xAxes: [{
type: 'linear',
ticks: {
userCallback: function(tick) {
return tick.toString() + ':00';
}
},
scaleLabel: {
labelString: 'Time',
display: true
}
}]
}
}
});
}
window.onload = function() {
mainfunc();
};
</script>
</body>
</html>