forked from leeoniya/uPlot
/
Chart.js.html
152 lines (134 loc) · 3.57 KB
/
Chart.js.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
141
142
143
144
145
146
147
148
149
150
151
152
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Chart.js Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css">
<style>
.chart-container {
position: relative;
margin: auto;
height: 600px;
width: 1920px;
}
</style>
</head>
<body>
<h2 id="wait">Loading lib....</h2>
<script src="https://cdn.jsdelivr.net/npm/luxon@1.15.0"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.2"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-luxon@0.2.0"></script>
<script src="https://www.chartjs.org/samples/latest/utils.js"></script>
<div class="chart-container">
<canvas id="chart1"></canvas>
</div>
<script>
function round2(val) {
return Math.round(val * 100) / 100;
}
function round3(val) {
return Math.round(val * 1000) / 1000;
}
function prepData(packed) {
console.time('prep');
// epoch,idl,recv,send,read,writ,used,free
const numFields = packed[0];
packed = packed.slice(numFields + 1);
var cpu = Array(packed.length/numFields);
var ram = Array(packed.length/numFields);
var tcp = Array(packed.length/numFields);
for (let i = 0, j = 0; i < packed.length; i += numFields, j++) {
let date = packed[i] * 60 * 1000;
cpu[j] = {t: date, y: round3(100 - packed[i+1])};
ram[j] = {t: date, y: round2(100 * packed[i+5] / (packed[i+5] + packed[i+6]))};
tcp[j] = {t: date, y: packed[i+3]};
}
console.timeEnd('prep');
return [cpu, ram, tcp];
}
function makeChart(data) {
console.time('chart');
var ctx = document.getElementById('chart1').getContext('2d');
Chart.defaults.global.animation = false;
var color = Chart.helpers.color;
var cfg = {
data: {
datasets: [
{
label: 'CPU',
borderColor: window.chartColors.red,
data: data[0],
type: 'line',
pointRadius: 0,
fill: false,
lineTension: 0,
borderWidth: 1
},
{
label: 'RAM',
borderColor: window.chartColors.blue,
data: data[1],
type: 'line',
pointRadius: 0,
fill: false,
lineTension: 0,
borderWidth: 1
},
{
label: 'TCP Out',
borderColor: window.chartColors.green,
data: data[2],
type: 'line',
pointRadius: 0,
fill: false,
lineTension: 0,
borderWidth: 1
}
]
},
options: {
maintainAspectRatio: false,
tooltips: {
mode: 'index',
intersect: false,
},
scales: {
xAxes: [{
type: 'time',
distribution: 'series',
ticks: {
source: 'auto',
maxRotation: 0,
autoSkip: true,
autoSkipPadding: 75,
sampleSize: 1
}
}],
yAxes: [{
scaleLabel: {
display: true,
labelString: '%'
}
}]
},
}
};
var chart = new Chart(ctx, cfg);
wait.textContent = "Done!";
console.timeEnd('chart');
}
let wait = document.getElementById("wait");
wait.textContent = "Fetching data.json (2.07MB)....";
fetch("data.json").then(r => r.json()).then(packed => {
wait.textContent = "Rendering...";
let data = prepData(packed);
setTimeout(() => makeChart(data), 0);
});
</script>
<p>
Code based on<br>
<a href="https://www.chartjs.org/samples/latest/scales/time/financial.html">https://www.chartjs.org/samples/latest/scales/time/financial.html</a><br>
</p>
</body>
</html>