简体   繁体   English

无法让 react-chartjs 更新

[英]Cannot get react-chartjs to update

I am trying to follow the "Example usage" code from react-chartjs github page我正在尝试遵循react-chartjs github 页面中的“示例用法”代码

I am new to javascript and react and probably just being naive.我是 javascript 的新手并做出反应,可能只是天真。 How can I get the new chartData from "_onChange" to update my PolarAreaChart?如何从“_onChange”获取新图表数据以更新我的 PolarAreaChart? I tried something more direct by calling element.getDocumentById("polarChart"), but that returns nothing and then I cannot call .update on it... the whole "insert redraw in the xml" and it will magically call update seems magical to me :(我通过调用 element.getDocumentById("polarChart") 尝试了更直接的方法,但它什么都不返回,然后我无法调用 .update ......整个“在 xml 中插入重绘”,它会神奇地调用 update 似乎很神奇我 :(

PolarPlot.jsx PolarPlot.jsx

var React = require ('react');
var PolarAreaChart = require ('react-chartjs').PolarArea;
var FilterStore = require ('FilterStore')

var PolarPlot = React.createClass ({
  componentWillMount: function () {
      FilterStore.addChangeListener  (this._onChange);
  },

  _onChange: function () {
    console.log("time to update")

    chartData = [
        {
            value: 300,
            color:"#F7464A",
            highlight: "#FF5A5E",
            label: "Red"
        }]
  },

  render: function () {
    return (
      <PolarAreaChart id="polarChart" data={chartData} options={chartOptions} redraw/>
    );
  }
});

var chartData = [
    {
        value: 300,
        color:"#F7464A",
        highlight: "#FF5A5E",
        label: "Red"
    },
    {
        value: 50,
        color: "#46BFBD",
        highlight: "#5AD3D1",
        label: "Green"
    },
    {
        value: 100,
        color: "#FDB45C",
        highlight: "#FFC870",
        label: "Yellow"
    },
    {
        value: 40,
        color: "#949FB1",
        highlight: "#A8B3C5",
        label: "Grey"
    },
    {
        value: 120,
        color: "#4D5360",
        highlight: "#616774",
        label: "Dark Grey"
    }

];

var chartOptions = [
  {
      //Boolean - Show a backdrop to the scale label
      scaleShowLabelBackdrop : true,

      //String - The colour of the label backdrop
      scaleBackdropColor : "rgba(255,255,255,0.75)",

      // Boolean - Whether the scale should begin at zero
      scaleBeginAtZero : true,

      //Number - The backdrop padding above & below the label in pixels
      scaleBackdropPaddingY : 2,

      //Number - The backdrop padding to the side of the label in pixels
      scaleBackdropPaddingX : 2,

      //Boolean - Show line for each value in the scale
      scaleShowLine : true,

      //Boolean - Stroke a line around each segment in the chart
      segmentShowStroke : true,

      //String - The colour of the stroke on each segement.
      segmentStrokeColor : "#fff",

      //Number - The width of the stroke value in pixels
      segmentStrokeWidth : 2,

      //Number - Amount of animation steps
      animationSteps : 100,

      //String - Animation easing effect.
      animationEasing : "easeOutBounce",

      //Boolean - Whether to animate the rotation of the chart
      animateRotate : true,

      //Boolean - Whether to animate scaling the chart from the centre
      animateScale : false,

      //String - A legend template
      legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].fillColor%>\"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>"

  }

];

module.exports = PolarPlot;

Your PolarPlot component is not rendered unless you explicitly change the state.除非您明确更改状态,否则不会呈现您的PolarPlot组件。 Your chartData is not part of the component state.您的chartData不是组件状态的一部分。 So assigning a new array to that variable does nothing more than that.因此,为该变量分配一个新数组仅此而已。 Move this chartData to the component state.将此chartData移动到组件状态。 Then, whenever you update this state variable you are going to force the re-render.然后,每当您更新此状态变量时,您都将强制重新渲染。 Something like this:像这样的东西:

var PolarPlot = React.createClass ({
  componentWillMount: function () {
      FilterStore.addChangeListener  (this._onChange);
  },

  getInitialState: function() {
    return {chartData: chartData};
  },

  _onChange: function () {
    console.log("time to update")
    this.setState({
      chartData: [{
        value: 300,
        color:"#F7464A",
        highlight: "#FF5A5E",
        label: "Red"
      }]
    });
  },

  render: function () {
    return (
      <PolarAreaChart id="polarChart" data={this.state.chartData} options={chartOptions} redraw/>
    );
  }
});

If you want to know more about how components rendering reacts to state changes check Reactive state section from the React Tutorial.如果你想了解更多关于组件渲染如何对状态变化做出反应,请查看 React Tutorial 中的Reactive state部分。

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

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