这是我的service.ts
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import { Headers } from '@angular/http';
@Injectable()
export class BarserviceService {
public http: Http;
people:any;
constructor(http: Http) {
this.http = http;
}
get() {
// return this.mediaItems;
let charts = this.http.get("http://localhost:3000/charts")
.map(response => { this.people = response.json()
console.log("pople", this.people)
});
return charts;
}
这是我的barchart.component.ts
ngOnInit() {
// this.getEarthquakes();
this.options = {
chart: {
type: 'discreteBarChart',
height: 450,
margin : {
top: 20,
right: 20,
bottom: 50,
left: 55
},
x: function(d){return d.label;},
y: function(d){return d.value;},
showValues: true,
valueFormat: function(d){
return d3.format(',.4f')(d);
},
duration: 500,
xAxis: {
axisLabel: 'X Axis'
},
yAxis: {
axisLabel: 'Y Axis',
axisLabelDistance: -10
}
}
}
this.data =
[{
values: [
]
}
]
this.baroneService.get().subscribe(c => {
// console.log("pep",this.people);
// this.media = c.charts
console.log( "media",this.media = c.charts)
this.data = this.media
console.log("pep",this.data);});
这将是我的 json
{
"charts": [
{
"values": [
{
"label": "A",
"value": -29.765957771107
},
{
"label": "B",
"value": 0
} ]} ]}
我在 service.ts 文件中的控制台 pople 中获取 json 数据,但我不明白如何在 component.ts 文件中的 this.data 中获取它,因此我可以通过 json 数据显示图表,并且在 console.log 中出现错误(“ media",this.media = c.charts) 在 component.ts 中。任何人都可以帮我解决这个问题。