ChartJS React图表中的响应图例字体大小js 2

slhcrj9b  于 2022-11-06  发布在  Chart.js
关注(0)|答案(2)|浏览(255)

我曾尝试使用react-chartjs 2制作一个饼图,它在桌面视图中工作正常,但在移动的视图中图例没有响应,它占用了很多空间,因此饼图的大小变得非常小。
我代码:

  1. function Chart(props: any) {
  2. const option = {
  3. tooltips: {enter image description here
  4. callbacks: {
  5. label: function (tooltipItem: any, data: any) {
  6. var dataset = data.datasets[tooltipItem.datasetIndex];
  7. var meta = dataset._meta[Object.keys(dataset._meta)[0]];
  8. var total = meta.total;
  9. var currentValue = dataset.data[tooltipItem.index];
  10. var percentage = parseFloat(
  11. ((currentValue / total) * 100).toFixed(1)
  12. );
  13. return currentValue + " (" + percentage + "%)";
  14. },
  15. title: function (tooltipItem: any, data: any) {
  16. return data.labels[tooltipItem[0].index];
  17. },
  18. },
  19. },
  20. legend: {
  21. display: true,
  22. labels: {
  23. fontSize: 12,
  24. },
  25. position: "right",
  26. },
  27. };
  28. return (
  29. <div className="chart">
  30. <Pie data={props.ChartData} options={option} />
  31. </div>
  32. );
vdgimpew

vdgimpew1#

您可以将fontSize对象设置为三元运算符,该运算符检查widts(或其他内容)以查看您是否使用移动的设备,并根据它给予正确的fontSize
如果由于屏幕大小更改而需要实时更新,可以通过在resizeEvent列表器中修改图表选项本身来实现
第一个

cbeh67ev

cbeh67ev2#

以下是具有响应图例字体大小的方法(也说明了here

  1. options: {
  2. plugins: {
  3. legend: {
  4. labels: {
  5. // This more specific font property overrides the global property
  6. font: {
  7. size: 14
  8. }
  9. }
  10. }
  11. }
  12. }

相关问题