I am starting to work these days with Angular2, and have a question with the framework ng2-charts.
Here is my component.ts code :
import { Component } from '@angular/core';
import { ChartsModule } from 'ng2-charts';
import { PredictionService } from './prediction.service'
@Component({
selector: 'prediction-result-chart',
templateUrl: './predictionResultChart.component.html'
})
export class PredictionResultChartComponent{
public pieChartLabels:string[] = [];
public pieChartData:number[] = [];
public pieChartType:string = 'pie';
public JSONobject = {}
constructor(private predictionService: PredictionService){
this.getPredictions();
}
public getPredictions() {
this.predictionService.getPredictions('hello').do(result => this.populateChart(result)).subscribe();
}
public populateChart(obj): void{
let labels:string[] = [];
let data:number[] = [];
for (var i = 0; i < obj.predictions.length; i++)
{
labels.push(String(obj.predictions[i].class));
data.push(obj.predictions[i].percentage);
};
this.pieChartData = data;
this.pieChartLabels = labels;
}
public chartClicked(e:any):void {}
public chartHovered(e:any):void {}
}
The component.html code :
<div style="display: block">
<canvas baseChart
[data]="pieChartData"
[labels]="pieChartLabels"
[chartType]="pieChartType"
(chartHover)="chartHovered($event)"
(chartClick)="chartClicked($event)"></canvas>
</div>
The service code :
import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
export class PredictionService {
private baseUrl: string = 'http://localhost:8080/predict/';
constructor(private http : Http){
}
getPredictions(text :string) {
return this.http.get(this.baseUrl + text).map(res => res.json());
}
}
With the codes above, here is what I have, a chart without any colors :
In fact when I looked deeply into my code, the HTML component took the variables at the beginning and update them then. So when the labels are empty, even if I add some labels, they will be added as undefined. So I have a chart with the right values but not the right labels. Everything marked as undefined.
And if I initiate the labels at the beginning, I will have a good coloured chart with the right values
So my questions are :
How to load the data, then render the HTML component ?
Is there anyway to render the chart.js component with no data and update it with right labels and data ?
Any help is needed, thanks.
I can get the values when i print to console but does not display on the chart.
Is there a way to update the chart. It seems the chart was loaded before the data was received from the web service.
but the chart never loaded.
first you to be sure that the order of labels is matching the order of the data_set, so basicly you have these two variables :
secondly i had the same issue with the colors, so i had to insert the colors manually, here is an example :
finally after creating these object properly, you have to call this function :
so i think that this might work :
for the html it should be looking like this :
thats all, you should try this way, it should work
I updated the chart labels by updating the labels in the config property.
In HTML add #{chartName} to access it
In standard chart.js you can use the
.update()
prototype method to re-render a chart after you have modified its data (including labels).However, it appears that ng2-charts doesn't provide a mechanism to trigger the update (as per this github issue). I'm not very experienced in Angular2 at all, but perhaps this will work for you? The approach was taken from a comment made by zbagley in the github issue posted 17 days ago (unfortunately I could not find a way to generate a url referencing this specific comment).
The approach is to basically not render your chart until the data is available. Here is the change to your component.ts code.
And then you would use
ngIf
in your component.html code.