【发布时间】:2019-07-10 20:15:56
【问题描述】:
我正在尝试实现this Piechart,但我想在不旋转的情况下旋转它,然后将其文本重新旋转为水平。问题是它没有在它的位置居中。我已经尝试转换原点中心。
这是我的代码:
const data= [
{
name: props.language == "CP",
value: 300
},
{
name: props.language == "CP",
value: 300
}
];
const RADIAN = Math.PI / 180;
const renderCustomizedLabel = ({
cx,
cy,
midAngle,
innerRadius,
outerRadius,
percent,
index,
name
}) => {
const radius = innerRadius + (outerRadius - innerRadius) * 0.5;
const x = cx + radius * Math.cos(-midAngle * RADIAN);
const y = cy + radius * Math.sin(-midAngle * RADIAN);
console.log(cx, cy, midAngle, innerRadius, outerRadius, RADIAN, radius);
return (
<text
style={{ WebkitTransform: "rotate(90deg)", transformOrigin: "center" }}
x={x}
y={y}
fill="white"
textAnchor={x > cx ? "start" : "end"}
dominantBaseline="central"
>
{name}
</text>
);
};
<div style={{ width: "100%", height: 310 }} className="rotate">
<ResponsiveContainer>
<PieChart>
<Pie
data={data}
labelLine={false}
label={renderCustomizedLabel}
fill="#0eb89a"
dataKey="value"
/>
</PieChart>
</ResponsiveContainer>
css:
.rotate {
background-color: transparent;
transform: rotate(270deg);
margin: auto;
}
【问题讨论】:
-
截图没用,设置一个sn-p来重现您的问题,以便我们回答。基本上:容器和文本可能需要相反的旋转才能让文本可读还是关于变换原点?