It is relatively easy to put json into html using ngFor, but I need to get the value of a json field in the ts of my component so I can put it into a graph.
I have a field called 'value' in my json. How do I put all the values into an array?
My code so far:
import { WeatherstatsService } from '../weatherstats.service';
import 'rxjs/add/operator/map';
@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 = [];
chart = new Chart({
chart: {
type: 'line'
},
title: {
text: this.title,
},
credits: {
enabled: false
},
series: [{
name: 'Line 1',
data: [1, 2, 3]
}]
});
// add point to chart serie
add() {
this.chart.addPoint(Math.floor(Math.random() * 10));
}
getyeardata(data, datavalues) {
}
ngOnInit() {
this.weatherstatsservice.getWeatherData()
.subscribe(data => {
this.data = data;
})
console.log(value);
// console.log(this.data)
}
}
My json looks like this, but is massive:
[{"id":1,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"JAN","value":6.1},{"id":2,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"FEB","value":7.2},{"id":3,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"MAR","value":8.9},{"id":4,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"APR","value":9.6},{"id":5,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"MAY","value":14.5},{"id":6,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"JUN","value":17.1},{"id":7,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"JUL","value":17.3},{"id":8,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"AUG","value":16.9},{"id":9,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"SEP","value":15.6},{"id":10,"measurement_type":"Tmax","location":"Wales","year":1910,"month_or_season":"OCT","value":12.5},
The json fields are id, measurement_type, location, year, month_or_season
Error log:
WeatherchartComponent.html:7 ERROR TypeError: Cannot read property 'year' of null
at Object.eval [as updateRenderer] (WeatherchartComponent.html:7)
at Object.debugUpdateRenderer [as updateRenderer] (core.js:14378)
at checkAndUpdateView (core.js:13514)
at callViewAction (core.js:13859)
at execEmbeddedViewsAction (core.js:13817)
at checkAndUpdateView (core.js:13510)
at callViewAction (core.js:13859)
at execComponentViewsAction (core.js:13791)
at checkAndUpdateView (core.js:13515)
at callViewAction (core.js:13859)