Skip to content

Commit b7faa2f

Browse files
authored
Add fetch-data example (#558)
1 parent ee4aebc commit b7faa2f

File tree

2 files changed

+150
-1
lines changed

2 files changed

+150
-1
lines changed

docs/.vuepress/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = {
2323
{
2424
entryPoints: ['../../types/index.d.ts'],
2525
hideInPageTOC: true,
26-
tsconfig: '../../tsconfig.json',
26+
tsconfig: 'tsconfig.json',
2727
sidebar: {
2828
fullNames: true,
2929
parentCategory: 'API',
@@ -100,6 +100,7 @@ module.exports = {
100100
]
101101
},
102102
'api',
103+
'fetch-data',
103104
'pan-region',
104105
],
105106
}

docs/samples/fetch-data.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Fetch data
2+
3+
```js chart-editor
4+
// <block:data:2>
5+
const start = new Date().valueOf();
6+
const end = start + 1000 * 60 * 60 * 24 * 2;
7+
const allData = [];
8+
let y = 100;
9+
for (let x = start; x <= end; x += 1000) {
10+
y += 5 - Math.random() * 10;
11+
allData.push({x, y});
12+
}
13+
14+
function fetchData(x1, x2) {
15+
const step = Math.max(1, Math.round((x2 - x1) / 100000));
16+
const data = [];
17+
let i = 0;
18+
while (i < allData.length && allData[i].x < x1) {
19+
i++;
20+
}
21+
while (i < allData.length && allData[i].x <= x2) {
22+
data.push(allData[i]);
23+
i += step;
24+
}
25+
return data;
26+
}
27+
// </block:data>
28+
29+
// <block:fetch:1>
30+
let timer;
31+
function startFetch({chart}) {
32+
const {min, max} = chart.scales.x;
33+
clearTimeout(timer);
34+
timer = setTimeout(() => {
35+
console.log('Fetched data between ' + min + ' and ' + max);
36+
chart.data.datasets[0].data = fetchData(min, max);
37+
chart.stop(); // make sure animations are not running
38+
chart.update('none');
39+
}, 500);
40+
}
41+
// </block:fetch>
42+
43+
// <block:scales:3>
44+
const scales = {
45+
x: {
46+
position: 'bottom',
47+
min: start,
48+
max: end,
49+
type: 'time',
50+
ticks: {
51+
autoSkip: true,
52+
autoSkipPadding: 50,
53+
maxRotation: 0
54+
},
55+
time: {
56+
displayFormats: {
57+
hour: 'HH:mm',
58+
minute: 'HH:mm',
59+
second: 'HH:mm:ss'
60+
}
61+
}
62+
},
63+
y: {
64+
type: 'linear',
65+
position: 'left',
66+
},
67+
};
68+
// </block:scales>
69+
70+
// <block:zoom:0>
71+
const zoomOptions = {
72+
limits: {
73+
x: {min: 'original', max: 'original', minRange: 60 * 1000},
74+
},
75+
pan: {
76+
enabled: true,
77+
mode: 'x',
78+
modifierKey: 'ctrl',
79+
onPanComplete: startFetch
80+
},
81+
zoom: {
82+
wheel: {
83+
enabled: true,
84+
},
85+
drag: {
86+
enabled: true,
87+
},
88+
pinch: {
89+
enabled: true
90+
},
91+
mode: 'x',
92+
onZoomComplete: startFetch
93+
}
94+
};
95+
// </block:zoom>
96+
97+
const zoomStatus = (chart) => 'zoom level: ' + chart.getZoomLevel() + '';
98+
99+
// <block:config:1>
100+
const config = {
101+
type: 'line',
102+
data: {
103+
datasets: [{
104+
label: 'My First dataset',
105+
borderColor: Utils.randomColor(0.4),
106+
backgroundColor: Utils.randomColor(0.1),
107+
pointBorderColor: Utils.randomColor(0.7),
108+
pointBackgroundColor: Utils.randomColor(0.5),
109+
pointBorderWidth: 1,
110+
data: fetchData(start, end),
111+
}]
112+
},
113+
options: {
114+
scales: scales,
115+
plugins: {
116+
zoom: zoomOptions,
117+
title: {
118+
display: true,
119+
position: 'bottom',
120+
text: (ctx) => zoomStatus(ctx.chart)
121+
}
122+
},
123+
transitions: {
124+
zoom: {
125+
animation: {
126+
duration: 100
127+
}
128+
}
129+
}
130+
}
131+
};
132+
// </block:config>
133+
134+
const actions = [
135+
{
136+
name: 'Reset zoom',
137+
handler(chart) {
138+
chart.resetZoom('zoom');
139+
}
140+
}
141+
];
142+
143+
module.exports = {
144+
actions,
145+
config,
146+
output: 'console.log output'
147+
};
148+
```

0 commit comments

Comments
 (0)