【问题标题】:Rechart - adding labels to chartsRechart - 为图表添加标签
【发布时间】:2019-05-12 22:32:29
【问题描述】:

我正在尝试学习如何使用 Rechart。文档说您可以在图表元素上放置标签,并举例说明如何使用“名称”作为标签数据键。

我尝试在我的图表中这样做,但它不起作用。

如果我从字段中删除“标签”,则不会出现任何标签。如果我保留它,那么显示的唯一标签就是饼图楔形上的值。

我有一个数据键为“名称”的标签(根据文档),但它没有呈现在图表上。

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>
    );
  }
}

如何为饼图添加标签?

【问题讨论】:

  • 你试过用nameKey属性告诉Recharts每个扇区的名称吗?此外,一定要写label 而不是Label(确实,第二个指的是 React 组件,但我很确定这是一个错字)
  • 嗨 - 图表文档使用大写的“标签”:recharts.org/en-US/api/Label
  • 我将如何使用nameKey?我试过: Label namekey="name" 但它不起作用
  • 我也试过了;标签 value="name" 但它不起作用
  • 我也尝试过小写 l as: label nameKey="name" 但它会将数据值打印为标签(就像使用默认值时一样)

标签: reactjs recharts


【解决方案1】:

对于寻找答案的其他人来说,这是可行的:

定义一个函数以您想要的方式呈现标签,例如:

let renderLabel = function(entry) {
    return entry.name;
}

将 label 属性设置为指向你的函数:

<Pie label={renderLabel} [ you other properties ]>
[ your content ]
</Pie>

【讨论】:

  • 这需要进入样本。
【解决方案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')
);

【讨论】:

  • 你为什么不用recharts标签方法?
  • 首先,大写Label是一个组件而不是一个方法。主要原因是 Rechart 的 Label 组件在灵活性方面很差,当你需要更多的自定义时,你总是要编写自己的自定义标签。
  • 好的 - 但是你如何使用 Rechart 中的标签。对于我的用例来说,它可能不是很差。当我使用这些工具时,我会尽量保留文档。
【解决方案3】:

您可以在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

【讨论】:

  • 嗨 - 不 - 这不能解决我提出的问题。我发布了一个有效的答案 - 帮助其他人学习
  • 您的发送标签={renderLabel} - renderLabel 是一个函数,它为数组中的每个对象返回值。 const data = [ { name: 'Group A', value: 400 }, { name: 'Group B', value: 300 }, { name: 'Group C', value: 300 }, { name: 'Group D' , 值: 200 }, ];返回值将与您在答案中提到的标签相同
  • @WaseemRajaShaik 是否可以在带有 rechart 的散点图上使用饼图线标签?如果你能提供帮助,我有一个悬而未决的问题会很好
猜你喜欢
  • 1970-01-01
  • 2019-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多