我正在尝试学习如何使用Rechart。该文档说,您可以在图表元素上放置标签,并举例说明如何使用“名称”作为标签数据键。
我已尝试在图表中执行此操作,但这不起作用。
如果我从字段中删除了“标签”,则不会显示任何标签。如果我保留它,那么显示的唯一标签就是饼图楔形上的值。
我有一个Label,其数据键为“名称”(根据文档),但未在图表上呈现。
import React, { PureComponent } from 'react';
import {
ResponsiveContainer, PieChart, Pie, Legend, Label, LabelList
} from 'recharts';
const data = [
{ name: 'Group A', value: 400 }, { name: 'Group B', value: 300 },
{ name: 'Group C', value: 300 }, { name: 'Group D', value: 200 },
];
export default class Example extends PureComponent {
static jsfiddleUrl = '//jsfiddle.net/alidingling/6okmehja/';
render() {
return (
<div style={{ width: '100%', height: 300 }}>
<ResponsiveContainer>
<PieChart>
<Pie dataKey="value"
data={data}
fill="#8884d8"
Label dataKey="name"
/>
</PieChart>
</ResponsiveContainer>
</div>
);
}
}
如何在饼图中添加标签?
答案 0 :(得分:2)
抱歉,我终于提出了一个解决方案,即使它不是很简单
const {ResponsiveContainer, PieChart, Pie, Legend} = Recharts;
const data = [{name: 'Group A', value: 400}, {name: 'Group B', value: 300},
{name: 'Group C', value: 300}, {name: 'Group D', value: 200}]
const RADIAN = Math.PI / 180;
const renderCustomizedLabel = ({
x, y, name
}) => {
return (
<text x={x} y={y} fill="black" textAnchor="end" dominantBaseline="central">
{name}
</text>
);
};
const SimplePieChart = React.createClass({
render () {
return (
<ResponsiveContainer>
<PieChart>
<Pie data={data} fill="#8884d8" label={renderCustomizedLabel} nameKey="name"/>
</PieChart>
</ResponsiveContainer>
);
}
})
ReactDOM.render(
<SimplePieChart />,
document.getElementById('container')
);
答案 1 :(得分:1)
您可以在http://recharts.org/en-US/examples/PieChartWithCustomizedLabel
中找到PieChartWithCustomizedLabel的示例或以下代码将为您提供帮助
import React, { PureComponent } from 'react';
import {
PieChart, Pie, Sector, Cell,
} from 'recharts';
const data = [
{ name: 'Group A', value: 400 },
{ name: 'Group B', value: 300 },
{ name: 'Group C', value: 300 },
{ name: 'Group D', value: 200 },
];
const COLORS = ['#0088FE', '#00C49F', '#FFBB28', '#FF8042'];
const RADIAN = Math.PI / 180;
const renderCustomizedLabel = ({
cx, cy, midAngle, innerRadius, outerRadius, percent, index,
}) => {
const radius = innerRadius + (outerRadius - innerRadius) * 0.5;
const x = cx + radius * Math.cos(-midAngle * RADIAN);
const y = cy + radius * Math.sin(-midAngle * RADIAN);
return (
<text x={x} y={y} fill="white" textAnchor={x > cx ? 'start' : 'end'} dominantBaseline="central">
{`${(percent * 100).toFixed(0)}%`}
</text>
);
};
export default class Example extends PureComponent {
static jsfiddleUrl = 'https://jsfiddle.net/alidingling/c9pL8k61/';
render() {
return (
<PieChart width={400} height={400}>
<Pie
data={data}
cx={200}
cy={200}
labelLine={false}
label={renderCustomizedLabel}
outerRadius={80}
fill="#8884d8"
dataKey="value"
>
{
data.map((entry, index) => <Cell key={`cell-${index}`} fill={COLORS[index % COLORS.length]} />)
}
</Pie>
</PieChart>
);
}
}
此处演示-link
答案 2 :(得分:0)
对于其他寻求答案的人,此方法有效:
定义一个函数以您想要的方式呈现标签,例如:
let renderLabel = function(entry) {
return entry.name;
}
设置标签prop指向您的功能:
<Pie label={renderLabel} [ you other properties ]>
[ your content ]
</Pie>