Skip to content

Commit e37553f

Browse files
committed
docs(plugin.legend): add legend navigation sample
1 parent 454c6a6 commit e37553f

File tree

2 files changed

+131
-6
lines changed

2 files changed

+131
-6
lines changed

docs/.vuepress/config.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ export default defineConfig({
3434
}],
3535
['vuepress-plugin-code-copy', true],
3636
['vuepress-plugin-typedoc', {
37-
entryPoints: ['../../src/types/index.d.ts'],
38-
hideInPageTOC: true,
39-
tsconfig: path.resolve(__dirname, '../../tsconfig.json'),
40-
},
37+
entryPoints: ['../../src/types/index.d.ts'],
38+
hideInPageTOC: true,
39+
tsconfig: path.resolve(__dirname, '../../tsconfig.json'),
40+
},
4141
],
4242
['@simonbrunel/vuepress-plugin-versions', {
4343
filters: {
4444
suffix: (tag) => tag ? ` (${tag})` : '',
4545
title: (v, vars) => {
4646
return window.location.href.includes('master') ? 'Development (master)' :
47-
vars.tag === 'latest' ? 'Latest (' + v + ')' :
48-
v + (vars.tag ? ` (${vars.tag})` : '') + ' (outdated)';
47+
vars.tag === 'latest' ? 'Latest (' + v + ')' :
48+
v + (vars.tag ? ` (${vars.tag})` : '') + ' (outdated)';
4949
},
5050
},
5151
menu: {
@@ -219,6 +219,7 @@ export default defineConfig({
219219
'legend/point-style',
220220
'legend/position',
221221
'legend/title',
222+
'legend/navigation'
222223
]
223224
},
224225
{

docs/samples/legend/navigation.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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

Comments
 (0)