React Recharts——从不同对象获取数据

React Recharts- getting data from different objects

我正在使用 React Recharts 进行可视化,但是,我在显示具有不同对应对象内容的简单条形图时遇到了挑战。

[
  {
    "month": "2017-07",
    "commodities": [
      { "name": "wheat", "moves": 100, "avg_weight": 167 },
      { "name": "maize", "moves": 150, "avg_weight": 367 },
      { "name": "grains", "moves": 89, "avg_weight": 467 }
    ]
  },
  {
    "month": "2017-08",
    "commodities": [
      { "name": "mangoes", "moves": 140, "avg_weight": 167 },
      { "name": "grains", "moves": 190, "avg_weight": 47 }
    ]
  },
  {
    "month": "2017-09",
    "commodities": [
      { "name": "wheat", "moves": 130, "avg_weight": 267 },
      { "name": "tomatoes", "moves": 176, "avg_weight": 132 },
      { "name": "onions", "moves": 120, "avg_weight": 47 }
    ]
  },
  {
    "month": "2018-10",
    "commodities": [
      { "name": "oranges", "moves": 130, "avg_weight": 267 },
    ]
  },
]

我有这个示例 json 对象,我希望月份作为我的 X 轴,商品的移动作为我的 Y 轴。 理想情况下,下图代表了我想要如何显示我的数据。 谢谢您的帮助,。

只需三步即可得到您想要的-

  1. 将数据转换为Rechart要求的格式
  2. 获取唯一标签

这是工作代码沙箱https://codesandbox.io/s/n95n2wpp6l

import React, { Component } from "react";
import {
  BarChart,
  Bar,
  XAxis,
  YAxis,
  CartesianGrid,
  Tooltip,
  Legend
} from "recharts";
import _ from "underscore";

class BarGraph extends Component {
  state = {
    converted: [],
    labels: []
  };
  componentDidMount() {
    let data = [
      {
        month: "2017-07",
        commodities: [
          { name: "wheat", moves: 100, avg_weight: 167 },
          { name: "maize", moves: 150, avg_weight: 367 },
          { name: "grains", moves: 89, avg_weight: 467 }
        ]
      },
      {
        month: "2017-08",
        commodities: [
          { name: "mangoes", moves: 140, avg_weight: 167 },
          { name: "grains", moves: 190, avg_weight: 47 }
        ]
      },
      {
        month: "2017-09",
        commodities: [
          { name: "wheat", moves: 130, avg_weight: 267 },
          { name: "tomatoes", moves: 176, avg_weight: 132 },
          { name: "onions", moves: 120, avg_weight: 47 }
        ]
      },
      {
        month: "2018-10",
        commodities: [{ name: "oranges", moves: 130, avg_weight: 267 }]
      }
    ];
    let converted = this.convertData(data);
    let labels = this.getLabels(data);
    console.log(labels, converted);

    this.setState({
      converted: converted,
      labels: labels
    });
  }

  convertData(data) {
    let arr = [];
    for (let i = 0; i < data.length; i++) {
      let obj = { month: data[i].month };
      // loop throgh comodities
      for (let j = 0; j < data[i].commodities.length; j++) {
        let commodity = data[i].commodities[j];
        obj[commodity.name] = commodity.moves;
      }
      arr.push(obj);
    }
    return arr;
  }

  getLabels(data) {
    let arr = [];
    _.each(data, obj => {
      arr = arr.concat(obj.commodities);
    });
    let grouped = _.groupBy(arr, "name");
    return Object.keys(grouped);
    //return Object.keys(_.groupby(arr.name));
  }

  render() {
    return (
      <BarChart
        width={600}
        height={300}
        data={this.state.converted}
        margin={{ top: 5, right: 30, left: 20, bottom: 5 }}
      >
        <CartesianGrid strokeDasharray="3 3" />
        <XAxis dataKey="month" />
        <YAxis />
        <Tooltip />
        <Legend />
        {this.state.labels.map((label, index) => (
          <Bar key={index} dataKey={label} fill="#8884d8" />
        ))}
      </BarChart>
    );
  }
}

export default BarGraph;