温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用vue-cli3+echarts实现渐变色仪表盘组件封装

发布时间:2022-03-28 09:08:05 阅读:427 作者:iii 栏目:开发技术
Vue开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

本篇内容主要讲解“怎么用vue-cli3+echarts实现渐变色仪表盘组件封装”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用vue-cli3+echarts实现渐变色仪表盘组件封装”吧!

效果预览

怎么用vue-cli3+echarts实现渐变色仪表盘组件封装

思路

1、使用两个仪表盘叠加,起始角度一样,底部仪表盘结束角度固定不变
2、通过props传入数据
3、计算在上层的仪表盘的结束角度并赋值

代码

<template>
  <div class="gauge">
    <div class="gauge__bottom" ref="bottomGauge"></div>
    <div class="gauge__top" ref="topGauge"></div>
    <div class="gauge__label">数据占比</div>
    <div class="gauge__title">{{ this.gaugeData.gaugeTitle }}</div>
  </div>
</template>

<script>
import echarts from "echarts";
export default {
  name"gauge",
  props: ["gaugeData"],//传入的数据
  data() {
    return {
      bottomOption: {
        series: [
          {
            name"",
            type"gauge",
            startAngle"225",
            endAngle"-45",
            data: [{ value100name"" }],
            splitNumber10,
            detail: {
              showfalse,
            },
            splitLine: {
              showfalse,
            },
            pointer: {
              showfalse,
            },
            axisTick: {
              showfalse,
            },
            axisLabel: { showfalse },
            axisLine: {
              lineStyle: {
                width10,
                color: [
                  [
                    1,
                    new echarts.graphic.LinearGradient(0010, [
                      {
                        offset0,
                        // 起始颜色
                        color"#707089",
                      },
                      {
                        offset1,
                        // 结束颜色
                        color"#707089",
                      },
                    ]),
                  ],
                ],
              },
            },
          },
        ],
      },
      topOption: {
        series: [
          {
            name"业务指标",
            type"gauge",
            startAngle"225",
            endAngle"",
            detail: {
              formatter"{value}%",
              color"#01F9FF",
              fontSize18,
              fontFamily"ZhenyanGB-Regular",
              offsetCenter: [00],
            },
            data: [{ value""name"" }],
            splitNumber10,
            splitLine: {
              showfalse,
            },
            pointer: {
              showfalse,
            },
            axisTick: {
              showfalse,
            },
            axisLabel: { showfalse },
            axisLine: {
              lineStyle: {
                width10,
                color"",
              },
            },
          },
        ],
      },
    };
  },
  mounted() {
    this.getTopGauge();
    this.getBottomGauge();
  },
  methods: {
    getTopGauge() {
      const chart = this.$refs.topGauge;
      if (chart) {
        const myChart = this.$echarts.init(chart, null, { renderer"svg" });
        this.$once("hook:beforeDestroy"function () {
          echarts.dispose(myChart);
        });
        this.topOption.series[0].data[0].value = this.gaugeData.gaugePercent;
        this.topOption.series[0].axisLine.lineStyle.color = this.gaugeData.guageColor;
        let tmp = 225 - 270 * (this.gaugeData.gaugePercent / 100);
        this.topOption.series[0].endAngle = tmp;
        const option = this.topOption;
        myChart.setOption(option);
      }
    },
    getBottomGauge() {
      const chart = this.$refs.bottomGauge;
      if (chart) {
        const myChart = this.$echarts.init(chart, null, { renderer"svg" });
        this.$once("hook:beforeDestroy"function () {
          echarts.dispose(myChart);
        });
        const option = this.bottomOption;
        myChart.setOption(option);
      }
    },
  },
};
</script>

<style lang="less">
.gauge {
  width150px;
  height165px;
  position: relative;
  &__top {
    position: absolute;
    top0;
    left0;
    width100%;
    height150px;
  }
  &__bottom {
    position: absolute;
    top0;
    left0;
    width100%;
    height150px;
  }
  &__label {
    position: absolute;
    height21px;
    width64px;
    background#0547c9;
    border1px solid #1e92ff;
    border-radius11.5px;
    border-radius11.5px;
    bottom35px;
    left50%;
    transformtranslate(-50%0);
    font-family: PingFangSC-Regular;
    font-size8px;
    color#ffffff;
    text-align: center;
    line-height21px;
  }
  &__title {
    font-family: PingFangSC-Medium;
    font-size14px;
    color#52f9cb;
    text-align: center;
    position: absolute;
    bottom5px;
    left50%;
    transformtranslate(-50%0);
  }
}
</style>

到此,相信大家对“怎么用vue-cli3+echarts实现渐变色仪表盘组件封装”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×