问题:
我已经使用图表创建了一个饼图。但是在标签中它显示了值,我想显示数据的名称。
这就是我的代码。
import React, { Component } from "react";
import { bindActionCreators } from "redux";
import { connect } from "react-redux";
import { Card, CardBody, CardTitle, CardFooter } from "reactstrap";
import { PieChart, Pie, ResponsiveContainer, Cell, Tooltip } from "recharts";
import "./SubscribersByChannelCategory.css";
import { get_device_width } from "../../../actions";
const data01 = [
{
name: "Group A",
value: 50
},
{
name: "Group B",
value: 700
},
{
name: "Group C",
value: 700
},
{
name: "Group D",
value: 200
},
{
name: "Group E",
value: 278
},
{
name: "Group F",
value: 189
},
{
name: "Group C",
value: 300
},
{
name: "Group G",
value: 200
},
{
name: "Group H",
value: 278
},
{
name: "Group I",
value: 189
}
];
const COLORS = [
"#65d3da",
"#79d69f",
"#fad144",
"#d76c6c",
"#138185",
"#26a0a7",
"#ec983d",
"#cbe989",
"#f9ec86",
"#ebf898"
];
class SubscribersByChannelCategory extends Component {
constructor(props) {
super(props);
this.getDeviceWidth = this.getDeviceWidth.bind(this);
}
componentDidMount() {
this.getDeviceWidth();
window.addEventListener("resize", this.getDeviceWidth);
}
getDeviceWidth() {
this.props.get_device_width();
}
render() {
let innerRadius = "50%";
let outerRadius = "90%";
let yaspect = 6.0;
if (this.props.device >= 1024) {
outerRadius = "95%";
innerRadius = "70%";
yaspect = 5.7971;
}
if (this.props.device >= 1666) {
outerRadius = "90%";
innerRadius = "60%";
yaspect = 7.297;
}
return (
<div>
<Card className="subscribers-by-channel-category-card">
<CardTitle className="subscribers-by-channel-category-card-title">
Subscribers by Channel Category
</CardTitle>
<CardBody>
<ResponsiveContainer
width="100%"
height="100%"
aspect={5.0 / yaspect}
>
<PieChart>
<Pie
data={data01}
dataKey="value"
nameKey="name"
fill="#8884d8"
innerRadius={innerRadius}
outerRadius={outerRadius}
label={true}
labelLine={false}
cursor="pointer"
>
{data01.map((entry, index) => (
<Cell fill={COLORS[index]} key={index}/>
))}
</Pie>
<Tooltip content={<CustomTooltip />} />
</PieChart>
</ResponsiveContainer>
<CardFooter className="subscribers-by-channel-category-footer">
<div>Bring the cursor near to see the details</div>
</CardFooter>
</CardBody>
</Card>
</div>
);
}
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({ get_device_width }, dispatch);
}
function mapStateToProps(state) {
return {
device: state.device
};
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(SubscribersByChannelCategory);
const CustomTooltip = ({ active, payload }) => {
if (active) {
return (
<div className="subscribers-by-channel-tooltip">
<p className="subscribers-by-channel-tooltip-title">{payload[0].name}</p>
<p className="subscribers-by-channel-tooltip-label">{`Channel Category : ${payload[0].name}`}</p>
<p className="subscribers-by-channel-tooltip-intro">{`Subscribers : ${payload[0].value}`}</p>
<p className="subscribers-by-channel-tooltip-data">
{`Share : ${payload[0].value}`}
</p>
</div>
);
}
return null;
};
我做了很多尝试以找到解决该问题的方法,但是我无法找到有关此问题的任何解决方法。有人可以通过修改我的代码来帮助我解决此问题吗?谢谢。
答案 0 :(得分:0)
有一个适当的方法可能会有用。我无法重新编辑您拥有的代码。因此,我从图表中获取了代码并进行了尝试。这是解决方案。
? dev.new
您只需要删除百分比部分并为其赋予 {data [index] .name} 。在这种情况下,您必须使用data01及其索引。