简体   繁体   English

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

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

I am unable to plot a Bar graph using VictoryBar from the Victory Library for React Components. 我无法使用Victory Library中的VictoryBar绘制React的条形图。 I am using data from my database to bring the data to the front using Redux and axios middleware. 我正在使用数据库中的数据,使用Redux和axios中间件将数据带到最前端。

The payload is getting updated, however the state of the React component only gets larger for instance. 有效负载正在更新,但是React组件的状态例如只会变大。 Data from 1 country gives an array of objects = 26. When I click a button again, it concatenates the Object to now be 52 Array of Objects. 来自1个国家/地区的数据给出了一个对象数组=26。当我再次单击按钮时,它将对象串联在一起,现在是52个对象数组。 This skews the graph as it is not refreshing the data. 这会歪曲图形,因为它没有刷新数据。


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

The VictoryBar: VictoryBar Chart for React VictoryBar: React的VictoryBar图表

I am not sure how to update the state of the chart, so that the charts update, as I update the input in the Input Box. 我不确定如何更新图表的状态,以便在更新输入框中的输入时更新图表。 I am using this.state.processed to update the information in the VictoryBar . 我使用this.state.processed以更新的信息VictoryBar this.props.waterData is the data received from the Redux Promise, which comes back with the right data for each country, but then it is getting concatenated to the this.state.waterData , everytime I click on Plot Graph. this.props.waterData是从Redux Promise接收到的数据,该数据会返回每个国家的正确数据,但是每次我单击Plot Graph时,它就会被连接到this.state.waterData I'd like it to generate a new plot, everytime I click on Plot Graph. 每次单击“绘图图”时,我都希望它生成一个新的绘图。


That was my mistake, I had already fixed that and tried a bunch of ways of updating the state of the component upon click a Plot Graph Button. 那是我的错误,我已经解决了该问题,并尝试了多种方法来单击“绘图图”按钮来更新组件的状态。 For more clarity I will paste in my action reducer, and perhaps a screenshot of what I am trying to accomplish here. 为了更加清楚,我将粘贴动作减少器,也许是我要在此处完成的操作的屏幕截图。

Action Reducer (get_water_data.js) 行动减速器(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,
      };
    }

Updated Code that works, however I am mutating the array and I am using splice, which is not the correct way to render new data on the screen, everytime I click on Plot Graph. 更新后的代码可以正常工作,但是我正在改变数组并且使用了拼接,这不是每次单击“绘图图”时在屏幕上呈现新数据的正确方法。 I am looking to implement this the correct way, and I don't think I know how. 我希望以正确的方式实施此方法,但我认为我不知道该怎么做。

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

VictoryBar Chart showing Improved Water Resources from Countries 胜利条形图显示了各国水资源的改善

Looks like a simple typo: 看起来像一个简单的错字:

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

should be: 应该:

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

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

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