Issue
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.
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}>
?
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.
<Doughnut
data={data}
width={"30%"}
options={{ maintainAspectRatio: false }}
/>
Answered By - technogeek1995
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.