首页 前端知识 for循环生成多个echarts图表

for循环生成多个echarts图表

2024-08-07 00:08:30 前端知识 前端哥 429 845 我要收藏

在这里插入图片描述
在这里插入图片描述
完整示例

<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>

运行结果:
在这里插入图片描述

转载请注明出处或者链接地址:https://www.qianduange.cn//article/14953.html
标签
评论
发布的文章

前端-axios应用在html文件

2024-08-15 23:08:39

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!