-
Notifications
You must be signed in to change notification settings - Fork 41
Chart redraw #7
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
base: master
Are you sure you want to change the base?
Chart redraw #7
Conversation
…of time. (necessary for changing data)
…of time. (necessary for changing data)
|
just implement OnChanges as i just did in my pull request. that will redraw every time you change the underlying data |
|
@stefanvitz can you share how you implemented this functionality? |
|
I needed the same functionality basically and some more. import { Directive, ElementRef, Input, Output, OnChanges, OnInit, EventEmitter, DoCheck, IterableDiffers, IterableDiffer } from "@angular/core";
@Directive({
selector: "[GoogleChart]",
})
export class GoogleChartDirective implements OnChanges, OnInit, DoCheck {
private element: HTMLElement;
@Input()
public chartType: string;
@Input()
public chartOptions: Object;
@Input()
public chartData: google.visualization.DataObject;
@Input()
public chartView: string[];
@Output()
public onWrapperReady = new EventEmitter();
@Output()
public afterUpdate = new EventEmitter();
private static loaded: boolean = false;
private chartWrapper: google.visualization.ChartWrapper;
private dataObjectRowDiffer: IterableDiffer<google.visualization.DataObjectRow>;
constructor(element: ElementRef,
iterableDiffer: IterableDiffers) {
this.dataObjectRowDiffer = iterableDiffer.find([]).create<google.visualization.DataObjectRow>(null);
this.element = element.nativeElement;
}
public ngOnInit() {
google.load("visualization", "1.0", { "packages": ["corechart"], callback: () => { this.onLoadCallback(); } });
// the follwoing doesn't work for me for some reason... :-/
//google.charts.load("current", { packages: ["corechart"] });
//google.charts.setOnLoadCallback(() => { this.onLoadCallback(); });
}
public ngOnChanges() {
if (!GoogleChartDirective.loaded) {
return;
}
this.update();
}
public ngDoCheck() {
let dataObjectChanges = this.dataObjectRowDiffer.diff(this.chartData.rows);
if (dataObjectChanges) {
this.update();
}
}
private onLoadCallback = () => {
this.chartWrapper = new google.visualization.ChartWrapper({
chartType: this.chartType,
dataTable: this.chartData,
options: this.chartOptions || {},
view: this.chartView
});
this.chartWrapper.draw(this.element);
this.chartWrapper.setContainerId(this.element.id);
GoogleChartDirective.loaded = true;
this.onWrapperReady.emit(this.chartWrapper);
}
private update() {
if (GoogleChartDirective.loaded)
{
this.chartWrapper.setChartType(this.chartType);
this.chartWrapper.setDataTable(this.chartData as any);
this.chartWrapper.setView(JSON.stringify(this.chartView));
this.chartWrapper.setOptions(this.chartOptions);
this.chartWrapper.draw();
this.afterUpdate.emit();
}
}
} |
|
Does the previous solution posted work in Angular4? Because I cannot make it work |
|
What do you define as previous solution? |
Added functionality, for redrawing a chart if wanted. (Necessary for changing data.)
Just add the
reDraw="true"to your chart, and the chart will be redrawn after a period of time.Example:
<div id="pressureGauge" [chartData]="pressureData" [chartOptions]="pressureChartOptions" chartType="Gauge" reDraw="true" GoogleChart ></div>