纯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>
相关推荐
Q_Q51100828523 分钟前
python的婚纱影楼管理系统
开发语言·spring boot·python·django·flask·node.js·php
EutoCool35 分钟前
Qt窗口:菜单栏
开发语言·c++·嵌入式硬件·qt·前端框架
nightunderblackcat1 小时前
新手向:使用Python将多种图像格式统一转换为JPG
开发语言·python
我爱Jack1 小时前
深入解析 LinkedList
java·开发语言
engchina2 小时前
Python PDF处理库深度对比:PyMuPDF、pypdfium2、pdfplumber、pdfminer的关系与区别
开发语言·python·pdf
拓端研究室2 小时前
专题:2025供应链数智化与效率提升报告|附100+份报告PDF、原数据表汇总下载
开发语言·php
浮桥2 小时前
vue3实现pdf文件预览 - vue-pdf-embed
前端·vue.js·pdf
AA-代码批发V哥2 小时前
Vue框架之钩子函数详解
vue.js
一百天成为python专家2 小时前
python库之jieba 库
开发语言·人工智能·python·深度学习·机器学习·pycharm·python3.11
四季豆豆豆2 小时前
博客项目 laravel vue mysql 第四章 分类功能
vue.js·mysql·laravel