后端请求的数据放入echarts图表,数据不能直接放入需要经过处理,变成图表中需要的数据,今天也是请教同事写出来的。
<template>
// 定义图表
<div ref="main" :style="{width:'100%',height:'280px',}"></div>
</template>
<script>
methods:{
initbar(data) {
let chartsDom = this.$refs.main;
// 初始化echarts
let myChart = this.$myEcharts.init(chartsDom);
// console.log(myChart);
// 配置图表选项
let option = {
tooltip: {},
title: {
text: "单位(件)",
textStyle: {
color: "#5695DE",
},
},
// x轴
xAxis: {
type: "category",
// x轴是否显示
splitLine: {
show: true,
lineStyle: { type: "dashed", color: ["#5695DE"],},
},
data: data.xdatas,
},
// y轴
yAxis: {
// y轴是否显示
splitLine: {
show: true,
lineStyle: { type: "dashed", color: ["#5695DE"] },
},
},
series: [
{
type: "bar",
barWidth: 10,
// color: "#3398DB",
data:data.ydatas,
// showBackground:true,
// itemStyle: {
// },
},
],
};
// 指定图表选项 给 图表对象
myChart.setOption(option);
},
//这是从后端请求的数据
async EventType() {
const res = await EventTypeAPI(this.parameter);
console.log(res);
this.eventData = res.result;
// 定义两个数组 把请求的数据拆分出来,分别放入数组中 一个给x轴用 ,一个给y轴
var xdatas=[];
var ydatas=[];
for(var i=0;i<res.result.length;i++){
xdatas.push(res.result[i].typeName);
ydatas.push(res.result[i].value);
}
this.initbar({xdatas:xdatas,ydatas:ydatas});
console.log("事件分布类型", this.eventData);
// let [typeName,value] = this.eventData
// console.log('1111',typeName);
},
}
</script>