diff --git a/website/astro.config.js b/website/astro.config.js index 1a5ff23ab..d7f2f0d08 100644 --- a/website/astro.config.js +++ b/website/astro.config.js @@ -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', diff --git a/website/src/content/docs/faq/index.md b/website/src/content/docs/faq/index.md index dbb4685cf..7128e5603 100644 --- a/website/src/content/docs/faq/index.md +++ b/website/src/content/docs/faq/index.md @@ -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/) diff --git a/website/src/content/docs/faq/maintain-aspect-ratio.md b/website/src/content/docs/faq/maintain-aspect-ratio.md index 18b91d05e..8500339df 100644 --- a/website/src/content/docs/faq/maintain-aspect-ratio.md +++ b/website/src/content/docs/faq/maintain-aspect-ratio.md @@ -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 +
+ +
+``` + +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 ``` + +See [Responsive Charts](https://www.chartjs.org/docs/latest/configuration/responsive.html) in the Chart.js docs for details. diff --git a/website/src/content/docs/faq/plugins.md b/website/src/content/docs/faq/plugins.md new file mode 100644 index 000000000..c33488f5e --- /dev/null +++ b/website/src/content/docs/faq/plugins.md @@ -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 ( + + ) +} +``` + +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 + +``` + +The `redraw` prop recreates the chart as well, but every time `data` or `options` change, not only when the plugins do.