VUE生成多个ECharts图

效果图:
在这里插入图片描述

HTML:

<div class="chart-dis-area" style="display: flex; background-color:#0B1837 ;">
  <div v-for="(item,index) in chartList" class="chart-item-area" style="width: 20%;">
    <div class="echartsList" :id="item.id" style="height: 225px;"></div>
  </div>
</div>

JS:

// 引入基本模板
  let echarts = require('echarts/lib/echarts')
  // 引入柱状图等组件
  require('echarts/lib/chart/bar')
  require('echarts/lib/chart/line')
  require('echarts/lib/chart/pie')
  require('echarts/lib/chart/gauge')
  // 引入提示框和title等组件
  require('echarts/lib/component/tooltip')
  require('echarts/lib/component/title')
  require('echarts/lib/component/legend')
  require('echarts/lib/component/graphic')
  //引入水球图组件
  import echartsLiquidfill from 'echarts-liquidfill'
  export default {
    data() {
      return {
        chartList: [],    //
        HardDisklength: 0 //获取硬盘数组的数量
      }
    },
    mounted() {
      this.init();
    },
    methods: {
      init() { //生成对应数量的硬盘echarts图
        var data = [{"a": 68,"b": "C", {"a": 12,"b": "D"}, {"a": 5,"b": "E"}, {"a": 20,"b": "F"}]
        var alist = [];
        var blist = [];
        let arr = [];
        this.HardDisklength = data.length;for (let i = 0; i < this.HardDisklength; i++) {
          let item = {
            barChart: '', // chart 对象实例
            id: 'id' + i, // 为了标示 id 不同
          }
          arr.push(item);
          alist.push(data[i].a);
          blist.push(data[i].b);
        }
        console.log(arr);
        this.chartList = arr;
        this.$nextTick(() => {
          for (let i = 0; i < this.chartList.length; i++) {
            this.chartList[i].barChart = echarts.init(document.getElementById(this.chartList[i].id));
            window.onresize = this.chartList[i].barChart.resize;
            this.chartList[i].barChart.setOption(this.HardDiskOption(alist[i], blist[i])); //设置option
          }
        })
      },
      //已使用率    盘符
      HardDiskOption(a, b) {
        return  {
          backgroundColor: "#0B1837",
          title: {
            text: a + '%',
            subtext: b + '盘',
            subtextStyle: {
              fontSize: '15'
            },
            x: 'center',
            y: 'center',
            textStyle: {
              fontWeight: 'normal',
              color: '#0580f2',
              fontSize: '30'
            }
          },
          color: ['rgba(176, 212, 251, 1)'],
          series: [{
            name: 'Line 1',
            type: 'pie',
            clockWise: true,
            radius: ['50%', '66%'],
            itemStyle: {
              normal: {
                label: {
                  show: false
                },
                labelLine: {
                  show: false
                }
              }
            },
            hoverAnimation: false,
            data: [{
              value: a,
              name: '01',
              itemStyle: {
                normal: {
                  color: { // 完成的圆环的颜色
                    colorStops: [{
                      offset: 0,
                      color: '#00cefc' // 0% 处的颜色
                    }, {
                      offset: 1,
                      color: '#367bec' // 100% 处的颜色
                    }]
                  },
                  label: {
                    show: false
                  },
                  labelLine: {
                    show: false
                  }
                }
              }
            }, {
              name: '02',
              value: 100-a
            }]
          }]
        }
      },
    }
  }
要在 Vue 中使用 Echarts 生成多个水球,你需要先安装 Echarts: ``` npm install echarts --save ``` 然后在 Vue 组件中引入 Echarts: ``` import echarts from 'echarts' ``` 接下来,你需要创建一个包含多个水球的容器,在 Vue 的 `mounted` 钩子函数中使用 Echarts 渲染水球: ``` <template> <div class="container"> <div class="chart" ref="chart1"></div> <div class="chart" ref="chart2"></div> <!-- 其他水球的容器 --> </div> </template> <script> import echarts from 'echarts' export default { mounted() { // 使用 Echarts 渲染水球 this.renderChart(this.$refs.chart1, 50) // 第一个水球,进度为 50% this.renderChart(this.$refs.chart2, 80) // 第二个水球,进度为 80% // 渲染其他水球 }, methods: { renderChart(container, percent) { const chart = echarts.init(container) chart.setOption({ series: [ { type: 'liquidFill', data: [percent / 100], color: ['#00a6ff', '#71c9ff', '#c7e1ff'], // 其他配置项 } ] }) } } } </script> ``` 在这个例子中,我们创建多个水球的容器,并在 `mounted` 钩子函数中使用 `renderChart` 方法来渲染每个水球。在 `renderChart` 方法中,我们使用 Echarts 提供的 `liquidFill` 类型来绘制水球,通过传递不同的进度值来生成多个水球。 当然,你还可以根据自己的需求来调整代码,比如使用 Vue 组件来封装水球,或者将数据传递给组件来动态生成水球等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值