|
| 1 | +/* playground-hide */ |
| 2 | +import Chart from '../scripts/register.js'; |
| 3 | +import * as Utils from '../scripts/utils.js'; |
| 4 | +/* playground-hide-end */ |
| 5 | +// data |
| 6 | +/* playground-fold */ |
| 7 | +const NUMBER_CFG = {count: 20, min: -100, max: 100}; |
| 8 | +const data = { |
| 9 | + datasets: [{ |
| 10 | + label: 'My First dataset', |
| 11 | + borderColor: Utils.randomColor(0.4), |
| 12 | + backgroundColor: Utils.randomColor(0.1), |
| 13 | + pointBorderColor: Utils.randomColor(0.7), |
| 14 | + pointBackgroundColor: Utils.randomColor(0.5), |
| 15 | + pointBorderWidth: 1, |
| 16 | + data: Utils.points(NUMBER_CFG), |
| 17 | + }, { |
| 18 | + label: 'My Second dataset', |
| 19 | + borderColor: Utils.randomColor(0.4), |
| 20 | + backgroundColor: Utils.randomColor(0.1), |
| 21 | + pointBorderColor: Utils.randomColor(0.7), |
| 22 | + pointBackgroundColor: Utils.randomColor(0.5), |
| 23 | + pointBorderWidth: 1, |
| 24 | + data: Utils.points(NUMBER_CFG), |
| 25 | + }] |
| 26 | +}; |
| 27 | +/* playground-fold-end */ |
| 28 | + |
| 29 | +// scales |
| 30 | +/* playground-fold */ |
| 31 | +const scaleOpts = { |
| 32 | + reverse: true, |
| 33 | + ticks: { |
| 34 | + callback: (val, index, ticks) => index === 0 || index === ticks.length - 1 ? null : val, |
| 35 | + }, |
| 36 | + grid: { |
| 37 | + borderColor: Utils.randomColor(1), |
| 38 | + color: 'rgba( 0, 0, 0, 0.1)', |
| 39 | + }, |
| 40 | + title: { |
| 41 | + display: true, |
| 42 | + text: (ctx) => ctx.scale.axis + ' axis', |
| 43 | + } |
| 44 | +}; |
| 45 | +const scales = { |
| 46 | + x: { |
| 47 | + position: 'top', |
| 48 | + }, |
| 49 | + y: { |
| 50 | + position: 'right', |
| 51 | + }, |
| 52 | +}; |
| 53 | +Object.keys(scales).forEach(scale => Object.assign(scales[scale], scaleOpts)); |
| 54 | +/* playground-fold-end */ |
| 55 | + |
| 56 | +// chart |
| 57 | +/* playground-fold */ |
| 58 | +const ctx = document.querySelector('canvas'); |
| 59 | +const chart = new Chart(ctx, { |
| 60 | + type: 'scatter', |
| 61 | + data: data, |
| 62 | + options: { |
| 63 | + scales: scales, |
| 64 | + } |
| 65 | +}); |
| 66 | +/* playground-fold-end */ |
| 67 | + |
| 68 | +// buttons |
| 69 | +createButton('Zoom +10%', () => chart.zoom(1.1)); |
| 70 | +createButton('Zoom -10%', () => chart.zoom(0.9)); |
| 71 | +createButton('Zoom x +10%', () => chart.zoom({x: 1.1})); |
| 72 | +createButton('Zoom x -10%', () => chart.zoom({x: 0.9})); |
| 73 | +createButton('Pan x 100px (anim)', () => chart.pan({x: 100}, undefined, 'default')); |
| 74 | +createButton('Pan x -100px (anim)', () => chart.pan({x: -100}, undefined, 'default')); |
| 75 | +createButton('Zoom x: 0..-100, y: 0..100', () => { |
| 76 | + chart.zoomScale('x', {min: -100, max: 0}, 'default'); |
| 77 | + chart.zoomScale('y', {min: 0, max: 100}, 'default'); |
| 78 | +}); |
| 79 | +createButton('Reset zoom', () => chart.resetZoom()); |
| 80 | + |
| 81 | +function createButton(label, handler) { |
| 82 | + const btn = document.createElement('button'); |
| 83 | + btn.textContent = label; |
| 84 | + btn.addEventListener('click', handler); |
| 85 | + document.body.append(btn); |
| 86 | +} |
0 commit comments