Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing chart instance #3

Closed
ayozebarrera opened this issue May 23, 2016 · 9 comments
Closed

Accessing chart instance #3

ayozebarrera opened this issue May 23, 2016 · 9 comments

Comments

@ayozebarrera
Copy link

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?

@gor181
Copy link
Collaborator

gor181 commented May 23, 2016

Hey @ayozebarrera,

Looking into it, thanks for contributions

@ayozebarrera
Copy link
Author

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

@gor181
Copy link
Collaborator

gor181 commented May 23, 2016

Hey @ayozebarrera ,

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

@gor181
Copy link
Collaborator

gor181 commented May 30, 2016

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

@ayozebarrera
Copy link
Author

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

@rycastaneda
Copy link

is this now possible?

@gor181 gor181 mentioned this issue Aug 6, 2016
@gor181 gor181 closed this as completed in bd11d27 Aug 6, 2016
@gor181
Copy link
Collaborator

gor181 commented Aug 6, 2016

Hey @ayozebarrera @rycastaneda,

It should work now.

Apologies for the delay.

Cheerio,
Goran

@ayozebarrera
Copy link
Author

ayozebarrera commented Aug 9, 2016

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

@guifeliper
Copy link

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>
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants