Skip to content

Commit b7f8865

Browse files
authored
docs: explain the size and the plugins of a chart in the FAQ (#1379)
* docs: size a chart with a container of its own The FAQ suggested `maintainAspectRatio: false` with the width and height props, which a responsive chart does not keep, and said nothing about the container. A chart that shares its parent element with other content grows without end, as in #1168. * docs: explain why a chart does not use new plugins Chart.js takes the plugins of a chart when it creates the chart. The new FAQ page shows how to pass changing values through the options of a plugin, and how to switch plugins with a key.
1 parent 094fa63 commit b7f8865

4 files changed

Lines changed: 72 additions & 5 deletions

File tree

‎website/astro.config.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ export default defineConfig({
183183
'faq/maintain-aspect-ratio',
184184
'faq/chartjs-instance',
185185
'faq/canvas-context',
186+
'faq/plugins',
186187
'faq/why-two',
187188
'faq/fill-property',
188189
'faq/registered-element',

‎website/src/content/docs/faq/index.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Answers to the questions that come up most often.
99
- [Why doesn't the chart maintain its width/height?](/faq/maintain-aspect-ratio/)
1010
- [How to access the Chart.js instance?](/faq/chartjs-instance/)
1111
- [How to access the canvas context?](/faq/canvas-context/)
12+
- [Why doesn't the chart use new plugins?](/faq/plugins/)
1213
- [Why does react-chartjs-2 have "2" in its name?](/faq/why-two/)
1314
- [Why is a background fill not working?](/faq/fill-property/)
1415
- [How to fix "... is not a registered element" error?](/faq/registered-element/)
Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
---
22
title: "Why doesn't the chart maintain its width/height?"
3-
description: "Make a react-chartjs-2 chart keep a custom width and height by setting the maintainAspectRatio option to false."
3+
description: "Size a react-chartjs-2 chart with a container of its own and maintainAspectRatio set to false, or give it a fixed size with responsive set to false."
44
prev: FAQ
55
---
66

7-
In order for Chart.js to obey the custom size you need to set `maintainAspectRatio` to `false`:
7+
A chart is responsive by default: Chart.js takes its size from the parent element and keeps the aspect ratio, so the `width` and `height` props only set the size the canvas starts with.
8+
9+
To give the chart a size of your own, set `maintainAspectRatio` to `false` and put the chart in a container of its own, with `position: relative` and the size you need:
10+
11+
```tsx
12+
<div style={{ position: 'relative', height: '300px' }}>
13+
<Bar
14+
data={data}
15+
options={{ maintainAspectRatio: false }}
16+
/>
17+
</div>
18+
```
19+
20+
The chart takes the height of this container, so the container needs a height of its own and must hold nothing but the chart. When the chart shares its parent element with other content, such as a button next to it, the parent grows with the chart, and the chart grows without end.
21+
22+
For a fixed size that does not follow the page, turn `responsive` off, and the `width` and `height` props set the size:
823

924
```tsx
1025
<Bar
1126
data={data}
12-
width={100}
13-
height={50}
14-
options={{ maintainAspectRatio: false }}
27+
width={400}
28+
height={200}
29+
options={{ responsive: false }}
1530
/>
1631
```
32+
33+
See [Responsive Charts](https://www.chartjs.org/docs/latest/configuration/responsive.html) in the Chart.js docs for details.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
title: "Why doesn't the chart use new plugins?"
3+
description: "react-chartjs-2 gives the plugins to Chart.js when it creates the chart: pass changing values through the options of a plugin, or remount the chart with a key to switch plugins."
4+
---
5+
6+
Chart.js takes the plugins of a chart when it creates the chart, so react-chartjs-2 passes the `plugins` prop only then. A new `plugins` array does not change a chart that already exists.
7+
8+
Values that a plugin uses can change through its options: put them in `options.plugins` under the id of the plugin, and the plugin gets them as the third argument of its hooks. Options are updated without recreating the chart:
9+
10+
```jsx
11+
const centerText = {
12+
id: 'centerText',
13+
afterDraw(chart, args, options) {
14+
const { ctx, chartArea } = chart
15+
16+
ctx.save()
17+
ctx.textAlign = 'center'
18+
ctx.fillText(
19+
options.text,
20+
(chartArea.left + chartArea.right) / 2,
21+
(chartArea.top + chartArea.bottom) / 2
22+
)
23+
ctx.restore()
24+
}
25+
}
26+
27+
function Votes({ total }) {
28+
return (
29+
<Doughnut
30+
data={data}
31+
plugins={[centerText]}
32+
options={{ plugins: { centerText: { text: `${total} votes` } } }}
33+
/>
34+
)
35+
}
36+
```
37+
38+
To turn plugins on or off, give the chart a `key` that changes with them. React then mounts a new chart, and Chart.js creates it with the new plugins:
39+
40+
```jsx
41+
<Bar
42+
key={showLabels ? 'labels' : 'plain'}
43+
data={data}
44+
plugins={showLabels ? [ChartDataLabels] : []}
45+
/>
46+
```
47+
48+
The `redraw` prop recreates the chart as well, but every time `data` or `options` change, not only when the plugins do.

0 commit comments

Comments
 (0)