Javascript 使用react-d3绘制图形

Javascript 使用react-d3绘制图形,javascript,reactjs,d3.js,react-d3,Javascript,Reactjs,D3.js,React D3,我正在尝试使用react-d3-components制作一些数据的柱状图。我的代码是这样的 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.Bar

我正在尝试使用
react-d3-components
制作一些数据的柱状图。我的代码是这样的

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;

根据图形需要操作数据格式似乎有问题。我还没有测试过,但应该可以

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

它又坏了吗@Aayushi/Ankit?我已经试过了,但是没有一个有效。@xp依赖于外部API。
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>
    )
  }
}
[{
  label: 'somethingA',
  values: [{x: 'SomethingA', y: 10}, {x: 'SomethingB', y: 4}, {x: 'SomethingC', y: 3}]
}];