//假设这是后端返回的数据结构
{
"code": 0,
"msg": "OK",
"result":
[{
"count": 1,
"name": "其他"
}, {
"count": 38,
"name": "平台登记"
}, {
"count": 97,
"name": "微信上报"
}, {
"count": 1,
"name": "牛皮癣"
}, {
"count": 87196,
"name": "监督员上报"
}, {
"count": 4705,
"name": "视频上报"
}]
}
methods:{
//接口请求方法
search() {
getLYFXData({ timeType: this.timeType }).then((res) => {
this.amount = res.result;
this.drawSource();
});
},
//echarts初始化
drawSource() {
let myChart = this.$echarts.initdocument.getElementById("echarts_source"));
let arr = [];
this.amount.forEach((element) => {
arr.push({
value: element.count, //将数组的conut值赋给value
name: element.name, //将数组的name值赋给name
});
});
// 绘制图表
myChart.setOption({
tooltip: {
trigger: "item",
formatter: "{b}: {c} ({d}%)",
},
series: [
{
type: "pie",
radius: ["50%", "70%"],
center: ["50%", "50%"],
//在此赋值即可
data: arr,
......
}
}
}
-
forEach() 方法用于调用数组的每个元素,并将元素传递给回调函数。注意: forEach() 对于空数组是不会执行回调函数的。
-
push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度。注意:新元素将添加在数组的末尾。此方法改变数组的长度。