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 char.js prototype methods #2

Closed
Methodician opened this issue Oct 21, 2016 · 13 comments
Closed

Accessing char.js prototype methods #2

Methodician opened this issue Oct 21, 2016 · 13 comments

Comments

@Methodician
Copy link

I'm not sure if you've got a reliable way to access the chart methods such as .destroy() and .update() as described here: http://www.chartjs.org/docs/#advanced-usage-prototype-methods

If so, a little documentation would rock.

If not, it would be a great feature that would really make this tool worth while!

@emn178
Copy link
Owner

emn178 commented Oct 24, 2016

Hi, v0.1.5 exposed chart instance to public. You can use ViewChild or local variable to access it.

@Methodician
Copy link
Author

Thanks for the update!

FWIW and if anyone else wants an easy hack - I got around my particular issue (updating the chart with changing data) simply by wrapping the containing div in an *ngIf and having it removed from and re-added to the DOM each time the data changed. It sounds inefficient but there is no perceptible delay and it was ridiculously easy.

None the less, I'm sure I'll find other reasons to access the chart instance. Great tool @emn178 ! Nice and flexible. Love it when people don't go too far - this allows for lots of customization without making get-going hard.

@Standaa
Copy link

Standaa commented Nov 1, 2016

@emn178 Thanks for the library, it's a good relief after ng2-charts...
How do you precisely get a hold on the chart instance in the view child ?
Tried to do this :
import { ChartModule } from 'angular2-chartjs';
@ViewChild(ChartModule) chart: ChartModule;
And this.chart.update();
But I get a property does not exist on ChartModule error.

Thanks !

@emn178
Copy link
Owner

emn178 commented Nov 1, 2016

You should use ChartComponent

import { ChartComponent } from 'angular2-chartjs';

//...

@ViewChild(ChartComponent) chart: ChartComponent; 

//...

this.chart.chart.update();

@Standaa
Copy link

Standaa commented Nov 1, 2016

Thanks ! My bad for not deep diving in the library's code.
Everything is running smoothly on angular-cli. No fix, no hack nothing.
I wish I had gone for this lib in the first place.

@shyamal890
Copy link

But lets say there are 4 charts on display would this.chart.chart.update(); update them all?

@MeghaKumari2203
Copy link

import { ChartComponent } from 'angular2-chartjs';
//...
@ViewChild(ChartComponent) chart: ChartComponent;
//...
this.chart.chart.update();

It's not working on my ionic 3 project.
I am getting an error: TypeError : this.chart is undefined.
Somebody,please help.

@emn178
Copy link
Owner

emn178 commented Apr 5, 2018

@MeghaKumari2203 more detail info please

@jmbrosa
Copy link

jmbrosa commented Aug 22, 2018

import { ChartComponent } from 'angular2-chartjs';
//...
@ViewChild(ChartComponent) chart: ChartComponent;
//...
this.chart.chart.update();

I'm using Angular 5 and tried the ViewChild to get my ChartComponent.

When I try to get the this.chart is undefined. I set correctly the options, the datasets are ok and the chart appears!

The main goal to use the ChartComponent will be to use the native method generateLegend() of chartjs. Because I created a customize html legend. Anyone can help me?

Example: this.chart.chart.generateLegend() <- I want only use this

@emn178
Copy link
Owner

emn178 commented Aug 23, 2018

when do you call this.chart, maybe it's not ready

@jmbrosa
Copy link

jmbrosa commented Aug 23, 2018

when do you call this.chart, maybe it's not ready

I thought on this case, but I call this.chart after load all chart content

@emn178
Copy link
Owner

emn178 commented Aug 23, 2018

post your sample code?

@jmbrosa
Copy link

jmbrosa commented Aug 23, 2018

Solve it! I used in Angular the ngAfterViewChecked and this.chart.chart.generateLegend() works.

Many thanks

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

6 participants