vue 封装水球图

1、 安装 echarts 与 echarts-liquidfill

pnpm i echarts
pnpm i echarts-liquidfill

2、组件中引入

import * as echarts from 'echarts'
import 'echarts-liquidfill'

3、封装通用组件

    <div class="waterball-chart">
      <div ref="chartContainer" style="width: 100%; height: 90%"></div>
    </div>
import { ref, onMounted, watch } from 'vue'
import * as echarts from 'echarts'
import 'echarts-liquidfill'
const props = defineProps<{
  borderColor: any
  titleInfo: any
  percentage: any
  color: any
  labelNumber: any
  percentSize: any
  unitSize: any
  padding: any
}>()
const chartContainer = ref(null)
// ----------------------------------
const drawWaterball = (percentage: any) => {
  const chart = echarts.init(chartContainer.value)
  const option = {
    series: [
      {
        type: 'liquidFill',
        data: [percentage / 100], // 百分比的值,取值范围为0到1
        color: [props.color],
        radius: '85%', // 水球图的半径,可以根据需要调整
        label: {
          formatter(param: any) {
            return [
              `{a|${props.labelNumber || (param.value * 100).toFixed(0)}}`,
              '{b|%}',
            ].join('')
          },
          rich: {
            a: {
              fontSize: props.percentSize,
              color: '#FFFFFF',
              fontFamily: 'DINPro',
              fontWeight: 400,
            },
            b: {
              fontSize: props.unitSize,
              color: '#FFFFFF',
              fontFamily: 'DINPro-Regular',
              fontWeight: 400,
              padding: props.padding,
            },
          },
        },
        title: {
          text: `${(0.2 * 100).toFixed(0)}{a|%}`,
          textStyle: {
            fontSize: 12,
            fontFamily: 'Microsoft Yahei',
            fontWeight: 'normal',
            color: '#bcb8fb',
            rich: {
              a: {
                fontSize: 10,
              },
            },
          },
          x: 'center',
          y: '35%',
        },

        backgroundStyle: {
          color: {
            type: 'radial',
            x: 0.5,
            y: 0.5,
            r: 0.8,
            colorStops: [
              {
                offset: 0,
                color: 'rgba(204, 230, 255,1)', // 0% 处的颜色
              },
              {
                offset: 0.5,
                color: 'rgba(204, 230, 255, 1)', // 0% 处的颜色
              },
              {
                offset: 1,
                color: 'rgba(204, 230, 255, 1)', // 100% 处的颜色
              },
            ],
            globalCoord: false, // 缺省为 false
          },
        },

        outline: {
          // show:false,
          borderDistance: 0,
          itemStyle: {
            borderWidth: 10,
            borderColor: props.borderColor,
          },
        },
      },
    ],
  }
  setTimeout(() => {
    chart.resize()
    chart.setOption(option)
  }, 8)
}
// ---------------------------------
watch(
  () => props.percentage,
  (newValue) => {
    drawWaterball(newValue)
  },
)
// ---------------------------------
onMounted(() => {
  drawWaterball(props.percentage)
})

4、效果图

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-09 16:34:02       5 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-09 16:34:02       5 阅读
  3. 在Django里面运行非项目文件

    2024-06-09 16:34:02       4 阅读
  4. Python语言-面向对象

    2024-06-09 16:34:02       6 阅读

热门阅读

  1. 力扣第185题:部门工资前三高的员工

    2024-06-09 16:34:02       13 阅读
  2. 手机UI设计中的按钮状态包含哪几种

    2024-06-09 16:34:02       13 阅读
  3. 等级保护与网络安全:构建信息安全的坚实防线

    2024-06-09 16:34:02       17 阅读
  4. Haproxy 搭建 web 集群

    2024-06-09 16:34:02       16 阅读
  5. 基于单片机的电流检测装置

    2024-06-09 16:34:02       14 阅读
  6. TypeScript常见面试题第九节

    2024-06-09 16:34:02       14 阅读
  7. Docker面试整理-什么是Docker Hub?

    2024-06-09 16:34:02       14 阅读
  8. 每天学习一个Windows命令或Linux命令——seq

    2024-06-09 16:34:02       15 阅读