|
| 1 | +# Navigation |
| 2 | + |
| 3 | +This sample shows how to use legend navigation to handle overflow. |
| 4 | + |
| 5 | +```js chart-editor |
| 6 | + |
| 7 | +// <block:setup:1> |
| 8 | +const DATA_COUNT = 100; |
| 9 | +const NUMBER_CFG = {count: DATA_COUNT, decimals: 0}; |
| 10 | +const LABEL_CFG = {count: DATA_COUNT, prefix: 'Group ', min: 1, max: DATA_COUNT + 1}; |
| 11 | + |
| 12 | +const data = { |
| 13 | + labels: Utils.labels(LABEL_CFG), |
| 14 | + datasets: [{ |
| 15 | + label: '# of Votes', |
| 16 | + data: Utils.numbers(NUMBER_CFG), |
| 17 | + backgroundColor: Object.values(Utils.CHART_COLORS), |
| 18 | + }] |
| 19 | +}; |
| 20 | +// </block:setup> |
| 21 | + |
| 22 | +// <block:config:0> |
| 23 | +const config = { |
| 24 | + type: 'pie', |
| 25 | + data: data, |
| 26 | + options: { |
| 27 | + plugins: { |
| 28 | + legend: { |
| 29 | + position: 'right', |
| 30 | + align: 'start', |
| 31 | + title: { |
| 32 | + display: true, |
| 33 | + text: 'Chart.js Legend Navigation Example', |
| 34 | + position: 'start', |
| 35 | + }, |
| 36 | + navigation: { |
| 37 | + display: 'auto', |
| 38 | + maxCols: 1, |
| 39 | + maxRows: 3, |
| 40 | + arrowSize: 12, |
| 41 | + align: 'start', |
| 42 | + grid: true |
| 43 | + } |
| 44 | + }, |
| 45 | + } |
| 46 | + } |
| 47 | +}; |
| 48 | +// </block:config> |
| 49 | + |
| 50 | +// <block:actions:2> |
| 51 | +const actions = [ |
| 52 | + { |
| 53 | + name: 'Toggle', |
| 54 | + handler(chart) { |
| 55 | + const {navigation} = chart.options.plugins.legend; |
| 56 | + navigation.display = navigation.display ? false : 'auto'; |
| 57 | + chart.update(); |
| 58 | + } |
| 59 | + }, |
| 60 | + { |
| 61 | + name: '+ Label', |
| 62 | + handler(chart) { |
| 63 | + console.log(chart); |
| 64 | + const lastLabel = chart.data.labels[chart.data.labels.length - 1] || ''; |
| 65 | + const lastIndex = +lastLabel.substring(6); |
| 66 | + chart.data.labels.push(LABEL_CFG.prefix + (lastIndex + 1)); |
| 67 | + chart.update(); |
| 68 | + } |
| 69 | + }, |
| 70 | + { |
| 71 | + name: '- Label', |
| 72 | + handler(chart) { |
| 73 | + chart.data.labels.pop(); |
| 74 | + chart.update(); |
| 75 | + } |
| 76 | + }, |
| 77 | + { |
| 78 | + name: 'Position: left', |
| 79 | + handler(chart) { |
| 80 | + chart.options.plugins.legend.position = 'left'; |
| 81 | + chart.update(); |
| 82 | + } |
| 83 | + }, |
| 84 | + { |
| 85 | + name: 'Position: top', |
| 86 | + handler(chart) { |
| 87 | + chart.options.plugins.legend.position = 'top'; |
| 88 | + chart.update(); |
| 89 | + } |
| 90 | + }, |
| 91 | + { |
| 92 | + name: 'Position: right', |
| 93 | + handler(chart) { |
| 94 | + chart.options.plugins.legend.position = 'right'; |
| 95 | + chart.update(); |
| 96 | + } |
| 97 | + }, |
| 98 | + { |
| 99 | + name: 'Position: bottom', |
| 100 | + handler(chart) { |
| 101 | + chart.options.plugins.legend.position = 'bottom'; |
| 102 | + chart.update(); |
| 103 | + } |
| 104 | + }, |
| 105 | + { |
| 106 | + name: 'Toggle grid', |
| 107 | + handler(chart) { |
| 108 | + chart.options.plugins.legend.navigation.grid = !chart.options.plugins.legend.navigation.grid; |
| 109 | + chart.update(); |
| 110 | + } |
| 111 | + }, |
| 112 | +]; |
| 113 | +// </block:actions> |
| 114 | + |
| 115 | +module.exports = { |
| 116 | + config, |
| 117 | + actions |
| 118 | +}; |
| 119 | +``` |
| 120 | + |
| 121 | +## Docs |
| 122 | +* [Doughnut and Pie Charts](../../charts/doughnut.md) |
| 123 | +* [Legend](../../configuration/legend.md) |
| 124 | + * [Navigation](../../configuration/legend.md#legend-navigation-configuration) |
0 commit comments