如何通过输入字段使用React JS更新Highcharts

时间:2017-03-13 15:20:50

标签: javascript jquery reactjs highcharts

我正在尝试使用输入字段创建Highcharts图表,允许用户自定义自己的颜色首选项。但是我使用React JS相对较新。我从输入字段传递值并将它们附加到图表选项,并希望在单击提交按钮时更新图表。我已经尝试过使用setState()和react api中的redraw(),但我没有运气。我在浏览器上运行时遇到此错误:

Uncaught TypeError: Cannot read property 'setState' of undefined
    at HTMLButtonElement.<anonymous> (VM111:125)
    at HTMLButtonElement.dispatch (jquery.min.js:3)
    at HTMLButtonElement.q.handle (jquery.min.js:3)
(anonymous) @ VM111:125
dispatch @ jquery.min.js:3
q.handle @ jquery.min.js:3

非常感谢任何帮助,谢谢!

    $('#submit').click(function () {     
           this.chart.setState(function () {
            return {
                chart:{
                        backgroundColor: '' + $('#backgroundColorSetting').val() //sets chart background 
                },
                series: [{
                        color: '' + $('#colorSetting').val(),           //sets the color of Line
                    name: $('#xaxis').val()                                 //sets name of X-Axis
                }],
                yAxis:{
                        text: $('#yaxis').val()                             //sets name of Y-Axis
                }

            }
            });
    });
var Chart = React.createClass({

    componentDidMount: function() {

      if (this.props.modules) {
        this.props.modules.forEach(function(module) {
          module(Highcharts);
        });
      }

      this.chart = new Highcharts[this.props.type || "Chart"](
        this.props.container,
        this.props.options
      );
    },

    componentWillUnmount: function() {
      this.chart.destroy();
    },

     render: function() {
      return React.createElement('div', {
        id: this.props.container
      });
    }
  }),
  categories= ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  data= [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
element = React.createElement(Chart, {
  container: 'chart',
  options: {

    title: {
        text: 'Inverted Chart'
    },
    chart: {
            inverted: true,
            polar: false
            },
    subtitle: {
        text: 'Subtitle1'
    },

    xAxis: {
        categories: categories
    },
    series: [{
        type: 'line',
        colorByPoint: true,
        data: data,
        lineColor:'#f7eac8'
        //showInLegend: false
    }]
  }
    });
    ReactDOM.render(element, document.getElementById('react-inverted'));

1 个答案:

答案 0 :(得分:0)

如果使用React,则无需使用jQuery。可以使用React组件处理表单。我建议在React docs中阅读forms(特别是受控组件)。我将使用一个不受控制的组件。

更改渲染方法以渲染表单:

render: function() {
     return React.createElement('div', null,
       React.createElement('div', {
         id: this.props.container
       }),
       React.createElement('form', {
           onSubmit: this.onSubmit
         },
         React.createElement('input', {
           ref: input => this.colorInput = input,
           placeholder: 'Type red, black or blue'
         }),
         React.createElement('input', {
           type: 'submit'
         })
       )
     );
   }

并定义一个提交处理程序,它将通过Axis.update()

更新轴
onSubmit: function(e) {
     e.preventDefault();

     var color = this.colorInput.value;
     if (['red', 'black', 'blue'].includes(color)) {
       this.chart.xAxis[0].update({
         lineColor: color
       });
     }
   },

实例:https://jsfiddle.net/mec4huyw/