I am using this npm package to make charts: https://www.npmjs.com/package/angular-highcharts
I have an interface and I want to take values from the interface to populate the chart. At this stage all I want is a line graph.
I tried various ways of doing this with arrays, and so long as I initialise the array by hard-coding all the values it works fine, but I would like to dynamically load values from a service into an interface, then take the values from the interface into the chart. What am I doing wrong?
Here is my component.ts
import { Component, OnInit } from '@angular/core';
import { Chart } from 'angular-highcharts';
import { WeatherstatsService } from '../weatherstats.service';
import 'rxjs/add/operator/map';
interface WeatherData {
id: any;
year: any;
measurement_type: string;
location: string;
month_or_season: string;
value?: any;
}
let datavalues: WeatherData [] = [];
@Component({
selector: 'weatherchart',
templateUrl: './weatherchart.component.html',
styleUrls: ['./weatherchart.component.css']
})
export class WeatherchartComponent implements OnInit {
constructor(private weatherstatsservice: WeatherstatsService) { }
title = "Title";
data: any;
datavalues = datavalues;
values: Array<number> = [];
chart = new Chart({
chart: {
type: 'line'
},
title: {
text: this.title,
},
credits: {
enabled: false
},
series: [{
name: 'Line 1',
data: this.datavalues.year,
}]
});
// add point to chart serie
add() {
this.chart.addPoint(Math.floor(Math.random() * 10));
}
ngOnInit() {
this.weatherstatsservice.getWeatherData()
.subscribe(data => {
this.data = data.slice(0, 100);
if(this.data){
this.data.forEach( res => {
this.datavalues.push({
id: res.id,
year: res.year,
measurement_type: res.measurement_type,
location: res.location,
month_or_season: res.month_or_season,
value: res.value,
})
});
console.log(this.datavalues);
}
})
}
}