完整示例
<template>
<div>
<div class="list">
<div class="list_item" v-for="(item, index) in listData" :key="index">
<div class="echart" :id="item.id"></div>
</div>
</div>
</div>
</template>
<script>
import * as echarts from 'echarts';
export default {
data() {
return {
listData: [
{
id: 'chart1',
chartData: [1, 2, 3]
},
{
id: 'chart2',
chartData: [2, 3, 4]
}
]
}
},
mounted() {
this.getEahcrts()
},
methods: {
getEahcrts() {
this.listData.forEach(elem => {
const chartDom = document.getElementById(elem.id)
const myChart = echarts.init(chartDom)
const option = {
grid: {
left: "15%",
right: "4%",
bottom: "15%",
},
xAxis: [
{
type: "category",
boundaryGap: false,
axisLine: {
lineStyle: {
color: "#57617B",
},
},
data: [
"test",
"test",
"test",
],
},
],
yAxis: [
{
type: "value",
name: elem.id,
},
],
series: [
{
name: '',
type: "line",
smooth: true,
symbol: "circle",
data: elem.chartData,
},
],
};
myChart.setOption(option)
})
}
}
}
</script>
<style lang="less">
.list {
width: 1000px;
height: 500px;
display: flex;
justify-content: space-between;
.list_item {
width: 40%;
height: 100%;
.echart {
width: 100%;
height: 100%;
}
}
}
</style>
运行结果: