纯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>
相关推荐
冬奇Lab9 分钟前
【Kotlin系列11】协程原理与实战(下):Flow与Channel驯服异步数据流
android·开发语言·kotlin
好大哥呀12 分钟前
如何在手机上运行Python程序
开发语言·python·智能手机
阿蒙Amon13 分钟前
C#每日面试题-is和as的区别
java·开发语言·c#
毕设源码-钟学长14 分钟前
【开题答辩全过程】以 基于Python的新闻热点舆情分析系统为例,包含答辩的问题和答案
开发语言·python
XerCis15 分钟前
Python代码检查与格式化工具Ruff
开发语言·python
少控科技21 分钟前
QT高阶日记010
开发语言·qt
秦jh_23 分钟前
【Qt】界面优化
开发语言·qt
阿蒙Amon26 分钟前
C#每日面试题-简述泛型约束
java·开发语言·c#
zh_xuan27 分钟前
kotlin 延迟属性
开发语言·kotlin
进击的小头36 分钟前
创建型模式:简单工厂模式(C语言实现)
c语言·开发语言·简单工厂模式