uniapp,自绘仪表盘组件(基础篇)

文章目录

一、为什么需要自绘仪表盘?

在物联网、数据监控等场景中,仪表盘是常见的数据可视化组件。uniapp的组件市场虽然有许多现成方案,但自绘组件具有以下优势:

  • 完全掌控视觉效果
  • 无依赖零冗余
  • 高性能Canvas渲染
  • 轻松适配多端

二、准备知识

  • 基础Canvas绘图API
  • uni-app的Canvas组件使用
  • 三角函数基础
  • Vue组件开发基础

三、实现基础仪表盘

1. 组件模板结构

html 复制代码
<template>
  <view class="gauge-container">
    <canvas 
      canvas-id="gaugeCanvas"
      :style="{ width: canvasSize + 'px', height: canvasSize + 'px' }"
    ></canvas>
    <view class="value-text">{{ currentValue }}</view>
  </view>
</template>

2. 核心绘制逻辑

javascript 复制代码
export default {
  props: {
    value: { type: Number, default: 0 },      // 当前值
    max: { type: Number, default: 100 },     // 最大值
    min: { type: Number, default: 0 },       // 最小值
    size: { type: Number, default: 300 }     // 画布尺寸
  },
  
  mounted() {
    this.initCanvas();
  },

  methods: {
    initCanvas() {
      this.ctx = uni.createCanvasContext('gaugeCanvas', this);
      this.drawBase();
      this.drawPointer();
    },

    // 绘制底盘
    drawBase() {
      const center = this.canvasSize / 2;
      const radius = center * 0.8;
      
      // 外圆环
      this.ctx.beginPath();
      this.ctx.arc(center, center, radius, 0.75 * Math.PI, 2.25 * Math.PI);
      this.ctx.strokeStyle = '#eee';
      this.ctx.lineWidth = 8;
      this.ctx.stroke();

      // 刻度线
      const totalTicks = 20;
      for (let i = 0; i <= totalTicks; i++) {
        this.ctx.save();
        this.ctx.translate(center, center);
        const angle = 0.75 * Math.PI + (i / totalTicks) * 1.5 * Math.PI;
        this.ctx.rotate(angle);
        
        // 长刻度
        this.ctx.beginPath();
        this.ctx.moveTo(radius - 15, 0);
        this.ctx.lineTo(radius, 0);
        this.ctx.strokeStyle = i % 5 === 0 ? '#333' : '#999';
        this.ctx.lineWidth = i % 5 === 0 ? 3 : 1;
        this.ctx.stroke();
        
        this.ctx.restore();
      }
    },

    // 绘制指针
    drawPointer() {
      const center = this.canvasSize / 2;
      const valueAngle = this.getCurrentAngle();
      
      this.ctx.save();
      this.ctx.translate(center, center);
      this.ctx.rotate(valueAngle);

      // 指针三角形
      this.ctx.beginPath();
      this.ctx.moveTo(-8, 0);
      this.ctx.lineTo(0, -center * 0.7);
      this.ctx.lineTo(8, 0);
      this.ctx.fillStyle = '#e64340';
      this.ctx.fill();

      // 中心圆点
      this.ctx.beginPath();
      this.ctx.arc(0, 0, 5, 0, 2 * Math.PI);
      this.ctx.fillStyle = '#333';
      this.ctx.fill();

      this.ctx.restore();
      this.ctx.draw();
    },

    getCurrentAngle() {
      const range = this.max - this.min;
      const progress = (this.value - this.min) / range;
      return 0.75 * Math.PI + progress * 1.5 * Math.PI;
    }
  }
}

3. 样式优化

css 复制代码
.gauge-container {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}

.value-text {
  position: absolute;
  font-size: 24px;
  font-weight: bold;
  color: #333;
}

四、使用示例

html 复制代码
<template>
  <view class="container">
    <Gauge :value="75" :max="100" size="300" />
  </view>
</template>

<script>
import Gauge from '@/components/gauge.vue'

export default {
  components: {
    Gauge
  }
}
</script>

五、核心实现原理

角度计算:将数值映射到135°~315°的扇形角度(0.75π ~ 2.25π)

坐标系变换:通过translate和rotate实现指针旋转

分层绘制:先绘制静态元素(底盘),再绘制动态元素(指针)

性能优化:使用Canvas的save/restore管理绘图状态


六、扩展方向

添加动画效果(使用requestAnimationFrame)

绘制渐变颜色区间

添加触摸交互

实现双指针仪表

添加数字标签


七、常见问题

模糊问题:确保canvas尺寸与样式尺寸一致

层级问题:数值文本需要绝对定位覆盖在canvas上

单位转换:使用uni.upx2px处理不同屏幕适配

多次绘制:在修改数据后需要手动调用draw方法

通过这个基础实现,开发者可以快速掌握uniapp中Canvas组件的使用技巧,后续可根据具体需求进行样式定制和功能扩展。这种自绘方案在H5和小程序端均可获得良好的性能表现。

相关推荐
Mintopia8 小时前
Vue3 项目如何迁移到 uni-app x:从纯 Web 到多端应用的系统指南
uni-app
Mintopia8 小时前
uni-app x 发展前景技术分析:跨端统一的新阶段?
uni-app
不爱说话郭德纲1 天前
告别漫长的HbuilderX云打包排队!uni-app x 安卓本地打包保姆级教程(附白屏、包体积过大排坑指南)
android·前端·uni-app
HashTang2 天前
【AI 编程实战】第 12 篇:从 0 到 1 的回顾 - 项目总结与 AI 协作心得
前端·uni-app·ai编程
JunjunZ2 天前
uniapp 文件预览:从文件流到多格式预览的完整实现
前端·uni-app
郑州光合科技余经理3 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
TT_Close3 天前
“啪啪啪”三下键盘,极速拉起你的 uni-app 项目!
vue.js·uni-app·前端工程化
特立独行的猫a3 天前
uni-app x跨平台开发实战:开发鸿蒙HarmonyOS影视票房榜组件完整实现过程
华为·uni-app·harmonyos·轮播图·uniapp-x
00后整顿职场3 天前
Hbuilderx APP真机无法识别iqoo Z9+手机设备解决方案
uni-app·uniapp真机调试·真机运行
前端小雪的博客.4 天前
【保姆级教程】uniAI 插件高效开发 uni-app 微信小程序(附实战案例)
微信小程序·uni-app·ai编程·uniai