我试图根据Rest数据绘制一张线图,但是我得到了这个错误
core.js:1521 ERROR TypeError: res.price.map不是SafeSubscriber._next (app.component.ts:26)
这是代码
import { Component } from '@angular/core';
import { HelperService } from './helper.service';
import {Chart} from 'chart.js';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
chart = [];
constructor(private _graph:HelperService){
}
ngOnInit(){
this._graph.getGraphData()
.subscribe(res => {
console.log(res);
let stock_price0 = res[0].price.map(res => res[0].price);
let stock_price1 = res[1].price.map(res => res[1].price);
let timeloop=[1,2,3,4,5,6];
this.chart = new Chart('canvas',{
type:'line',
data: {
labels:timeloop,
datasets: [
{
data:stock_price0,
borderColor:'#3cba9f',
fill:false
},
{
data:stock_price1,
borderColor:'#ffcc00',
fill:false
}
]
},
options:{
legend:{
display:false
}
}
})
})
}
}这是json格式,
{"id":1,"stockName":"A",“价格”:5,"qty":10,“扇区”:“金融”},{"id":2,"stockName":"B",“价格”:3,"qty":2,“扇区”:“Tech”},{"id":3,"stockName":"C",“价格”:4,"qty":1,“部门”:“Gov”},{"id":4,"stockName":"Z“、”价格“:2、"qty":5、”扇区“:”金融“}、{"id":5、"stockName":"J”、“价格”:5、"qty":1、“扇区”:“Gov”}、{"id":6、"stockName":"K“、”价格“:3、"qty":3、”扇区“:”Tech“}
发布于 2018-06-23 17:38:05
如果您查看JSON,res.price不是一个数组。这只是个领域。映射工作在数组上。我觉得你需要
let stock_price0 = res[0].price;编辑
我认为你需要一个数组的价格值,
你能做到的
let stock_price0 = res[0].map(res => res.price);演示
var myjson = [
{
"id": 1,
"stockName": "A",
"price": 5,
"qty": 10,
"sector": "Finance"
},
{
"id": 2,
"stockName": "B",
"price": 3,
"qty": 2,
"sector": "Tech"
},
{
"id": 3,
"stockName": "C",
"price": 4,
"qty": 1,
"sector": "Gov"
},
{
"id": 4,
"stockName": "Z",
"price": 2,
"qty": 5,
"sector": "Finance"
},
{
"id": 5,
"stockName": "J",
"price": 5,
"qty": 1,
"sector": "Gov"
},
{
"id": 6,
"stockName": "K",
"price": 3,
"qty": 3,
"sector": "Tech"
}
];
var result = myjson.map(a => a.price);
console.log(result);
https://stackoverflow.com/questions/51003561
复制相似问题