|
| 1 | +# Pan Toggle |
| 2 | + |
| 3 | +In this example pan is initially disabled. |
| 4 | + |
| 5 | +```js chart-editor |
| 6 | +// <block:data:1> |
| 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 | +// </block:data> |
| 28 | + |
| 29 | +// <block:scales:2> |
| 30 | +const scaleOpts = { |
| 31 | + grid: { |
| 32 | + borderColor: Utils.randomColor(1), |
| 33 | + color: 'rgba( 0, 0, 0, 0.1)', |
| 34 | + }, |
| 35 | + title: { |
| 36 | + display: true, |
| 37 | + text: (ctx) => ctx.scale.axis + ' axis', |
| 38 | + } |
| 39 | +}; |
| 40 | +const scales = { |
| 41 | + x: { |
| 42 | + position: 'top', |
| 43 | + }, |
| 44 | + y: { |
| 45 | + position: 'right', |
| 46 | + }, |
| 47 | +}; |
| 48 | +Object.keys(scales).forEach(scale => Object.assign(scales[scale], scaleOpts)); |
| 49 | +// </block:scales> |
| 50 | + |
| 51 | +// <block:zoom:0> |
| 52 | +const zoomOptions = { |
| 53 | + limits: { |
| 54 | + x: {min: -200, max: 200, minRange: 50}, |
| 55 | + y: {min: -200, max: 200, minRange: 50} |
| 56 | + }, |
| 57 | + pan: { |
| 58 | + enabled: false, |
| 59 | + mode: 'xy', |
| 60 | + }, |
| 61 | + zoom: { |
| 62 | + wheel: { |
| 63 | + enabled: false, |
| 64 | + }, |
| 65 | + pinch: { |
| 66 | + enabled: true |
| 67 | + }, |
| 68 | + } |
| 69 | +}; |
| 70 | +// </block:zoom> |
| 71 | + |
| 72 | +// <block:config:1> |
| 73 | +const config = { |
| 74 | + type: 'scatter', |
| 75 | + data: data, |
| 76 | + options: { |
| 77 | + scales: scales, |
| 78 | + plugins: { |
| 79 | + zoom: zoomOptions, |
| 80 | + }, |
| 81 | + }, |
| 82 | +}; |
| 83 | +// </block:config> |
| 84 | + |
| 85 | +const actions = [ |
| 86 | + { |
| 87 | + name: 'Toggle pan', |
| 88 | + handler(chart) { |
| 89 | + chart.options.plugins.zoom.pan.enabled = !chart.options.plugins.zoom.pan.enabled; |
| 90 | + chart.update(); |
| 91 | + } |
| 92 | + }, |
| 93 | + { |
| 94 | + name: 'Reset zoom', |
| 95 | + handler(chart) { |
| 96 | + chart.resetZoom('zoom'); |
| 97 | + } |
| 98 | + } |
| 99 | +]; |
| 100 | + |
| 101 | +module.exports = { |
| 102 | + actions, |
| 103 | + config, |
| 104 | +}; |
| 105 | +``` |
0 commit comments