|
| 1 | +# Reject outside chartArea |
| 2 | + |
| 3 | +Zooming is performed by clicking and selecting an area over the chart with the mouse. Pan is activated by keeping `ctrl` pressed. |
| 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 | + reverse: true, |
| 32 | + ticks: { |
| 33 | + callback: (val, index, ticks) => index === 0 || index === ticks.length - 1 ? null : val, |
| 34 | + }, |
| 35 | + grid: { |
| 36 | + borderColor: Utils.randomColor(1), |
| 37 | + color: 'rgba( 0, 0, 0, 0.1)', |
| 38 | + }, |
| 39 | + title: { |
| 40 | + display: true, |
| 41 | + text: (ctx) => ctx.scale.axis + ' axis', |
| 42 | + } |
| 43 | +}; |
| 44 | +const scales = { |
| 45 | + x: { |
| 46 | + position: 'top', |
| 47 | + }, |
| 48 | + y: { |
| 49 | + position: 'right', |
| 50 | + }, |
| 51 | +}; |
| 52 | +Object.keys(scales).forEach(scale => Object.assign(scales[scale], scaleOpts)); |
| 53 | +// </block:scales> |
| 54 | + |
| 55 | +// <block:zoom:0> |
| 56 | +const dragColor = Utils.randomColor(0.4); |
| 57 | +const zoomOptions = { |
| 58 | + pan: { |
| 59 | + enabled: true, |
| 60 | + mode: 'xy', |
| 61 | + modifierKey: 'ctrl', |
| 62 | + }, |
| 63 | + zoom: { |
| 64 | + mode: 'xy', |
| 65 | + // here is the magic! |
| 66 | + onZoomStart: ({chart, point}) => chart.isPointInArea(point), |
| 67 | + drag: { |
| 68 | + enabled: true, |
| 69 | + borderColor: 'rgb(54, 162, 235)', |
| 70 | + borderWidth: 1, |
| 71 | + backgroundColor: 'rgba(54, 162, 235, 0.3)' |
| 72 | + } |
| 73 | + } |
| 74 | +}; |
| 75 | +// </block:zoom> |
| 76 | + |
| 77 | +const zoomStatus = () => zoomOptions.zoom.drag.enabled ? 'enabled' : 'disabled'; |
| 78 | + |
| 79 | +// <block:config:1> |
| 80 | +const config = { |
| 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 | +// </block:config> |
| 96 | + |
| 97 | +const actions = [ |
| 98 | + { |
| 99 | + name: 'Toggle zoom', |
| 100 | + handler(chart) { |
| 101 | + zoomOptions.zoom.drag.enabled = !zoomOptions.zoom.drag.enabled; |
| 102 | + chart.update(); |
| 103 | + } |
| 104 | + }, { |
| 105 | + name: 'Reset zoom', |
| 106 | + handler(chart) { |
| 107 | + chart.resetZoom(); |
| 108 | + } |
| 109 | + } |
| 110 | +]; |
| 111 | + |
| 112 | +module.exports = { |
| 113 | + actions, |
| 114 | + config, |
| 115 | +}; |
| 116 | +``` |
0 commit comments