|
| 1 | +# Selecting annotations |
| 2 | + |
| 3 | +```js chart-editor |
| 4 | +// <block:setup:3> |
| 5 | +let count = 0; |
| 6 | +const selected = []; |
| 7 | + |
| 8 | +Utils.srand(8); |
| 9 | + |
| 10 | +const data = { |
| 11 | + labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'], |
| 12 | + datasets: [{ |
| 13 | + type: 'line', |
| 14 | + label: 'Dataset 1', |
| 15 | + borderColor: 'rgb(54, 162, 235)', |
| 16 | + borderWidth: 2, |
| 17 | + fill: false, |
| 18 | + data: Utils.numbers({count: 7, min: 0, max: 100}), |
| 19 | + }] |
| 20 | +}; |
| 21 | +// </block:setup> |
| 22 | + |
| 23 | +// <block:annotation1:1> |
| 24 | +const annotation1 = { |
| 25 | + type: 'box', |
| 26 | + backgroundColor: 'rgba(255, 245, 157, 0.2)', |
| 27 | + borderColor: 'rgb(255, 245, 157)', |
| 28 | + borderWidth: 2, |
| 29 | + click: ({element}) => select(element, 'rgba(255, 245, 157, 0.8)', 'rgba(255, 245, 157, 0.2)'), |
| 30 | + label: { |
| 31 | + display: true, |
| 32 | + content: 'Yellow box annotation', |
| 33 | + position: { |
| 34 | + y: 'start' |
| 35 | + }, |
| 36 | + font: { |
| 37 | + size: 12 |
| 38 | + } |
| 39 | + }, |
| 40 | + xMax: 'April', |
| 41 | + xMin: 'February', |
| 42 | + xScaleID: 'x', |
| 43 | + yMax: 90, |
| 44 | + yMin: 10, |
| 45 | + yScaleID: 'y' |
| 46 | +}; |
| 47 | +// </block:annotation1> |
| 48 | + |
| 49 | +// <block:annotation2:2> |
| 50 | +const annotation2 = { |
| 51 | + type: 'box', |
| 52 | + backgroundColor: 'rgba(165, 214, 167, 0.2)', |
| 53 | + borderColor: 'rgb(165, 214, 167)', |
| 54 | + borderWidth: 2, |
| 55 | + click: ({element}) => select(element, 'rgba(165, 214, 167, 0.8)', 'rgba(165, 214, 167, 0.2)'), |
| 56 | + label: { |
| 57 | + display: true, |
| 58 | + content: 'Green box annotation', |
| 59 | + position: { |
| 60 | + y: 'start' |
| 61 | + }, |
| 62 | + font: { |
| 63 | + size: 12 |
| 64 | + } |
| 65 | + }, |
| 66 | + xMax: 'May', |
| 67 | + xMin: 'March', |
| 68 | + xScaleID: 'x', |
| 69 | + yMax: 75, |
| 70 | + yMin: 25, |
| 71 | + yScaleID: 'y' |
| 72 | +}; |
| 73 | +// </block:annotation2> |
| 74 | + |
| 75 | +// <block:utils:4> |
| 76 | +function enter({chart, element}) { |
| 77 | + console.log(element.label.options.content + ' entered'); |
| 78 | + if (!count) { |
| 79 | + chart.canvas.style.cursor = 'pointer'; |
| 80 | + } |
| 81 | + count++; |
| 82 | +} |
| 83 | + |
| 84 | +function leave({chart, element}) { |
| 85 | + console.log(element.label.options.content + ' left'); |
| 86 | + count--; |
| 87 | + if (!count) { |
| 88 | + chart.canvas.style.cursor = 'default'; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +function select(element, selectedColor, unselectedColor) { |
| 93 | + console.log(element.label.options.content + ' selected'); |
| 94 | + if (selected.includes(element)) { |
| 95 | + selected.splice(selected.indexOf(element), 1); |
| 96 | + element.options.backgroundColor = unselectedColor; |
| 97 | + element.label.options.font.size = 12; |
| 98 | + } else { |
| 99 | + selected.push(element); |
| 100 | + element.options.backgroundColor = selectedColor; |
| 101 | + element.label.options.font.size = 14; |
| 102 | + } |
| 103 | + return true; |
| 104 | +} |
| 105 | +// </block:utils> |
| 106 | + |
| 107 | +/* <block:config:0> */ |
| 108 | +const config = { |
| 109 | + type: 'line', |
| 110 | + data, |
| 111 | + options: { |
| 112 | + scales: { |
| 113 | + y: { |
| 114 | + beginAtZero: true, |
| 115 | + min: 0, |
| 116 | + max: 100 |
| 117 | + } |
| 118 | + }, |
| 119 | + plugins: { |
| 120 | + annotation: { |
| 121 | + enter: enter, |
| 122 | + leave: leave, |
| 123 | + common: { |
| 124 | + drawTime: 'beforeDraw' |
| 125 | + }, |
| 126 | + annotations: { |
| 127 | + annotation1, |
| 128 | + annotation2 |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +}; |
| 134 | +/* </block:config> */ |
| 135 | + |
| 136 | +module.exports = { |
| 137 | + config: config, |
| 138 | + output: 'console.log output is shown here, click one of the annotations' |
| 139 | +}; |
| 140 | +``` |
0 commit comments