简体   繁体   English

尝试创建自定义 svg label 时反应最小饼图,所有标签都呈现在同一个 position

[英]react-minimal-pie-chart when trying to create custom svg label, all labels are rendering on same position

I am using react-minimal-pie-chart When trying to create custom svg label, all labels are rendering on same position我正在使用react-minimal-pie-chart尝试创建自定义 svg label 时,所有标签都呈现在同一个 position

const Element = (props) => {
  return (
    <text
      dominant-baseline="central"
      x="50"
      y="50"
      dx="19.151111077974452"
      dy="16.06969024216348"
      text-anchor="middle"
      style={{ fontSize: "10px" }}
    >
      <tspan x="50" dy="1.2em">
        {`${Math.round(props.percentage)} %`}
      </tspan>
      <tspan x="0" dy="1.2em">
        {props.title}
      </tspan>
    </text>
  );
};
<PieChart
              data={[
                { title: "Excellent", value: 10, color: "#8dcd81" },
                { title: "Good", value: 15, color: "#eefa6b" },
                { title: "Weak", value: 20, color: "#FF6382" },
              ]}
              label={({ dataEntry }) => <Element {...dataEntry} />}
            />

在此处输入图像描述

I want it to look like this:-我希望它看起来像这样:-

在此处输入图像描述

What can i do for the above svg code to fix the issue?对于上面的 svg 代码我可以做些什么来解决这个问题?

I'm the react-minimal-pie-chart maintainer.我是react-minimal-pie-chart维护者。

As @danny-365csi-engelman said, you should provide dynamic x , y , dx and dy values to the custom tspan element.正如@danny-365csi-engelman 所说,您应该为自定义tspan元素提供动态xydxdy值。

label render prop accept a function with the signature described here . label渲染道具接受带有此处描述的签名的 function。 You might pass x , y , dx , dy values down to the custom svg element.您可以将xydxdy值传递给自定义 svg 元素。

Something like:就像是:

label={({ x, y, dx, dy, dataEntry }) => (
  <text
    x={x}
    y={y}
    dx={dx}
    dy={dy}
    dominant-baseline="central"
    text-anchor="middle"
    style={}
  >
    {`${Math.round(dataEntry.value)} %`}
  </text>
)}

This is how I managed to get it following @andrea-carraro answer.这就是我在@andrea-carraro 回答之后设法得到它的方式。

<PieChart
        style={{width: '300px', height: '300px'}}
        label={({ x, y, dx, dy, dataEntry }) => (
            <text
                x={x}
                y={y}
                dx={dx}
                dy={dy}
                dominant-baseline="central"
                text-anchor="middle"
                style={{
                    fill: '#fff', pointerEvents: 'none', fontSize: '3px'
                }}>
                <tspan x={x} y={y} dx={dx} dy={dy}>{dataEntry.title}</tspan>
                <tspan x={x} y={y+5} dx={dx} dy={dy}>{`${Math.round(dataEntry.value)}%`}</tspan>
            </text>
        )}
        data={data}
    />

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

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