uni-app进度条

<template>
	<view>
		<canvas canvas-id="ring" id="ring"  style="width: 200px; height: 180px;">
		        <!-- <p>抱歉,您的浏览器不支持canvas</p> -->
		</canvas>
	</view>
</template>

<script>
	export default{
		data() {
			return{
				
			}
		},
		props: {
			score: {
				type: Number
			}
		},
		mounted(){
			console.log( this.score,' this.score')
			setTimeout(co=>{
				this.circleProgressbar('ring', 200, 180, this.score)
			},500)
		},
		/**
		 * 组件的方法列表
		 */
		methods: {
			circleProgressbar(canvasId, width, height, angle) {
				var context = uni.createCanvasContext(canvasId, this);
				
				// var context = wx.createCanvasContext(canvasId, this)
				// 外层圆环
				context.beginPath()
				context.arc(width / 2, height - 80, width / 2 - 20, 0.8 * Math.PI, 2.2 * Math.PI)
				context.setLineWidth(16)
				context.setLineCap('round')
				context.setStrokeStyle('rgba(255,255,255,0.1)')
				context.stroke()
	
				// 外层进度圆环
				context.beginPath()
				context.arc(width / 2, height - 80, width / 2 - 20, 0.8 * Math.PI, (1.4 * (angle / 100) + 0.8).toFixed(1) * Math.PI)
				context.setLineWidth(10)
				context.setLineCap('round')
				context.setStrokeStyle('#fff')
				context.stroke()
	
				// 指示器
				const xAxis = Math.cos(Math.PI * ((1.4 * (angle / 100) + 0.8).toFixed(1) - 0.005)) * (width / 2 - 20)
				const yAxis = Math.sin(Math.PI * ((1.4 * (angle / 100) + 0.8).toFixed(1) - 0.005)) * (width / 2 - 20)
				context.beginPath()
				context.arc(width / 2 + xAxis, height - 80 + yAxis, 10, 0, 2 * Math.PI)
				context.setFillStyle('#fff')
				context.fill()
				// 指示器内环
				const xInAxis = Math.cos(Math.PI * ((1.4 * (angle / 100) + 0.8).toFixed(1) - 0.005)) * (width / 2 - 20)
				const yInAxis = Math.sin(Math.PI * ((1.4 * (angle / 100) + 0.8).toFixed(1) - 0.005)) * (width / 2 - 20)
				context.beginPath()
				context.arc(width / 2 + xAxis, height - 80 + yAxis, 6, 0, 2 * Math.PI)
				context.setFillStyle('#2A8FFF')
				context.fill()
	
				// 文本
				const textY = Math.sin(Math.PI * 2 / 360 * (1.8 * (100 + 15))) * (width / 2 - 20)
				context.beginPath()
				context.setFillStyle('#fff')
				context.setFontSize(20)
				context.setTextAlign('center')
				context.setTextBaseline('middle')
				context.fillText('匹配度', width / 2, height - 20)
				context.font = `normal bold ${40}px sans-serif`;
				context.fillText(angle+"%", width / 2, height - 50 + textY)
	
				context.draw()
			},
		}
	}
	
</script>

效果如下:

### 关于uni-app中实现圆形进度条uni-app开发过程中,可以利用Vue的组件化特性来创建自定义的圆形进度条组件。通过结合CSS样式和Canvas绘图功能,或者借助第三方图表库(如ECharts、UCharts),可以轻松实现圆形进度条的效果。 以下是两种常见的方法: #### 方法一:纯CSS与HTML实现 这种方法适用于简单的圆形进度条效果,适合不需要动态数据更新的场景。 ```html <template> <view class="progress-circle"> <view :style="{ '--percentage': percentage }" class="circle"></view> <text>{{ percentage }}%</text> </view> </template> <script> export default { data() { return { percentage: 75, // 进度百分比 }; }, }; </script> <style scoped> .progress-circle { position: relative; width: 100px; height: 100px; } .circle { --size: 100px; /* 圆形大小 */ --stroke-width: 10px; /* 边框宽度 */ --color-primary: #4caf50; /* 主颜色 */ --color-secondary: #e0e0e0; /* 背景颜色 */ position: absolute; top: 0; left: 0; width: var(--size); height: var(--size); background: conic-gradient(var(--color-primary) calc(var(--percentage) * 1%), var(--color-secondary) 0%); border-radius: 50%; } .text { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 16px; font-weight: bold; } </style> ``` 此代码片段展示了如何使用CSS变量`--percentage`控制圆环的颜色渐变,从而模拟进度变化[^1]。 --- #### 方法二:基于Canvas绘制 对于更复杂的交互需求,可以通过Canvas API手动绘制圆形进度条。 ```html <template> <view> <canvas canvas-id="progressCircle" id="progressCircle" style="width: 200px; height: 200px;"></canvas> </view> </template> <script> export default { onReady() { const ctx = uni.createCanvasContext('progressCircle'); this.drawProgress(ctx, 75); // 设置初始进度为75% }, methods: { drawProgress(ctx, progress) { const centerX = 100; const centerY = 100; const radius = 80; // 清除画布 ctx.clearRect(0, 0, 200, 200); // 绘制背景圆圈 ctx.setStrokeStyle('#e0e0e0'); // 背景色 ctx.setLineWidth(10); ctx.beginPath(); ctx.arc(centerX, centerY, radius, 0, Math.PI * 2, false); ctx.stroke(); // 绘制进度圆弧 ctx.strokeStyle = '#4caf50'; // 前景色 const endAngle = (Math.PI * 2 * progress) / 100; ctx.beginPath(); ctx.arc(centerX, centerY, radius, -Math.PI / 2, endAngle - Math.PI / 2, false); ctx.stroke(); // 展示当前进度文字 ctx.setFontSize(20); ctx.setFillStyle('#333'); ctx.setTextAlign('center'); ctx.fillText(`${progress}%`, centerX, centerY + 5); ctx.draw(); }, }, }; </script> ``` 这段代码实现了通过Canvas绘制一个带有动态文本显示的圆形进度条[^1]。 --- #### 使用第三方插件(推荐) 如果项目中有更多图形展示需求,建议引入成熟的图表库,例如 **UCharts** 或 **ECharts for Uni-app**。这些工具提供了丰富的API接口,能够快速构建高质量的可视化界面。 以下是一个基于UCharts的例子: ```javascript import uCharts from '@/components/u-charts/u-charts.js'; const _self = this; _self.chartData = { series: [ { name: '已完成', data: 75 }, // 已完成部分占比 { name: '未完成', data: 25 }, // 未完成部分占比 ], }; // 初始化图表 new uCharts({ context: _self, canvasId: 'progressChart', type: 'ring', legend: true, fontSize: 11, title: '', subtitle: '', extra: { ring: { activeBgColor: ['#4CAF50', '#FFC107'], borderWidth: 10, }, }, }); ``` 以上代码片段演示了如何配置UCharts生成一个饼状图形式的圆形进度条[^2]。 --- ### 总结 无论是通过原生CSS/Canvas还是依赖外部库的方式,在uni-app中都可以灵活实现圆形进度条的功能。具体选择取决于项目的复杂程度和个人偏好。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值