简体   繁体   English

React-D3 使用获取数据

[英]React-D3 using fetch data

class MyChart extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      data: null
    }

    this.fetchData = this.fetchData.bind(this);
    this.barChart = this.barChart.bind(this);
  }

  componentDidMount() {
    this.fetchData();
    this.barChart(this.state.data);
  }

  fetchData() {
    const API = 'https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDP-data.json'

    fetch(API)
    .then(response => response.json())
    .then(data => {
      this.setState({
        data: data.data
      })
    })
  }

  barChart(dataset) {
    const canvasWidth = 600;
    const canvasHeight = 400;
    const svgCanvas = d3.select(this.refs.canvas)
                     .append('svg')
                     .attr('width', canvasWidth)
                     .attr('height', canvasHeight)

    const xScale = d3.scaleLinear()
                      .domain([0, d3.max(dataset, d => d[0])])
                      .range([0, canvasWidth])

    const yScale = d3.scaleLinear()
                      .domain([0, d3.max(dataset, d => d[1])])
                      .range([canvasHeight, 0])

    svgCanvas.selectAll('rect')
            .data(dataset)
            .enter()
              .append('rect')
              .attr('class', 'bar')
              .attr('x', (d, i) => i * 30)
              .attr('y', d => yScale(d[1]))
              .attr('width', xScale(25))
              .attr('height', d => yScale(d[1]))

  }

  render() {
    return (
      <d>
        <div id='title'>my chart</div>
        <div ref='canvas'></div>
      </d>
    )
  }
}

ReactDOM.render(<MyChart />, document.getElementById('app'))

I am using React to visualize the graph with D3.我正在使用 React 用 D3 来可视化图表。 I tried to fetch data of GDP, and use the data, but I have got nothing on the page.我试图获取 GDP 数据并使用这些数据,但页面上没有任何内容。

when I just put an array as an input to test by myself instead of fetching data, it shows at least something based on the input.当我只是将一个数组作为输入进行测试而不是获取数据时,它至少会根据输入显示一些内容。 I think the problem occurs when fetching data我认为在获取数据时会出现问题

Any thoughts on this matter?对这个问题有什么想法吗?

When the component start rendering, first thing will be called componentDidMount() .So in your componentDidMount() two things are there当组件开始渲染时,第一件事将被称为componentDidMount() 。所以在你的 componentDidMount() 中有两件事

  1. fetching api获取api
  2. rendering barchart , which will be run in same batch渲染条形图,将在同一批次中运行

So when api call happen setState will not assign state.data value as it is asynchronous.所以当 api 调用发生时 setState 不会分配 state.data 值,因为它是异步的。 But next when your barchart want to render, it's getting null value as argument.但是接下来当您的条形图想要呈现时,它会以空值作为参数。

Thats the reason it's not working.这就是它不起作用的原因。

I suggest u to put the this.barChart(data.data) inside the fetch api.我建议你把 this.barChart(data.data) 放在 fetch api 中。

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

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