简体   繁体   English

从 react chartjs 2 添加偏移量时如何居中和固定饼图的大小?

[英]how to centre and fix size of a pie chart while offset is added from react chartjs 2?

I need to center the pie chart into a component/div and also reduce the size of the chart to fix the cutting out in the ends.我需要将饼图居中放置在一个组件/div 中,并减小图表的大小以修复末端的切口。

在此处输入图像描述

Its cuts-off at bottom and sides.它在底部和侧面被切断。

Code:代码:

export const data = {
  labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
  datasets: [
    {
      label: "# of Votes",
      data: [13,1],
      backgroundColor: [
        "rgba(235, 159, 12,0.7)",
        "rgba(235, 159, 12, 1)",
      ],
      borderColor: [
        "rgba(235, 159, 12, 0.7)",
        "rgba(235, 159, 12, 1)",
      ],
      borderWidth: 1,
      offset:20
    },
  ],
};

export const options = {
    responsive: true,
    maintainAspectRatio: true,
    plugins: {
      title: {
        display: false,
      },
      legend:{
          display:false
      },
    },
  };

function MiniChart() {
  return (
    <div className="flex flex-col w-[150px] lg:w-[200px]">
      <p className="text-center text-md lg:text-lg">Unicorn Startups</p>
      <div className="bg-red-100 w-[150px] h-[150px] lg:w-[200px] lg:h-[200px]">
        <Pie data={data} options={options}/>
      </div>
    </div>
  );
}

you can reduce the size of the chart without affecting its ratio by setting the responsive to false.您可以通过将响应设置为 false 来减小图表的大小而不影响其比率。 so change所以改变
responsive: true to responsive: false responsive: true responsive: false

This was a bug.这是一个错误。

Has been fixed in Chart.Js ^4.0.0 .已在 Chart.Js ^4.0.0中修复。

Here's the solved Issue on Github .这是Github 上已解决的问题。

You can check the fix from the website, in the Pie example by acting on the dataset config values, adding some offset.您可以从网站上检查修复,在 Pie 示例中,通过对数据集配置值进行操作,添加一些偏移量。

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

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