繁体   English   中英

ChartJS 调整饼图图例

[英]ChartJS adjusting Pie Chart Legend

我想调整我的 ChartJS/React-chartjs-2 饼图以显示均匀堆叠在饼图一侧的图例。 我试图操纵 options.legend.position 但它似乎没有改变。 下面是代码片段和我的饼图的屏幕截图。 我正在使用 chart.js 3.9.0 / react-chartjs-2 4.3.1 / react 18.2.0

谢谢!

在此处输入图像描述

 const options = { title:{ display: true, text: "Current Allocations" }, legend: { position: "right", }, responsive: true, animation: { animateRotate: false, animateScale: true, }, }; const data = { labels: [ "Cash / Cash Equivalents - $" + cashTotal.toLocaleString() + " - " + cashPercent.toFixed(0) + "%", "Fixed Income - $" + fixedIncomeTotal.toLocaleString() + " - " + fixedIncomePecent.toFixed(0) + "%", "Real Estate - $" + realEstateTotal.toLocaleString() + " - " + realEstatePercent.toFixed(0) + "%", "Private Equities - $" + privateEquityTotal.toLocaleString() + " - " + privateEquityPercent.toFixed(0) + "%", "Commodities / Futures - $" + commoditiesAndFuturesTotal.toLocaleString() + " - " + commoditiesAndFuturesPercent.toFixed(0) + "%", "Blockchain and Crypto - $" + blockChainTotal.toLocaleString() + " - " + blockChainPercent.toFixed(0) + "%", "Equities - $" + equitiesTotal.toLocaleString() + " - " + equitiesPercent.toFixed(0) + "%", "Oil and Gas / Energy - $" + oilAndGasTotal.toLocaleString() + " - " + oilAndGasPercent.toFixed(0) + "%", "Longevity - $" + longevityTotal.toLocaleString() + " - " + longevityPercent.toFixed(0) + "%", "Life Insurance - $" + lifeInsuranceTotal.toLocaleString() + " - " + lifeInsurancePercent.toFixed(0) + "%", ], datasets: [ { data: [ cashTotal, fixedIncomeTotal, realEstateTotal, privateEquityTotal, commoditiesAndFuturesTotal, blockChainTotal, equitiesTotal, oilAndGasTotal, longevityTotal, lifeInsuranceTotal, ], backgroundColor: [ "rgba(121, 224, 162, 0.8)", "rgba(93, 217, 99, 0.8)", "rgba(174, 209, 69, 0.8)", "rgba(201, 134, 46, 0.8)", "rgba(194, 25, 70, 0.8)", "rgba(95, 8, 105, 0.8)", "rgba(18, 10, 102, 0.8)", "rgba(12, 61, 92, 0.8)", "rgba(18, 91, 115, 0.8)", "rgba(88, 177, 187, 0.8)", ], }, ], }; return ( <div style={{ width: "500px" }}> <Pie data={data} options={options} /> </div> );

我可以通过将 legend.position 从 options 移动到 options.plugins 来解决这个问题

 const options = { responsive: true, animation: { animateRotate: false, animateScale: true, }, plugins: { legend: { position: "right", }, title:{ display: true, text: "Current Allocations", }, } };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM