|
| 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 | +// zoom |
| 57 | +const dragColor = Utils.randomColor(0.4); |
| 58 | +const zoomOptions = { |
| 59 | + pan: { |
| 60 | + enabled: true, |
| 61 | + mode: 'xy', |
| 62 | + modifierKey: 'ctrl', |
| 63 | + }, |
| 64 | + zoom: { |
| 65 | + mode: 'xy', |
| 66 | + drag: { |
| 67 | + enabled: true, |
| 68 | + borderColor: 'rgb(54, 162, 235)', |
| 69 | + borderWidth: 1, |
| 70 | + backgroundColor: 'rgba(54, 162, 235, 0.3)' |
| 71 | + } |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +// chart |
| 76 | +/* playground-fold */ |
| 77 | +const zoomStatus = () => zoomOptions.zoom.drag.enabled ? 'enabled' : 'disabled'; |
| 78 | + |
| 79 | +const ctx = document.querySelector('canvas'); |
| 80 | +const chart = new Chart(ctx, { |
| 81 | + type: 'scatter', |
| 82 | + data: data, |
| 83 | + options: { |
| 84 | + scales: scales, |
| 85 | + plugins: { |
| 86 | + zoom: zoomOptions, |
| 87 | + title: { |
| 88 | + display: true, |
| 89 | + position: 'bottom', |
| 90 | + text: (ctx) => 'Zoom: ' + zoomStatus() |
| 91 | + } |
| 92 | + }, |
| 93 | + } |
| 94 | +}); |
| 95 | +/* playground-fold-end */ |
| 96 | + |
| 97 | +// buttons |
| 98 | +/* playground-fold */ |
| 99 | +createButton('Toggle zoom', () => { |
| 100 | + zoomOptions.zoom.drag.enabled = !zoomOptions.zoom.drag.enabled; |
| 101 | + chart.update(); |
| 102 | +}); |
| 103 | +createButton('Reset zoom', () => chart.resetZoom()); |
| 104 | + |
| 105 | +function createButton(label, handler) { |
| 106 | + const btn = document.createElement('button'); |
| 107 | + btn.textContent = label; |
| 108 | + btn.addEventListener('click', handler); |
| 109 | + document.body.append(btn); |
| 110 | +} |
| 111 | +/* playground-fold-end */ |
0 commit comments