React-chartjs-2: Accessing chart instance

Created on 23 May 2016  Â·  9Comments  Â·  Source: reactchartjs/react-chartjs-2

Hi

I tried to access to the chart_instance of ChartComponent but I couldn't... I tried to pass a ref to my component but they are stateless... any advice?

All 9 comments

Hey @ayozebarrera,

Looking into it, thanks for contributions

Great, I tried to give a ref to the chart but they are stateless components and they doesn't accept ref prop.

Just changing components to have state, I could ref them and access chart_instance like this:

...
<Bar ref="myChart" .... />
....
let chart = this.refs.myChart.chart_instance;
chart.update();
//and whatever we need ;)

If I'm wrong on anything tell me to know

Hey @ayozebarrera ,

do you have time for PR? Otherwise I may fix it by EOD or tomorrow morning./

Apologies for delaying this fix, was a pretty though week. On it.

@gor181 never saw your previous comment. I could try but maybe thursday or friday!

is this now possible?

Hey @ayozebarrera @rycastaneda,

It should work now.

Apologies for the delay.

Cheerio,
Goran

Dont worry @gor181

I just updated the npm package and I still have the same problem, components are stateless... I guess is a version issue or something

react-chartjs-2  ^1.1.0  →  ^1.2.3 

Edited: Nvm, was my mistake :p

It is working for me guys.
I am new to React, so I don't if I should use the createRef in that case, let me know if anyone has a better method.

My code is like that:

...
 const myChartRef = React.createRef();
  const handleResetZoom = () => {
    let chart = myChartRef.current.chartInstance;
    chart.resetZoom();
  };
...
...
     <div className={classes.chartContainer}>
          <Line
            ref={myChartRef}
            data={chartData}
            options={options}
          />
        </div>
...
Was this page helpful?
0 / 5 - 0 ratings

Related issues

DavidSongzw picture DavidSongzw  Â·  5Comments

Holychung picture Holychung  Â·  4Comments

justinmasse picture justinmasse  Â·  3Comments

flxwu picture flxwu  Â·  3Comments

cbroberg picture cbroberg  Â·  5Comments