I am trying to create line chart using Angular 4 and chart.js using data from database provided by http get method. Situation is weird:
export class ValvesComponent implements OnInit {
valves: Valve[];
ignitions: number[] = [8, 5, 8, 9, 5, 3];
dates: Date[];
selectedValve: Valve;
lineChartData: Array<any>;
constructor(private dataService: DataService) {
this.dataService.getIgnitions().then(ignitions => this.ignitions = ignitions);
this.lineChartData = [{data: this.ignitions, label: 'Series Z'}];
}
I declare array ignitions: number[] with random values. Then in constructor I am setting new values to this line chart data but it is not working. When I list all of ignitions values in html file everything works OK, data are the same as my get method returns. But chart shows still the old ones. Here is how i get those data:
getIgnitions(): Promise<number[]> {
return this.http.get(this.ignitionsUrl)
.toPromise()
.then(response => response.json() as number[])
.catch(this.handleError);
}
and at the end Java method from controller.
@GetMapping("/valve/findallignitions")
public Integer [] findAllIgnitions() {
Iterable<Valve> valvess = valveRepository.findAll();
List<Integer> valves = new ArrayList<>();
Integer [] valvesArray = {1};
for (Valve v : valvess) {
valvesArray = addElement(valvesArray, v.getIgnition());
}
return valvesArray;
}
What should I do to make it work ok, to get data frm http get in my line chart?
Thanks in advance for help!