简体   繁体   English

使用react-d3绘制图

[英]Plot graph using react-d3

I am trying to make a histogram of some data that I have using react-d3-components . 我正在尝试使用react-d3-componentsreact-d3-components一些数据的直方图。 The code that I have is like this 我拥有的代码是这样的

import React, { Component } from 'react';
import * as d3 from 'd3';
import * as ReactD3 from 'react-d3-components';
import propTypes from 'prop-types';
var axios=require('axios');
var BarChart=ReactD3.BarChart;

class App extends Component {
  constructor(props){
    super(props);
    this.state={
      data:[],
      label:'',
      values:[],
      x:'',
      y:''
    }
  }

  componentDidMount(){
   this.loadData();   
  }

  loadData(){

      var me=this;
       axios({
          method:'GET',
          url:'https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=SPY&apikey=2QEL3WC4KITIBISR',
        }).then(function(response){

          var values=[];
          var data=[];
          Object.keys(response.data['Monthly Time Series']).forEach(function(k){
            var y=response.data['Monthly Time Series'][k]["1. open"];

            me.setState({
              label:k,
              x:k,
              y:y
            })


           })
           values.push({"x":me.state.x,"y":me.state.y});
           data.push({"label":me.state.x,"values":values});
                       me.setState({
              data:data
            })
          console.log(me.state.data);

        }).catch(function(error){
          console.log(error);
        })
    }


  render() {

    return (
      <div>
        <BarChart data={this.state.data} width={100} height={100}/>
      </div>
    )
  }
}

App.propTypes={
    values:React.PropTypes.array

}

export default App;

With the above code, I get this error 使用上面的代码,我得到这个错误 错误

The documentation that I am referring to is this 我要指的文件是这个

The value of data in c onsole.log(me.state.data) is this [{…}] c onsole.log(me.state.data)的数据值是此[{…}]

0
:
label
:
"2000-02-29"
values
:
Array(1)
0
:
{x: "2000-02-29", y: "139.8000"}
length
:
1
__proto__
:
Array(0)
__proto__
:
Object
length
:
1
__proto__
:
Array(0)

It seems problem in manipulating data format as per graph needs. 似乎在按图需要处理数据格式时出现问题。 I haven't tested it yet, but this should work. 我还没有测试过,但这应该可行。

class App extends Component {
  state = {
    label: 'Monthly Dates',
    values: []
  };

  componentDidMount() {
    this.loadData();
  }

  loadData() {
    axios({
      method: 'GET',
      url: 'https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=SPY&apikey=2QEL3WC4KITIBISR'
    })
      .then(function(response) {
        const values = [];
        if (response && response.data && response.data['Monthly Time Series']) {
          Object.keys(response.data['Monthly Time Series']).forEach((keys) => {
            if (keys) {
              const pointValue = {
                x: String(keys),
                y: Number(response.data['Monthly Time Series'][keys]['1. open'])
              }
              values.push(pointValue);
            }
          });
          this.setState({values});
        }
      })
      .catch(function(error) {
        console.log(error);
      });
  }

  getGraphData() {
    const { label, values } = this.state;
    return [{ label, values }]
  }

  renderGraph() {
    if (this.state.values && this.state.values.length) {
      return (
        <BarChart
          data={this.getGraphData()}
          width={1000}
          height={400}
          margin={{top: 10, bottom: 50, left: 50, right: 10}}
        />
      );
    }
    return '';
  }

  render() {
    return (
      <div>
        {this.renderGraph()}
      </div>
    )
  }
}

UPDATE: you should not render your BarChart until data is present. 更新:在数据存在之前,您不应该渲染BarChart。 As BarChart is expecting a valid data format as below. 由于BarChart期望如下所示的有效数据格式。

[{
  label: 'somethingA',
  values: [{x: 'SomethingA', y: 10}, {x: 'SomethingB', y: 4}, {x: 'SomethingC', y: 3}]
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM