简体   繁体   English

React recharts:饼图未呈现

[英]React recharts : Pie chart is not rendering

I am not able to render Pie charts, I cannot see that I am doing anything incorrectly (based on what I see in examples and documentation)我无法渲染饼图,我看不到我做错了什么(基于我在示例和文档中看到的内容)

Data and Colors:数据和 Colors:

const piedata = [
    { name: "Technology", value: props.data.tech },
    { name: "Services", value: props.data.services },
    {
      name: "Communication",
      value: props.data.communication,
    },
    { name: "Others", value: props.data.others },
  ];

  const PieColors = ["#bce893", "#44a7e1", "#556cfb", "#f4b040"];

props are in this form道具是这种形式

data: EquityAllocation(
communication: 6.1,
others: 2.7,
services: 7.5,
tech: 83.6
)

console.log(props)控制台日志(道具)

Code for Piechart:饼图代码:

<PieChart width="400px" height="400px">
          <Pie
            data={piedata}
            cx="50%"
            cy="50%"
            innerRadius={60}
            outerRadius={200}
            paddingAngle={5}
            fill="#82ca9d"
            dataKey="value"
            nameKey="name"
          >
            {piedata.map((entry, index) => (
              <Cell key={`cell-${index}`} fill={PieColors[index]} />
            ))}
          </Pie>
        </PieChart>

I am using "recharts": "^2.1.8",我正在使用“recharts”:“^2.1.8”,

Example in official docs: https://recharts.org/en-US/examples/PieChartWithPaddingAngle官方文档中的示例: https://recharts.org/en-US/examples/PieChartWithPaddingAngle

Nothing is printed in logs also, no errors or warnings.日志中也没有打印任何内容,没有错误或警告。 I also started whole app again but still, charts are not rendering.我也再次启动了整个应用程序,但仍然没有呈现图表。

Seems your data is nested one more level.似乎您的数据嵌套了一层。 Try like below.尝试如下。

const piedata = [
  { name: "Technology", value: props.data.EquityAllocation.tech },
  { name: "Services", value: props.data.EquityAllocation.services },
  {
    name: "Communication",
    value: props.data.EquityAllocation.communication
  },
  { name: "Others", value: props.data.EquityAllocation.others }
];

So I started the migration to MUIv5 and also upgraded to the latest version of recharts and ran into the same issue.所以我开始迁移到 MUIv5 并升级到最新版本的 recharts 并遇到了同样的问题。 Some of my charts stopped rendering.我的一些图表停止渲染。 The observation that I'm having right now is that the chart wants width or height as an actual numeric value.我现在的观察结果是图表需要widthheight作为实际数值。 Try switching width="400px" height="400px" to width={400} height={400}尝试将width="400px" height="400px"切换为width={400} height={400}

That's what they also have in their Demo charts.这也是他们在演示图表中的内容。

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

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