I have doubts about how to correctly load the data of the graphic and its label.
This is the code for my service:
getinvfull () {
return this._http.get (this.url + 'getinvfull'). map (res => res.json ());
}
This the code of my component:
public lineChartData: Array <any> = [];
public lineChartLabels: Array <any> = [];
public lineChartOptions: any = {
responsive: true
};
public lineChartLegend: boolean = true;
public lineChartType: string = 'line';
// events
public chartClicked (e: any): void {
console.log (e);
}
public chartHovered (e: any): void {
console.log (e);
}
ngOnInit () {
console.log (this._invService.getinvfull ());
this._invService.getinvfull ().
data1 => {
if (! data1) {
console.log ('error loading data');
} else {
this.resultData = data1;
this.barChartLabels = this.resultData.map (item => item.day);
var d = this.resultData.map (item => item.sensorluz1)
console.log ('this is the variable d' + d);
this.barChartData = this.resultData.map (item => item.sensorlight1);
this.data = this.barChartData;
console.log (this.barChartData);
console.log ('the variable data' + this.data);
this.loaded = true;
}
},
error => {
console.log (<any> error);
}
);
}
this is my template:
<div style = "display: block;">
<canvas baseChart width = "400" height = "400"
[datasets] = "lineChartData"
[labels] = "lineChartLabels"
[options] = "lineChartOptions"
[colors] = "lineChartColors"
[legend] = "lineChartLegend"
[chartType] = "lineChartType"
(chartHover) = "chartHovered ($ event)"
(chartClick) = "chartClicked ($ event)"> </ canvas>
</ div>
I'm testing with the lineChart chart my JSON is this:
[{"id":13,"sensorluz1":"32","sensorluz2":"55","sensorluz3":"33","fecha":"2017-09-05T11:15:06.000Z"},{"id":16,"sensorluz1":"111","sensorluz2":"222","sensorluz3":"66","fecha":"2017-09-05T17:22:14.000Z"},{"id":17,"sensorluz1":"44","sensorluz2":"55","sensorluz3":"33","fecha":"2017-09-05T17:22:14.000Z"}]
I would like to show the date and the three types of sensors in the same graph.
I tried with another type of graph with the barChart and I work something but not quite.
barChartOptions: any = {
scaleShowVerticalLines: false,
responsive: true,
scales: {
xAxes: [{
ticks: {
beginAtZero:true
}
}]
}
};
barChartLabels: string[] =[];
barChartType: string = 'horizontalBar';
barChartLegend: boolean = true;
barChartData: any[] =[];
resultData: Animal[] =[];
loaded = false;
ngOnInit(){
console.log(this._invService.getinvfull());
this._invService.getinvfull().subscribe(
data1 => {
if (!data1) {
console.log('error al cargar datos');
} else {
this.resultData = data1;
this.barChartLabels = this.resultData.map(item => item.fecha);
//this.lineChartLabels = this.resultData.map(item => item.fecha);
var d=this.resultData.map(item => item.sensorluz1)
console.log('esta es la variable d'+d);
this.barChartData = this.resultData.map(item => item.sensorluz1);
//let newChartData:Array<any> = [];
//newChartData.push({data: [1, 2], label: 'Series A'}, {data: [1, 2], label: 'Series B'});
//this.lineChartData = newChartData;
this.data = this.barChartData;
console.log(this.barChartData);
console.log('la variable data'+this.data);
this.loaded = true;
//data = this.animals;
}
},
error =>{
console.log(<any>error);
}
);
}
<canvas *ngIf="loaded" baseChart [data]="barChartData"
[labels]="barChartLabels" [options]="barChartOptions"
[legend]="barChartLegend" [chartType]="barChartType"
(chartHover)="chartHovered($event)" (chartClick)="chartClicked($event)">
</canvas>
Thanks for the help
I already found the solution, I leave it in case it is worth to some other user.
My service:
My component:
My html
Greetings and thank you.