I want to create chart based on the JSON data. I using angular2-highcharts my ChartsMain component looks like:
@Component({
moduleId: module.id,
selector: 'charts',
templateUrl: 'charts.html',
directives: [CHART_DIRECTIVES,]
providers: [DataService]
})
export class ChartsMain {
result: Data[];
constructor(DataService:DataService) {
DataService.getData().subscribe(res => this.result = res);
this.options = {
chart: {
type: "candlestick"
},
title: {
text: "JSON data"
},
xAxis: {
type: "category",
allowDecimals: false,
title: {
text: ""
}
},
yAxis: {
title: {
text: "Number"
}
},
series: [{
name: "Hour",
data: this.result
}]
};
}
options: Object;
And my DataService looks:
@Injectable()
export class DataService {
http: Http;
constructor(http: Http) {
this.http = http;
}
getData(): Observable<Array<Data>> {
return this.http.get('http://JSON-DATA')
.map(this.extractData)
.catch(this.handleError)
}
private extractData(res: Response) {
let body = res.json();
return body || { };
}
private handleError(error: any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
}
}
Where is a problem, why is chart empty? How do I fill the chart with JSON data. JSON data must be in any specific format?
[ { Id: 1, Name: "Name1", ProductId: 2, ProductName: "ProductName1", StartMonth: "2016-01-01T00:00:00", EndMonth: "2016-01-01T00:00:00", Number1: 1, Number2: 2, ProductDetail: [ { Id: 101, OrderId: 1001, ProductionMonth: "2016-01-01T00:00:00", OrdersCount: 10, StorageCount: 1, ProductAll: null } ] }, ]
– Marko Aug 16 '16 at 12:12