I want to populate a Highchart graph with the answer of a REST controller, here is the typescript part of the Angular9 component:
import { Component, OnInit } from '@angular/core';
import * as Highcharts from 'highcharts';
import { ApiService } from '../api.service';
@Component({
selector: 'jhi-device-graph',
templateUrl: './device-graph.component.html',
styleUrls: ['./device-graph.component.scss']
})
export class DeviceGraphComponent implements OnInit {
Highcharts: typeof Highcharts = Highcharts;
chartOptions: Highcharts.Options = {
xAxis: {
type: 'datetime'
},
yAxis: {
title: {
text: 'Voltage'
}
},
title: {
text: 'Input voltage'
},
series: [
{
data: [
[Date.UTC(2010, 0, 1), 29.9],
[Date.UTC(2010, 2, 1), 71.5],
[Date.UTC(2010, 3, 1), 106.4]
],
type: 'line',
name: 'Vin1'
},
{
data: [
[Date.UTC(2010, 0, 1), 39.9],
[Date.UTC(2010, 2, 1), 91.5],
[Date.UTC(2010, 3, 1), 96.4]
],
type: 'line',
name: 'Vin2'
}
]
};
data: String[] = [];
isLoadingResults = true;
constructor(private api: ApiService) {}
ngOnInit(): void {
this.api.getInputVoltage('10.1.30.1').subscribe(
(res: any) => {
this.chartOptions.series.data[0]=res;
this.data = res;
//console.log(this.data);
this.isLoadingResults = false;
Highcharts.chart('container', this.chartOptions);
},
err => {
//console.log(err);
this.isLoadingResults = false;
}
);
}
}
The code contains hard coded data for testing purpouses, but is supposed to be replaced with the response to the GET API (which is already implemented). The JSON format is similar to the hard coded data: timestamp and an array of values.
When I try to compile it, I get these errors:
[INFO] ERROR in src/main/webapp/app/device-graph/device-graph.component.ts:54:9 - error TS2532: Object is possibly 'undefined'.
[INFO]
[INFO] 54 this.chartOptions.series.data[0]=res;
[INFO] ~~~~~~~~~~~~~~~~~~~~~~~~
[INFO] src/main/webapp/app/device-graph/device-graph.component.ts:54:34 - error TS2339: Property 'data' does not exist on type 'SeriesOptionsType[]'.
[INFO]
[INFO] 54 this.chartOptions.series.data[0]=res;
[INFO] ~~~~
What am I doing wrong?
EDIT: This is my JSON, but the issue is that the code above don't even compile https://pastebin.com/D6MBG2dW