Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize Chart.JS element in React.js?

I have a React component Webstats which returns a Doughnut chart from react-chartjs-2 library. Here is how the code looks like:

const Webstats = () => {
  //code to create chart.
  return <Doughnut data={pd} />;
}

I use this component inside another component called Dashboard. I want to display the chart alongside a Logout button. Both should be in the same row. I use flex property of css for this purpose.

const rowC = {
  display: "flex",
  flexDirection: "row"
};

const Dashboard = () => {
  return (
    <div style={rowC}>
      <Webstats />
      <Link to="/">
        <div>
          <button onClick={auth.logout}>Logout</button>
        </div>
      </Link>
    </div>
  );
};

export default Dashboard;

But the problem is that, chart size is too big as compared to logout button.

Screenshot

I want to make chart size small, around 30% of the width of <div style={rowC}>. I tried these things:

return <Doughnut data={pd} width="30%"/>;

and

return (
    <div style={rowC}>
      <Webstats width="30%" />
      // rest of html
)

and also

return (
    <div width="30%">
      <Doughnut data={pd} />
    </div>
  );

But none of this gives me the desired result. How to I resize the chart to 30% of the width (and height auto adjusted) of the <div style={rowC}>?

like image 706
Tarun Khare Avatar asked Dec 13 '19 15:12

Tarun Khare


People also ask

How do I resize a chart in react JS?

Solution. The documentation for ChartJS states that you need to set maintainAspectRatio to false for it to use the width and height props that you pass into your chart. In order for Chart. js to obey the custom size you need to set maintainAspectRatio to false.

How do I change the size of my ChartJS?

To set the chart size in ChartJS, we recommend using the responsive option, which makes the Chart fill its container. You must wrap the chart canvas tag in a div in order for responsive to take effect. You cannot set the canvas element size directly with responsive .

Is ChartJS responsive?

Luckily, Chart. js does most of this complex work for us! A Chart. js chart is responsive by default, but with one limitation: it maintains its aspect ratio.


3 Answers

The documentation for ChartJS states that you need to set maintainAspectRatio to false for it to use the width and height props that you pass into your chart.

In order for Chart.js to obey the custom size you need to set maintainAspectRatio to false.

<Doughnut
  data={data}
  width={"30%"}
  options={{ maintainAspectRatio: false }}
/>
like image 63
technogeek1995 Avatar answered Oct 19 '22 16:10

technogeek1995


Wrap it in div Container and set width and height.

Ex:

<div style={{height:"60vh",position:"relative", marginBottom:"1%", padding:"1%"}}>
     <Doughnut options={options} data={chartData} />
</div>
like image 41
Smack Alpha Avatar answered Oct 19 '22 16:10

Smack Alpha


Doing the following works for me:

  1. set height and width on the <Doughnut /> component.
  2. Set the maintainAspectRation to false.

Here is what the outcome looks like:

<Doughnut
  data={data}
  height="200px"
  width="200px"
  options={{ maintainAspectRatio: false }}
/>
like image 32
Abdulsalam Avatar answered Oct 19 '22 17:10

Abdulsalam