-
Notifications
You must be signed in to change notification settings - Fork 12
/
v3anims.js
154 lines (148 loc) · 3.22 KB
/
v3anims.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
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
153
import Chart from 'chart.js/auto';
import {red, yellow, blue, green} from '../colours.js';
const count = 100;
const randomDataSet = function() {
var dataset = [];
let prev = Math.random() * 100;
for (let i = 0; i < count; i++) {
prev += 5 - Math.random() * 10;
dataset.push({x: i, y: prev});
}
return dataset;
};
const totalDuration = 5000;
const delayBetweenPoints = totalDuration / count;
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 datasets = [{
borderColor: red,
data: randomDataSet(),
animations: {
x: {
type: 'number',
easing: 'linear',
duration: delayBetweenPoints,
from: NaN, // the point is initially skipped
delay(ctx) {
if (ctx.type !== 'data') {
return 0;
}
return ctx.index * delayBetweenPoints;
}
},
y: {
type: 'number',
easing: 'linear',
duration: delayBetweenPoints,
from: previousY,
delay(ctx) {
if (ctx.type !== 'data') {
return 0;
}
return ctx.index * delayBetweenPoints;
}
}
}
}, {
borderColor: blue,
data: randomDataSet(),
animations: {
borderWidth: {
easing: 'linear',
from: 1,
loop: true
}
}
}, {
borderColor: yellow,
borderWidth: 1,
data: randomDataSet(),
tension: 1,
animations: {
tension: {
type: 'number',
duration: 1000,
easing: 'linear',
from: 0,
to: 1,
loop: true
}
}
}, {
borderColor: green,
data: randomDataSet(),
animations: {
x: {
type: 'number',
easing: 'linear',
duration: delayBetweenPoints,
from: NaN, // the point is initially skipped
delay(ctx) {
if (ctx.type !== 'data') {
return 0;
}
return (100 - ctx.index) * delayBetweenPoints;
}
},
y: {
type: 'number',
easing: 'linear',
duration: delayBetweenPoints,
from: previousY,
delay(ctx) {
if (ctx.type !== 'data') {
return 0;
}
return (100 - ctx.index) * delayBetweenPoints;
}
}
}
}];
export default function(ctx) {
const chart = new Chart(ctx, {
type: 'line',
data: {
datasets: datasets
},
options: {
radius: 0,
animations: {
y: {
delay: (context) => 1000 * context.datasetIndex,
easing: 'easeInOutElastic',
from: (context) => {
if (context.type === 'data') {
if (context.mode === 'default' && !context.dropped) {
context.dropped = true;
return -10;
}
}
}
}
},
interaction: {
mode: 'x',
intersect: false
},
plugins: {
legend: false
},
scales: {
x: {
type: 'linear',
display: false,
bounds: 'data'
},
y: {
type: 'linear',
display: false,
}
}
}
});
setInterval(() => {
for (const dataset of chart.data.datasets) {
dataset.data = randomDataSet();
}
chart.update();
}, 10000);
}