|
| 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