8

I am using highcharts-react-official in react-redux to create a drilldown chart.

However when I click on a bar to drilldown, I also update some props which causes the component to re-render - which seems to prevent the drilldown event.

I kind of gathered from Change series data dynamically in react-highcharts without re-render of the chart that I should use shouldComponentUpdate and getChart() to prevent re-render and instead dynamically update the data.

My issue is that getChart() doesn't seem to work for the official highcharts react package. I'm getting Uncaught TypeError: this.refs.chart.getChart is not a function

Is there an alternative I'm meant to be using to get and dynamically update the chart? Or some examples that I could look at?

Just including render and shouldComponentUpdate parts here:

shouldComponentUpdate(nextProps, nextState) {
    let chart = this.refs.chart.getChart();
    //dynamically update data using nextProps
    return false;
}

render () {

    const options = {
        chart: {
            type: 'column',
            height: 300,
            events: {
                drillup: (e) => {this.drilledUp(e)}
            }
        },
        plotOptions: {
            series: {
                events:{
                      click: (e) => {this.categoryClicked(e)}
                }
            }
        },
        xAxis: {type: "category"},
        yAxis: {title: {text: 'Amount Spent ($)'}},
        series: [{
            name: 'weekly spending',
            showInLegend: false,
            data: this.props.transactionChartData.series_data,
            cursor: 'pointer',
            events: {
                click: (e)=> {this.weekSelected(e)}
            }
        }],
        drilldown: {
            series: this.props.transactionChartData.drilldown_data

        }
    };
    return (
        <HighchartsReact
        highcharts={Highcharts}
        options={options}
        ref="chart"
        />
    )
}
1

2 Answers 2

7

In highcharts-react-official v2.0.0 has been added allowChartUpdate option, which should work great in your case. By using this option you can block updating the chart with updating the component:

categoryClicked() {
  this.allowChartUpdate = false;
  this.setState({
    ...
  });
}

...

  <HighchartsReact
    ref={"chartComponent"}
    allowChartUpdate={this.allowChartUpdate}
    highcharts={Highcharts}
    options={...}
  />

Moreover, to get the chart instance use refs:

componentDidMount(){
  const chart = this.refs.chartComponent.chart;
}

Live demo: https://codesandbox.io/s/98nl4pp5r4

Sign up to request clarification or add additional context in comments.

1 Comment

ah, now that you've written it out, its so blatantly in the readme as well. Thank you!
-1
// react native functional

import React, {useState,  useRef,useLayoutEffect} from "react"
import HighchartsReactNative from '@highcharts/highcharts-react-native'

function chartcomponent(props){
   const [options, setoptions] = useState({});
   const chartRef = useRef(null);
  

  useEffect(() => {
     // create the options for your chart.
     setoptions({chart:{}, yaxis:{}, xAxis:{},})// etc.

  }, []);

  useLayoutEffect(() => {
       
        // new point to add, you can get new data via props, fetch, socket, etc.
        var x = new Date(hr.timestamp).getTime();
        var y = 10 

        // these are the important parts here:    
        var series = chartRef.current.props.options.series;
        if (!Array.isArray(series)){return;}
        var seriesdata = series[0].data;
        seriesdata.push([x,y]);
        // next line limits points in chart to 10, so new poitn replaces first point
        if (seriesdata.length>10){seriesdata.splice(0,1);}

        return () => {};
    }, [props]);


   return(
       <View style={styles.charting}><HighchartsReactNative styles={{height:300, width:600}}  options={options} ref={chartRef} ></HighchartsReactNative></View>


   )
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.