9👍
Have a detailed look at https://www.chartjs.org/docs/latest/configuration/responsive.html. The Important Note section mentions how responsiveness can be achieved. It states that
Chart.js uses its parent container to update the canvas render and display sizes. However, this method requires the container to be relatively positioned and dedicated to the chart canvas only. Responsiveness can then be achieved by setting relative values for the container size.
In your case, the parent container for the line chart is the <div>
element. The render
function of LineDemo
component in LineDemo.js
file then can be modified to look like:
render() {
return (
<div style={{ position: "relative", margin: "auto", width: "80vw" }}>
<h2>Line Example</h2>
<Line ref="chart" data={data} />
</div>
);
}
Here is a working example: https://codesandbox.io/s/react-chartjs-2-example-hzygw
7👍
What worked for me was to add chart container a width style:
<div style={{width: '99%'}}>
<Bar options={options} data={data} />
</div>
However if I set width 100% it won’t work lol