-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph2.html
101 lines (96 loc) · 2.54 KB
/
graph2.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
<!DOCTYPE html>
<html>
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script> -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style type="text/css">
.chartBox{
width: 700px;
}
</style>
<body>
<div class="chartBox">
<canvas id="myChart"></canvas>
</div>
<script>
const data = [];
// const data2 = [];
let prev = 420;
// let prev2 = 80;
const min = 0;
const max = 10;
for (let i = 0; i < 100; i++) {
// prev += 5 - Math.random(100, 620) * 10;
prev += Math.floor(Math.random() * (max - min) + min);
// prev = Math.random(100, 650);
data.push({x: i, y: prev});
// prev2 += 5 - Math.random() * 10;
// data2.push({x: i, y: prev2});
}
const totalDuration = 1000;
// const delayBetweenPoints = totalDuration / data.length;
const delayBetweenPoints = 100;
const previousY = (ctx) => ctx.index === 0 ? ctx.chart.scales.y.getPixelForValue(100) : ctx.chart.getDatasetMeta(ctx.datasetIndex).data[ctx.index - 1].getProps(['y'], true).y;
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
borderColor: 'red',
borderWidth: 1,
radius: 0,
data: data,
}
// ,
// {
// borderColor: 'blue',
// borderWidth: 1,
// radius: 0,
// data: data2,
// }
]
},
options: {
animation: {
x: {
type: 'number',
easing: 'linear',
duration: delayBetweenPoints,
from: NaN, // the point is initially skipped
delay(ctx) {
if (ctx.type !== 'data' || ctx.xStarted) {
return 0;
}
ctx.xStarted = true;
return ctx.index * delayBetweenPoints;
}
},
y: {
type: 'number',
easing: 'linear',
duration: delayBetweenPoints,
from: previousY,
delay(ctx) {
if (ctx.type !== 'data' || ctx.yStarted) {
return 0;
}
ctx.yStarted = true;
return ctx.index * delayBetweenPoints;
}
}
},
interaction: {
intersect: false
},
plugins: {
legend: false
},
scales: {
x: {
type: 'linear'
}
}
}
});
</script>
</body>
</html>