I structured a horizontal bar graph using ng2-charts in angular2 but i am not sure why i am not seeing graph data nor am i seeing any errors in my console.
HTML:
<canvas baseChart [data]="barChartData" [labels]="barChartLabels" [options]="barChartOptions" [legend]="barChartLegend" [chartType]="barChartType" (chartHover)="chartHovered($event)" (chartClick)="chartClicked($event)">
</canvas>
Component:
result: Iresult[] = [];
barChartLabels: string[] =[];
barChartType: string = 'horizontalBar';
barChartLegend: boolean = true;
barChartData: any[] = [];
barChartOptions: any = {
scaleShowVerticalLines: false,
responsive: true
};
barChartLabels: string[] =[];
barChartType: string = 'horizontalBar';
barChartLegend: boolean = true;
barChartData: any[] = [];
getData(){
this.dataService.getData().subscribe(
data => {
this.result = data;
this.barChartLabels = this.result.map(item => item.label);
console.log(this.barChartLabels); // RESULT: ["test 1", "test 2"]
this.barChartData = this.result.map(item => item.data);
console.log(this.barChartData); // RESULT: [25, 55]
},
error => this.errorMessage = <any>error);
console.log(this.barChartLabels); // RESULT: []
console.log(this.barChartData); // RESULT: []
}
JSON file:
[
{
"id": 5,
"label": "test 1",
"data": 25
},
{
"id": 6,
"label": "test 2",
"data": 55
}
]
Service File:
getData(): Observable<IChartData[]>{
return this._http.get('https://api.myjson.com/bins/ywavt')
.map((res:Response) => <IChartData[]> res.json())
.do(data => console.log('All: ' + JSON.stringify(data))) //DEBUG USE
.catch(this.handleError);
}
private handleError(error: Response) {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}
//Console do result: All: [{"id":5,"label":"test 1","data":25},{"id":6,"label":"test 2","data":55}]
Interface class
export interface IChartData {
id: number;
label: string;
data: any;
}
UPDATE
Here is a Plunker of what i have so far: https://plnkr.co/edit/0GyFTkuqF7m8JhUKK6Kl?p=preview
Labels are not being displayed but a colored horizontal bar is not being positioned properly.