如何显示ReCharts中的元素总数?

时间:2017-07-08 15:43:12

标签: javascript reactjs redux material-ui

我想知道如何从material-ui访问recharts的“payload”字段中的其他数据项。我试着向上看,但没有找到参考。我想访问'return'函数中的其他组名和值

const {PieChart, Pie, Sector} = Recharts;
const data = [{name: 'Group B', value: 14},{name: 'Group A', value: 2}, {name: 'Group C', value: 10}];

const renderActiveShape = (props) => {
  const RADIAN = Math.PI / 180;
  const { cx, cy, midAngle, innerRadius, outerRadius, startAngle, endAngle,
    fill, payload, percent, value } = props;
  //payload is the data here...
  const sin = Math.sin(-RADIAN * midAngle);
  const cos = Math.cos(-RADIAN * midAngle);
  const sx = cx + (outerRadius + 10) * cos;
  const sy = cy + (outerRadius + 10) * sin;
  const mx = cx + (outerRadius + 30) * cos;
  const my = cy + (outerRadius + 30) * sin;
  const ex = mx + (cos >= 0 ? 1 : -1) * 22;
  const ey = my;
  const textAnchor = cos >= 0 ? 'start' : 'end';

  return (
    <g>
      <text x={cx} y={cy} dy={8} textAnchor="middle" fill={fill}>{payload.name}</text>

      {/*I want to know how to access the second element in the payload*/}

      <Sector
        cx={cx}
        cy={cy}
        innerRadius={innerRadius}
        outerRadius={outerRadius}
        startAngle={startAngle}
        endAngle={endAngle}
        fill={fill}
      />
      <Sector
        cx={cx}
        cy={cy}
        startAngle={startAngle}
        endAngle={endAngle}
        innerRadius={outerRadius + 6}
        outerRadius={outerRadius + 10}
        fill={fill}
      />
      <path d={`M${sx},${sy}L${mx},${my}L${ex},${ey}`} stroke={fill} fill="none"/>
      <circle cx={ex} cy={ey} r={2} fill={fill} stroke="none"/>
      <text x={ex + (cos >= 0 ? 1 : -1) * 12} y={ey} textAnchor={textAnchor} fill="#333">{`PV ${value}`}</text>
      <text x={ex + (cos >= 0 ? 1 : -1) * 12} y={ey} dy={18} textAnchor={textAnchor} fill="#999">
        {`(Rate ${(percent * 100).toFixed(2)}%)`}
      </text>
    </g>
  );
};

const TwoLevelPieChart = React.createClass({
    getInitialState() {
    return {
      activeIndex: 0,
    };
  },


    render () {
    return (
        <PieChart width={800} height={400} onMouseEnter={this.onPieEnter}>
        <Pie 
            activeIndex={this.state.activeIndex}
          activeShape={renderActiveShape} 
          data={data} 
          cx={300} 
          cy={200} 
          innerRadius={60}
          outerRadius={80} 
          fill="#8884d8"/>
       </PieChart>
    );
  }
})

ReactDOM.render(
  <TwoLevelPieChart />,
  document.getElementById('container')
);

2 个答案:

答案 0 :(得分:0)

您无法从renderActiveShape内部访问整个数据集,因为它只会传递到图表的一个部分,即活动部分。您可以做的是提供<Legend>组件:

const renderLegend = (props) => {
  const { payload } = props;

  return (
    <p>Total count: {payload.length}</p>
  );
}

然后在你的图表中:

<Legend content={renderLegend} />

答案 1 :(得分:0)

Okay so after banging my head for a long time, here's the solution:

whatever data you'd like to pass in for your own purpose, just add an extra field in the first object and then use it, example-

const data = [{name: 'Group B', value: 14, total: 420, whatever: "myData", kiddingMe: "Yes"},{name: 'Group A', value: 2}, {name: 'Group C', value: 10}];

and finally in the piechart, just use payload.fieldName