|
| 1 | +# Intro |
| 2 | + |
| 3 | +```js chart-editor |
| 4 | +// <block:setup:4> |
| 5 | +Utils.srand(8); |
| 6 | + |
| 7 | +const data = { |
| 8 | + labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'], |
| 9 | + datasets: [{ |
| 10 | + type: 'line', |
| 11 | + label: 'Dataset 1', |
| 12 | + borderColor: 'rgb(54, 162, 235)', |
| 13 | + borderWidth: 2, |
| 14 | + fill: false, |
| 15 | + data: Utils.numbers({count: 7, min: -100, max: 100}), |
| 16 | + }, { |
| 17 | + type: 'bar', |
| 18 | + label: 'Dataset 2', |
| 19 | + backgroundColor: 'rgb(255, 99, 132)', |
| 20 | + data: Utils.numbers({count: 7, min: -100, max: 100}), |
| 21 | + borderColor: 'white', |
| 22 | + borderWidth: 2 |
| 23 | + }, { |
| 24 | + type: 'bar', |
| 25 | + label: 'Dataset 3', |
| 26 | + backgroundColor: 'rgb(75, 192, 192)', |
| 27 | + data: Utils.numbers({count: 7, min: -100, max: 100}), |
| 28 | + }] |
| 29 | +}; |
| 30 | +// </block:setup> |
| 31 | + |
| 32 | +// <block:annotation1:1> |
| 33 | +const annotation1 = { |
| 34 | + type: 'line', |
| 35 | + scaleID: 'y', |
| 36 | + value: Utils.rand(-100, 100), |
| 37 | + borderColor: 'black', |
| 38 | + borderWidth: 5, |
| 39 | + label: { |
| 40 | + backgroundColor: 'red', |
| 41 | + content: 'Test Label', |
| 42 | + enabled: true |
| 43 | + }, |
| 44 | + click: function({chart, element}) { |
| 45 | + console.log('Line annotation clicked'); |
| 46 | + } |
| 47 | +}; |
| 48 | +// </block:annotation1> |
| 49 | + |
| 50 | +// <block:annotation2:2> |
| 51 | +const annotation2 = { |
| 52 | + drawTime: 'beforeDatasetsDraw', |
| 53 | + type: 'box', |
| 54 | + xScaleID: 'x', |
| 55 | + yScaleID: 'y', |
| 56 | + xMin: 'February', |
| 57 | + xMax: 'April', |
| 58 | + yMin: Utils.rand(-100, 100), |
| 59 | + yMax: Utils.rand(-100, 100), |
| 60 | + backgroundColor: 'rgba(101, 33, 171, 0.5)', |
| 61 | + borderColor: 'rgb(101, 33, 171)', |
| 62 | + borderWidth: 1, |
| 63 | + click: function({chart, element}) { |
| 64 | + console.log('Box annotation clicked'); |
| 65 | + } |
| 66 | +}; |
| 67 | +// </block:annotation2> |
| 68 | + |
| 69 | +/* <block:config:0> */ |
| 70 | +const config = { |
| 71 | + type: 'line', |
| 72 | + data, |
| 73 | + options: { |
| 74 | + plugins: { |
| 75 | + annotation: { |
| 76 | + annotations: { |
| 77 | + annotation1, |
| 78 | + annotation2 |
| 79 | + } |
| 80 | + } |
| 81 | + }, |
| 82 | + } |
| 83 | +}; |
| 84 | +/* </block:config> */ |
| 85 | + |
| 86 | +var actions = [ |
| 87 | + { |
| 88 | + name: 'Randomize', |
| 89 | + handler: function(chart) { |
| 90 | + chart.data.datasets.forEach(function(dataset, i) { |
| 91 | + dataset.data = Utils.numbers({count: 7, min: -100, max: 100}); |
| 92 | + }); |
| 93 | + |
| 94 | + chart.update(); |
| 95 | + } |
| 96 | + }, |
| 97 | +]; |
| 98 | + |
| 99 | +module.exports = { |
| 100 | + actions: actions, |
| 101 | + config: config, |
| 102 | + output: 'console.log output is shown here, click one of the annotations' |
| 103 | +}; |
| 104 | +``` |
0 commit comments