繁体   English   中英

无法在React-Redux中使用VictoryBar绘制条形图

[英]Unable to plot a bar graph using VictoryBar in React-Redux

我无法使用Victory Library中的VictoryBar绘制React的条形图。 我正在使用数据库中的数据,使用Redux和axios中间件将数据带到最前端。

有效负载正在更新,但是React组件的状态例如只会变大。 来自1个国家/地区的数据给出了一个对象数组=26。当我再次单击按钮时,它将对象串联在一起,现在是52个对象数组。 这会歪曲图形,因为它没有刷新数据。


    import React, { Component } from 'react';
    import { connect } from 'react-redux';
    import { bindActionCreators } from 'redux';
    import { Link } from 'react-router';
    import { VictoryChart } from 'victory-chart';
    import { VictoryLine } from 'victory-line';
    import { VictoryAxis } from 'victory-axis';
    import { VictoryBar } from 'victory-bar';
    import { getWaterData } from '../actions/get_water_data';
    import CountryList from '../containers/countryList';

    // console.log('Printing the Water Data', waterData);

    var plottingData = [
      { x: '1990', y: 0 },
      { x: '1992', y: 0 },
      { x: '1994', y: 0 },
      { x: '1996', y: 0 },
      { x: '1998', y: 0 },
    ];
    var processWaterData;

    class VictoryPlots extends Component {
      constructor(props) {
        super(props);
        getWaterData();
        this.update = this.update.bind(this);
        this.state = {
          processed: plottingData,
          count: 2,
          countryId: 1,
          // waterData: getWaterData(),
        };

      }

      processingData() {
        processWaterData = [];

        for (var i = 0; i < this.props.waterData.length; i++) {
          processWaterData.push({x: this.props.waterData[i].year, y: this.props.waterData[i].value });
        }

        // return processWaterData;
        this.setState({ processed: processWaterData}, function() {
          console.log("SET State", this.state.processed);
        });

      }

      // data= { this.state.data } />
      // this.props.getWaterData();

      onInputChange(pCountry) {
        this.setState({ processed: [{x:'1990', y: 100}] });
        this.setState({countryId: pCountry}, function() {
          console.log("countryID: ", this.state.countryId);
        });

      }

      render() {
        return (
          <div>

            <div>
              <input value = { this.state.countryId }
              onChange = { event => this.onInputChange(event.target.value)} />
            </div>

          <Link to="/">Main</Link>

          <button onClick = { this.processingData.bind(this) } >
          Plot Graph
          </button>

          <button onClick = { () => { this.props.getWaterData(this.state.countryId);}}>
          Water Data
          </button>


          <VictoryChart >
            <VictoryBar
              data = { this.state.processed }
              dataAttributes= {[
              {fill: "cornflowerblue"}
              ]}
              />
          </VictoryChart>
        </div>
        );

      }
    }; // End of Component

    function mapStateToProps({ waterData }) {
      // console.log('WATER STATE:', { waterData });
      return { waterData };
    }

    function mapDispatchToProps(dispatch) {
      return bindActionCreators({ getWaterData: getWaterData }, dispatch);
    }

    export default connect(mapStateToProps, mapDispatchToProps)(VictoryPlots);

VictoryBar: React的VictoryBar图表

我不确定如何更新图表的状态,以便在更新输入框中的输入时更新图表。 我使用this.state.processed以更新的信息VictoryBar this.props.waterData是从Redux Promise接收到的数据,该数据会返回每个国家的正确数据,但是每次我单击Plot Graph时,它就会被连接到this.state.waterData 每次单击“绘图图”时,我都希望它生成一个新的绘图。


那是我的错误,我已经解决了该问题,并尝试了多种方法来单击“绘图图”按钮来更新组件的状态。 为了更加清楚,我将粘贴动作减少器,也许是我要在此处完成的操作的屏幕截图。

行动减速器(get_water_data.js)

    import axios from 'axios';

    // Send action to server
    export const GET_WATER_DATA = 'GET_WATER_DATA';

    export function getWaterData(pCountryId) {
      // console.log('Calling Water Data Function');

      const url = '//localhost:3001/api/statistics/' + pCountryId;
      // console.log(url);
      const request = axios.get(url);

      // console.log('PROMISE:', request.data);

      return {
        type: GET_WATER_DATA,
        payload: request,
      };
    }

更新后的代码可以正常工作,但是我正在改变数组并且使用了拼接,这不是每次单击“绘图图”时在屏幕上呈现新数据的正确方法。 我希望以正确的方式实施此方法,但我认为我不知道该怎么做。

    processingData() {
      var processWaterData = []

      for (var i = 0; i < this.props.waterData.length; i++) {
        processWaterData.push({x: this.props.waterData[i].year, y: this.props.waterData[i].value });
      }

      this.setState({ processed: processWaterData}, function() {
        // console.log("processed info after: ", this.props.waterData);
        if (this.props.waterData.length > 1) {
          // console.log("Length of the waterData:", this.props.waterData.length);
          this.props.waterData.splice(0, this.props.waterData.length);
        }
      });
    }

胜利条形图显示了各国水资源的改善

看起来像一个简单的错字:

// return processWaterData;
    this.setState({ processed: processed}, function() {

应该:

// return processWaterData;
    this.setState({ processed: processWaterData}, function() {

暂无
暂无

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

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