reactjs - Rechart - adding labels to charts
问题描述
I'm trying to learn how to use Rechart. The documentation says you can put labels on chart elements, and gives an example of how to do it using 'name' as the label data key.
I've tried to do that in my chart, but it doesn't work.
If i remove the 'label' from the field, then no labels appear. If I keep it, then the only labels that display are the values on the pie chart wedges.
I have a Label with a data key of 'name' (per the docs) but it doesn't render on the chart.
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>
);
}
}
How do you add labels to pie charts?
解决方案
对于其他寻找答案的人,这有效:
定义一个函数以您想要的方式呈现标签,例如:
let renderLabel = function(entry) {
return entry.name;
}
将 label 属性设置为指向您的函数:
<Pie label={renderLabel} [ you other properties ]>
[ your content ]
</Pie>
推荐阅读
- python - 如何递归地反转单链表
- java - AWS Java SDK - 将 ProgressListener 与 TransferManager 结合使用
- css - ng-bootstrap 的 Carousel 在全窗口中看起来不正确,但至少看起来不错
- bash - 在 bash 中编写相当于 Python 的输入函数
- git - 是否可以将服务帐户与 Git-Secrets 一起使用?
- python - 提取 span 标签内的信息
- c# - .NET Core 全局到和自包含 exe
- mongodb - 各种规模的MongoDB数据库性能对比
- tkinter - 我无法设置顶级标题
- ios - 为 UIView 设置动画以从屏幕上移出然后从底部无休止地移出屏幕