纯JS+Vue实现一个仪表盘

在使用canvas的时候发现数值变化,每次都要重新渲染,值都从0开始,这和我的需求冲突。

1. 先绘制基本的圆环背景,利用border-colorborder-radius将正方形变成基本的圆环。

javascript 复制代码
<div class="circle">
    <div class="Inner"></div>
</div>
css 复制代码
.circle {
    position: relative;
    border-radius: 50%;
    border: 12px solid;
    border-color: green green transparent green;
    width: 480px;
    height: 484px;
    top: 4%;
    left: 12%;
}

利用border-radius,就可将正方形变成圆形


2. 背景绘制完成,下面就是每个刻度。

javascript 复制代码
<div class="circle">
    <div class="Inner"></div>
    <div class="center"></div>
    <div class="pointer"></div>
</div>
css 复制代码
.center{
    width: 20px;
    height: 20px;
    background-color: grey;
    border-radius: 50%;
    position: absolute;
    z-index: 35;
    top: calc(50% - 5px);
    left: calc(50% - 5px);
}
.pointer{
    width: 190px;
    height:10px;
    background-color: red;
    border-radius: 50%;
    position: absolute;
    z-index: 34;
    top: calc(50% - -2px);
    left: calc(50% - 6px); 
    transform-origin:5% 35%;
}
.number {
    color: #fff;
    display: block;
    padding-top: 16px;
    position: absolute;
    left: -6px;
}

一共100个值,每两个刻度就要有线,到10线的长度会更长一点。其实和画钟表一样,0的位置是坐标轴的225°,到100的位置,总共是180°+45°

javascript 复制代码
mounted() {
	let circle = document.getElementsByClassName('Inner')[0];
	circle.style.setProperty('--width', Math.floor(227) + 'px');
	for (let i = 0; i <= 50; i++) {
	   const ul = document.createElement('ul');
	   const li = document.createElement('li');
	   li.style.transform = `rotate(${225 + i * 2 * 2.7}deg)`;
	   if (i % 5 === 0) {
	       li.style.height = '15px';
	       li.innerHTML = `<span class = 'number'>${i*2}</span>`
	   }
	   circle?.appendChild(ul);
	   ul.appendChild(li);
	}
}

3. 让指针根据数据变动,和刻度一样,每移动一个点要更改相应的刻度

javascript 复制代码
<div class="circle">
	<div class="Inner"></div>
	<div class="center"></div>
	<div class="pointer"></div>
	<div class="num">{{dataValue}}%</div>
</div>

<script>
export default {
    data() {
        return {
            dataValue: 50,
            }
         },
   watch: {
        dataValue: {
            handler(newValue, oldVal) {
                this.dataValue = newValue;
                this.runGuage()
            },
        },
    },
    methods: {
    	runGuage() {
            let pointer = document.getElementsByClassName("pointer")[0];
            pointer.style.transform = `rotate(${137 + this.dataValue * 2.66}deg)`;
        },
    },
    mounted() {
        this.runGuage();
    }
}
</script>   

<style scoped>
.num{
    position: absolute;
    color: #fff;
    font-size: 70px;
    z-index: 32;
    top: 54%;
    left: 30%;
}	
</style>
相关推荐
Better Rose12 分钟前
【2025“华中杯”大学生数学建模挑战赛】C题:就业状态分析与预测 详细解题思路
c语言·开发语言·数学建模
谁点的猪脚饭13 分钟前
vue2 element-ui 中 el-radio 单选框点击事件失效问题
vue.js·elementui·vue2
天天扭码17 分钟前
一分钟吃透一道面试算法题——字母异位词分组(最优解)
前端·javascript·算法
网络安全研发随想18 分钟前
C语言核心结构+难点精讲+工程技巧
c语言·开发语言·算法
天天扭码28 分钟前
JavaScript 中字符串转字符数组的两种优雅方式
前端·javascript·代码规范
superior tigre31 分钟前
C++学习:六个月从基础到就业——面向对象编程:虚函数与抽象类
开发语言·c++·学习
_一条咸鱼_35 分钟前
Vue 指令模块深度剖析:从基础应用到源码级解析(十二)
前端·javascript·面试
ademen37 分钟前
关于 IntelliJ IDEA 中频繁出现的 Kotlin 及其核心作用
java·开发语言·kotlin
苹果酱05671 小时前
redis系列--1.redis是什么
java·vue.js·spring boot·mysql·课程设计
kovlistudio1 小时前
红宝书第四十六讲:Node.js基础与API设计解析
前端·javascript·node.js