我在用vue2和echarts组件展示数据,后端返回的数据是这样的:[ { "server": "172.28.35.50", "monitorDate": "2023-05-22 00:00:00", "name": "QUEUE_INVSEGINFO_OUT_CHANGE", "enqueueCount": 4374 }, { "server": "172.28.35.50", "monitorDate": "2023-05-22 00:00:00", "name": "QUEUE_EtermAV", "enqueueCount": 153763 }, { "server": "172.28.35.50", "monitorDate": "2023-05-22 00:00:00", "name": "QUEUE_paysend", "enqueueCount": 1620 }, { "server": "172.28.35.50", "monitorDate": "2023-05-22 00:00:00", "name": "QUEUE_ORDHNL", "enqueueCount": 0 }, { "server": "172.28.35.50", "monitorDate": "2023-05-22 00:00:00", "name": "QUEUE_ticketInfoAll_test", "enqueueCount": 0 }],我需要用图来展示数据,其中monitordate为横轴值,enqueuecount为纵轴值,,不同的name对应的横轴纵轴值我要做出不同的折线,那么作图部分的代码应该怎么写
时间: 2023-07-02 09:21:45 浏览: 119
在vue中使用echarts(折线图的demo,markline用法)
你可以先对数据进行处理,将不同的name对应的数据分开,然后再使用echarts的折线图来展示数据。下面是一个简单的例子:
```
<template>
<div class="chart-container">
<div ref="chart" class="chart"></div>
</div>
</template>
<script>
import * as echarts from 'echarts';
export default {
data() {
return {
chartData: [],
chartNames: [],
chartOptions: {},
};
},
mounted() {
// 获取数据并处理
this.getChartData().then((data) => {
this.chartData = data.reduce((prev, curr) => {
prev[curr.name] = prev[curr.name] || [];
prev[curr.name].push([curr.monitorDate, curr.enqueueCount]);
return prev;
}, {});
this.chartNames = Object.keys(this.chartData);
this.initChart();
});
},
methods: {
async getChartData() {
// 获取后端数据
const response = await fetch('your_api_url');
const data = await response.json();
return data;
},
initChart() {
this.chartOptions = {
tooltip: {
trigger: 'axis',
},
legend: {
data: this.chartNames,
},
xAxis: {
type: 'time',
splitLine: {
show: false,
},
},
yAxis: {
type: 'value',
splitLine: {
show: false,
},
},
series: [],
};
this.chartNames.forEach((name) => {
this.chartOptions.series.push({
name,
type: 'line',
data: this.chartData[name],
});
});
const chart = echarts.init(this.$refs.chart);
chart.setOption(this.chartOptions);
},
},
};
</script>
<style scoped>
.chart-container {
height: 500px;
}
.chart {
height: 100%;
}
</style>
```
在这个例子中,我们使用了echarts的折线图,将不同的name对应的数据分开处理,并在图表中展示。你需要将代码中的your_api_url替换为你自己的后端API地址,以获取数据。
阅读全文