Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions website/astro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export default defineConfig({
'faq/maintain-aspect-ratio',
'faq/chartjs-instance',
'faq/canvas-context',
'faq/plugins',
'faq/why-two',
'faq/fill-property',
'faq/registered-element',
Expand Down
1 change: 1 addition & 0 deletions website/src/content/docs/faq/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Answers to the questions that come up most often.
- [Why doesn't the chart maintain its width/height?](/faq/maintain-aspect-ratio/)
- [How to access the Chart.js instance?](/faq/chartjs-instance/)
- [How to access the canvas context?](/faq/canvas-context/)
- [Why doesn't the chart use new plugins?](/faq/plugins/)
- [Why does react-chartjs-2 have "2" in its name?](/faq/why-two/)
- [Why is a background fill not working?](/faq/fill-property/)
- [How to fix "... is not a registered element" error?](/faq/registered-element/)
Expand Down
27 changes: 22 additions & 5 deletions website/src/content/docs/faq/maintain-aspect-ratio.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
---
title: "Why doesn't the chart maintain its width/height?"
description: "Make a react-chartjs-2 chart keep a custom width and height by setting the maintainAspectRatio option to false."
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."
prev: FAQ
---

In order for Chart.js to obey the custom size you need to set `maintainAspectRatio` to `false`:
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.

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:

```tsx
<div style={{ position: 'relative', height: '300px' }}>
<Bar
data={data}
options={{ maintainAspectRatio: false }}
/>
</div>
```

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.

For a fixed size that does not follow the page, turn `responsive` off, and the `width` and `height` props set the size:

```tsx
<Bar
data={data}
width={100}
height={50}
options={{ maintainAspectRatio: false }}
width={400}
height={200}
options={{ responsive: false }}
/>
```

See [Responsive Charts](https://www.chartjs.org/docs/latest/configuration/responsive.html) in the Chart.js docs for details.
48 changes: 48 additions & 0 deletions website/src/content/docs/faq/plugins.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: "Why doesn't the chart use new plugins?"
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."
---

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.

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:

```jsx
const centerText = {
id: 'centerText',
afterDraw(chart, args, options) {
const { ctx, chartArea } = chart

ctx.save()
ctx.textAlign = 'center'
ctx.fillText(
options.text,
(chartArea.left + chartArea.right) / 2,
(chartArea.top + chartArea.bottom) / 2
)
ctx.restore()
}
}

function Votes({ total }) {
return (
<Doughnut
data={data}
plugins={[centerText]}
options={{ plugins: { centerText: { text: `${total} votes` } } }}
/>
)
}
```

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:

```jsx
<Bar
key={showLabels ? 'labels' : 'plain'}
data={data}
plugins={showLabels ? [ChartDataLabels] : []}
/>
```

The `redraw` prop recreates the chart as well, but every time `data` or `options` change, not only when the plugins do.