vue+echarts 循环生成多个相同的图表

在vue项目中循环生成多个相同的echarts图表
效果图:
在这里插入图片描述
我直接封装成组件,代码如下:

<template>
  <!-- 实时数据图表 -->
  <div class="main-container">
    <el-row class="totalEchart">
      <el-col :span="8" :key="index" v-for="(item, index) in tmpList" class="tmplist">
        <img :src="item.path" alt="" />
        <span>{{ item.title }}</span>
        <el-col :span="8" v-for="(item, index) in list" :key="index"></el-col>
        <div class="roseChart"></div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
export default {
  name: "",
  components: {},
  props: {},
  data() {
    return {
      tmpList: [
        { id: 1, path: require("@/assets/images/jiance/jiance1.png"), title: "风速" },
        { id: 2, path: require("@/assets/images/jiance/jiance2.png"), title: "温度" },
        { id: 3, path: require("@/assets/images/jiance/jiance3.png"), title: "PM2.5" },
        { id: 4, path: require("@/assets/images/jiance/jiance4.png"), title: "风向" },
        { id: 5, path: require("@/assets/images/jiance/jiance5.png"), title: "噪音" },
        { id: 6, path: require("@/assets/images/jiance/jiance6.png"), title: "PM10" },
      ],
      list: [
        // 假数据
        {
          aData: [
            "1:00",
            "2:00",
            "3:00",
            "4:00",
            "5:00",
            "6:00",
            "7:00",
            "8:00",
            "9:00",
            "10:00",
            "11:00",
            "12:00",
            "13:00",
            "14:00",
            "15:00",
            "16:00",
            "17:00",
            "18:00",
            "19:00",
            "20:00",
            "21:00",
            "22:00",
            "23:00",
            "24:00",
          ],
          bData: [
            3.5,
            0,
            1,
            2.5,
            0.5,
            3.2,
            3.3,
            3,
            3.1,
            3.3,
            2,
            0.8,
            2.9,
            1.2,
            3.5,
            0,
            1,
            2.5,
            0.5,
            3.2,
            3.3,
            3,
            3.1,
            3.3,
            2,
            0.8,
            2.9,
            1.2,
          ],
          color: "#307EFF",
          colorLinearb:'#f0f6ff',
          colorLinearc:'#ddeaff',
          colorLineart:'#d6e5ff',
        },
          {
          aData: [
            "1:00",
            "2:00",
            "3:00",
            "4:00",
            "5:00",
            "6:00",
            "7:00",
            "8:00",
            "9:00",
            "10:00",
            "11:00",
            "12:00",
            "13:00",
            "14:00",
            "15:00",
            "16:00",
            "17:00",
            "18:00",
            "19:00",
            "20:00",
            "21:00",
            "22:00",
            "23:00",
            "24:00",
          ],
          bData: [
            3.5,
            0,
            1,
            2.5,
            0.5,
            3.2,
            3.3,
            3,
            3.1,
            3.3,
            2,
            0.8,
            2.9,
            1.2,
            3.5,
            0,
            1,
            2.5,
            0.5,
            3.2,
            3.3,
            3,
            3.1,
            3.3,
            2,
            0.8,
            2.9,
            1.2,
          ],
            color:'#00C0E9',
           colorLinearb:'#ecfafd',
          colorLinearc:'#d8f5fc',
          colorLineart:'#ccf2fb',
        },
         {
          aData: [
            "1:00",
            "2:00",
            "3:00",
            "4:00",
            "5:00",
            "6:00",
            "7:00",
            "8:00",
            "9:00",
            "10:00",
            "11:00",
            "12:00",
            "13:00",
            "14:00",
            "15:00",
            "16:00",
            "17:00",
            "18:00",
            "19:00",
            "20:00",
            "21:00",
            "22:00",
            "23:00",
            "24:00",
          ],
          bData: [
            3.5,
            0,
            1,
            2.5,
            0.5,
            3.2,
            3.3,
            3,
            3.1,
            3.3,
            2,
            0.8,
            2.9,
            1.2,
            3.5,
            0,
            1,
            2.5,
            0.5,
            3.2,
            3.3,
            3,
            3.1,
            3.3,
            2,
            0.8,
            2.9,
            1.2,
          ],
         color:'#F3BB5C', 
          colorLinearb:'#fefcf7',
          colorLinearc:'#fdf5e7',
          colorLineart:'#fdf1de',
        },
        {
          aData: [
            "1:00",
            "2:00",
            "3:00",
            "4:00",
            "5:00",
            "6:00",
            "7:00",
            "8:00",
            "9:00",
            "10:00",
            "11:00",
            "12:00",
            "13:00",
            "14:00",
            "15:00",
            "16:00",
            "17:00",
            "18:00",
            "19:00",
            "20:00",
            "21:00",
            "22:00",
            "23:00",
            "24:00",
          ],
          bData: [
            3.5,
            0,
            1,
            2.5,
            0.5,
            3.2,
            3.3,
            3,
            3.1,
            3.3,
            2,
            0.8,
            2.9,
            1.2,
            3.5,
            0,
            1,
            2.5,
            0.5,
            3.2,
            3.3,
            3,
            3.1,
            3.3,
            2,
            0.8,
            2.9,
            1.2,
          ],
        color:'#00C0E9',
           colorLinearb:'#ecfafd',
          colorLinearc:'#d8f5fc',
          colorLineart:'#ccf2fb',
        },
        {
          aData: [
            "1:00",
            "2:00",
            "3:00",
            "4:00",
            "5:00",
            "6:00",
            "7:00",
            "8:00",
            "9:00",
            "10:00",
            "11:00",
            "12:00",
            "13:00",
            "14:00",
            "15:00",
            "16:00",
            "17:00",
            "18:00",
            "19:00",
            "20:00",
            "21:00",
            "22:00",
            "23:00",
            "24:00",
          ],
          bData: [
            3.5,
            0,
            1,
            2.5,
            0.5,
            3.2,
            3.3,
            3,
            3.1,
            3.3,
            2,
            0.8,
            2.9,
            1.2,
            3.5,
            0,
            1,
            2.5,
            0.5,
            3.2,
            3.3,
            3,
            3.1,
            3.3,
            2,
            0.8,
            2.9,
            1.2,
          ],
           color:'#EE7173',
          colorLinearb:'#fefafa',
          colorLinearc:'#fdecec',
          colorLineart:'#fce3e3',
        },
        {
          aData: [
            "1:00",
            "2:00",
            "3:00",
            "4:00",
            "5:00",
            "6:00",
            "7:00",
            "8:00",
            "9:00",
            "10:00",
            "11:00",
            "12:00",
            "13:00",
            "14:00",
            "15:00",
            "16:00",
            "17:00",
            "18:00",
            "19:00",
            "20:00",
            "21:00",
            "22:00",
            "23:00",
            "24:00",
          ],
          bData: [
            3.5,
            0,
            1,
            2.5,
            0.5,
            3.2,
            3.3,
            3,
            3.1,
            3.3,
            2,
            0.8,
            2.9,
            1.2,
            3.5,
            0,
            1,
            2.5,
            0.5,
            3.2,
            3.3,
            3,
            3.1,
            3.3,
            2,
            0.8,
            2.9,
            1.2,
          ],
           color:'#A35CF3',
          colorLinearb:'#faf7fe',
          colorLinearc:'#f5ecfe',
          colorLineart:'#ede0fd',
        },
      ],
    };
    //     
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.drawRose();
  },
  methods: {
    // 遍历饼图
    drawRose() {
      var echarts = require("echarts");
      var roseCharts = document.getElementsByClassName("roseChart"); // 对应地使用ByClassName
      for (var i = 0; i < roseCharts.length; i++) {
        // 通过for循环,在相同class的dom内绘制元素
        var myChart = echarts.init(roseCharts[i]);
        myChart.setOption(
          {
            color: [this.list[i].color],
            grid: {
              top: 30,
              left: 20,
              right: 20,
              bottom: 10,
              containLabel: true,
            },
            tooltip: {
              show: true,
              trigger: "axis",
            },
            xAxis: {
              type: "category",
              //
              data: this.list[i].aData,//渲染每个图标对应的数据
              axisTick: {
                show: false,
              },
              boundaryGap: false,
              axisLabel: {
                show: true,
                color: "#858fa6", //x轴字体颜色
              },
              splitLine: {
                lineStyle: {
                  width: 1,
                  color: "#E2E2E2",
                },
              },
              axisLine: {
                lineStyle: {
                  width: 1,
                  color: "#E2E2E2", //x轴线颜色
                },
              },
            },
            yAxis: {
              type: "value",
              axisTick: {
                show: false,
              },
              axisLabel: {
                color: "#858fa6",
              },
              splitLine: {
                lineStyle: {
                  width: 1,
                  color: "#E2E2E2",
                },
              },
              axisLine: {
                lineStyle: {
                  width: 0,
                  color: "#E2E2E2",
                  show: false,
                },
              },
              max: 5,
            },
            series: [
              {
                name: "数量",
                type: "line",
                step: "start",

                //  smooth: true,
                symbolSize: 3,
                showSymbol: false,
                areaStyle: {
                  color: {
                    type: "linear",
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [
                      {
                        offset: 1,
                        color: this.list[i].colorLinearb, //从下向上,下部分的颜色
                      },
                      {
                        offset: 0.5,
                        color: this.list[i].colorLinearc, //从下向上,中间部分的颜色
                      },
                      {
                        offset: 0,
                        color: this.list[i].colorLineart, //从下向上,上部分的颜色
                      },
                    ],
                  },
                },
                data:this.list[i].bData,//渲染每个图标对应的数据
              },
            ],
          },
          true
        );
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.main-container {
  width: 100%;

  margin-top: 40px;
  .totalEchart {
    width: 100%;
    .tmplist {
      height: 310px;
      img {
        width: 42px;
        height: 42px;
        vertical-align: middle;
      }
      span {
        vertical-align: middle;
        margin-left: 10px;
      }
    }
    .roseChart {
      width: 530px;
      height: 250px;
    }
  }
}
</style>

  • 5
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
要实现动态绘制图表,可以使用Vue.js做前端框架,使用Echarts.js做图表库。下面是一个简单的实现过程: 1. 在Vue.js中安装Echarts.js。可以通过npm或者cdn的方式来安装。 2. 在Vue.js组件中引入Echarts.js,并初始化一个echarts实例。 3. 在Vue.js的data中定义一个数组,用来存储图表的数据。在mounted生命周期钩子中,设置一个定时器,每隔一段时间就往数据数组中添加一些数据。 4. 在Vue.js的methods中定义一个函数,用来更新图表。在这个函数中,使用echarts实例的setOption方法来更新图表数据。 5. 在Vue.js的template中,添加一个div元素作为图表容器,并设置宽高。在mounted生命周期钩子中,使用echarts实例的init方法初始化图表,并在更新图表数据时调用更新函数。 下面是一个简单的示例代码: ``` <template> <div id="chart" style="width: 600px; height: 400px;"></div> </template> <script> import echarts from 'echarts' export default { data () { return { chartData: [] } }, mounted () { this.chart = echarts.init(document.getElementById('chart')) this.timer = setInterval(() => { this.addData() this.updateChart() }, 1000) }, methods: { addData () { this.chartData.push(Math.random() * 100) }, updateChart () { this.chart.setOption({ xAxis: { type: 'category', data: Array.from({ length: this.chartData.length }, (_, i) => i) }, yAxis: { type: 'value' }, series: [{ data: this.chartData, type: 'line' }] }) } }, beforeDestroy () { clearInterval(this.timer) this.chart.dispose() } } </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值