vue3之仪表盘

vue3之仪表盘

效果:

版本

"echarts": "^5.5.1"

核心代码:

javascript 复制代码
<!--
 * @Description: 圆环组件封装
 * @Version: 1.0
 * @Autor: qh
-->

<template>
  <div ref="chartRef" class="circle"></div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import * as echarts from 'echarts';

const props = defineProps({
  percent: {
    type: Number,
    default: 0,
  },
});

const chartRef = ref(null);
let chart: any = null;

const rich = {
  bule: {
    fontSize: 22,
    fontFamily: 'DINPro',
    color: '#fff',
    padding: [6, 0, 0, 0],
  },
  white: {
    fontSize: 12,
    color: '#fff',
    padding: [10, 0, 0, 0],
  },
};

const topChartOptions = (value: number) => {
  return {
    tooltip: {
      show: false,
      formatter: '{a} <br/>{b} : {c} ({d}%)',
    },
    series: [
      {
        name: 'S',
        z: 3,
        type: 'gauge', // 仪表盘
        center: ['49.5%', '58%'],
        radius: '95%',
        detail: {
          formatter: (value: any) => {
            return '{bule|' + value + '}{white|%}';
          },
          rich,
          offsetCenter: ['0%', '0%'],
        },
        title: {
          show: false,
        },
        data: [
          {
            value,
            name: 'Percent',
          },
        ],
        axisLine: {
          show: true,
          lineStyle: {
            roundCap: true,
            width: 7,
            color: [
              [
                value / 100,
                new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                  {
                    offset: 0,
                    color: 'rgba(0, 255, 231, 0.5)',
                  },
                  {
                    offset: 0.5,
                    color: 'rgba(0, 255, 231, 0.8)', // 100% 处的颜色
                  },
                  {
                    offset: 1,
                    color: 'rgba(0, 255, 231, 0.5)', // 100% 处的颜色
                  },
                ]),
              ],
              [1, 'rgba(4, 211, 251, 0)'],
            ],
          },
        },
        axisTick: {
          show: false,
        },
        axisLabel: {
          show: false,
        },
        splitLine: {
          show: false,
        },
        pointer: {
          length: '14%',
          width: 2,
          icon: 'rect',
          offsetCenter: [0, '-87%'],
          itemStyle: {
            color: 'rgba(0, 255, 231, 1)',
          },
        },
      },
      {
        name: 'T',
        z: 2,
        type: 'gauge', // 仪表盘
        center: ['49.5%', '58%'],
        radius: '96%',
        detail: {
          show: false,
        },
        title: {
          show: false,
        },
        data: [
          {
            value: 100,
            name: 'Percent',
          },
        ],
        axisLine: {
          show: true,
          lineStyle: {
            roundCap: true,
            width: 8,
            color: [
              [
                1,
                new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                  {
                    offset: 0,
                    color: 'rgba(150, 179, 180, 1)',
                  },
                  {
                    offset: value / 100,
                    color: 'rgba(150, 179, 180, 0.5)',
                  },
                  {
                    offset: 1,
                    color: 'rgba(150, 179, 180, 1)', // 100% 处的颜色
                  },
                ]),
              ],
              [1, 'rgba(255, 192, 1, 0)'],
            ],
          },
        },
        axisTick: {
          show: false,
        },
        axisLabel: {
          show: false,
        },
        splitLine: {
          show: false,
        },
        pointer: {
          show: false,
        },
      },
    ],
  };
};

const initChart = () => {
  if (!chart) chart = echarts.init(chartRef.value);
  chart.setOption(topChartOptions(props.percent));
};

onMounted(() => {
  initChart();
});

onBeforeUnmount(() => {
  if (chart) {
    chart.clear();
    chart.dispose();
    chart = null;
  }
});
</script>

<style scoped lang="scss">
.circle {
  width: 132px;
  height: 113px;
  // 根据实际情况加背景图
  background: url('@/assets/img/inspection/overview-bg.png') no-repeat;
}
</style>
相关推荐
雲墨款哥1 分钟前
一个前端开发者的救赎之路-JS基础回顾(三)-Function函数
前端·javascript
猩猩程序员1 分钟前
NAPI-RS v3:优化 Rust 与 前端 Node.js 跨平台支持
前端
艾小码1 分钟前
CSS粘性定位失效?深度解析 position: sticky 的陷阱与解决方案
前端·css
小徐_23333 分钟前
Trae 辅助下的 uni-app 跨端小程序工程化开发实践分享
前端·uni-app·trae
汪子熙4 分钟前
深入理解 TypeScript 的 /// <reference /> 注释及其用途
前端·javascript
全栈老石4 分钟前
设计师到前端不再有墙:Figma + VS Code 自动出码实践
前端·vue.js·html
GIS之路4 分钟前
GeoTools 结合 OpenLayers 实现叠加分析
前端
Nexmoe5 分钟前
前端新手常踩的坑:方法一改全站崩
前端
Spider_Man6 分钟前
面试官的 JS 继承陷阱,你能全身而退吗?🕳️
前端·javascript·面试
happyCoder7 分钟前
实现AI聊天-核心技术点详解
前端