[英]How to resize Chart.JS element in React.js?
我有一個 React 組件Webstats
,它從react-chartjs-2庫返回一個圓環圖。 下面是代碼的樣子:
const Webstats = () => {
//code to create chart.
return <Doughnut data={pd} />;
}
我在另一個名為Dashboard
的組件中使用這個組件。 我想在注銷按鈕旁邊顯示圖表。 兩者應該在同一行。 為此,我使用 css 的flex
屬性。
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;
但問題是,與注銷按鈕相比,圖表尺寸太大了。
我想讓圖表尺寸變小,約為<div style={rowC}>
寬度的 30%。 我試過這些東西:
return <Doughnut data={pd} width="30%"/>;
和
return (
<div style={rowC}>
<Webstats width="30%" />
// rest of html
)
並且
return (
<div width="30%">
<Doughnut data={pd} />
</div>
);
但是這些都沒有給我想要的結果。 如何將圖表調整為<div style={rowC}>
寬度(和高度自動調整)的 30%?
將其包裹在div
容器中並設置寬度和高度。
前任:
<div style={{height:"60vh",position:"relative", marginBottom:"1%", padding:"1%"}}>
<Doughnut options={options} data={chartData} />
</div>
為我做以下工作:
<Doughnut />
組件上設置高度和寬度。結果如下:
<Doughnut
data={data}
height="200px"
width="200px"
options={{ maintainAspectRatio: false }}
/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.