自定义环形电量图uniapp可用

刚说完uniapp不可用,就惨遭打脸:注意驼峰的写法要用 kekbeb-case命名

<template>
	<view class="ring-data">
		<view class="loading">
			<view class="left" ref="left" :style="{'--background-color': backgroundColor,'--left-rote': leftRote,'--right-rote':rightRote}"></view>
			<view class="right" ref="right" :style="{'--background-color': backgroundColor,'--left-rote': leftRote,'--right-rote':rightRote}"></view>
			<view class="progress"><text style="font-size: 12px;">{{ data.percentage }}%</text></view>
		</view>
		<view class="unit"><text style="font-size: 12px;">{{ data.type }}</text></view>
	</view>
</template>

<script>
	export default {
		name: 'RingData',
		data() {
			return {
				backgroundColor: 'red',
				leftRote: 'rotateZ(180deg)',
				rightRote: 'rotateZ(180deg)'
			}
		},
		props: {
			data: {
				type: Object,
				default () {
					return {}
				},
				require: true
			}
		},
		created: function() {
			this.changeStyle()
		},
		methods: {
			changeStyle() {
				// 改变环形比例
				let rote = 0
				if (this.data.percentage < 50) {
					rote = 180 - (this.data.percentage / 100) * 360
					this.leftRote = 'rotateZ(' + rote  + 'deg)'
				} else if (this.data.percentage === 50) {
					this.leftRote = `rotateZ(0deg)`
				} else if (this.data.percentage > 50 && this.data.percentage < 100) {
					rote = Math.floor(180 - ((this.data.percentage - 50) / 100) * 360)
					this.leftRote = `rotateZ(0deg)`
					this.rightRote = 'rotateZ(' + rote + 'deg)'
				} else if (this.data.percentage === 100) {
					this.leftRote = 'rotateZ(0deg)'
					this.rightRote = 'rotateZ(0deg)'
				}
				// 改变环形颜色
				if (this.data.percentage <= 10) {
					this.backgroundColor = '#F55C5C'
				} else if (this.data.percentage > 10 && this.data.percentage <= 45) {
					this.backgroundColor = '#FF9352'
				} else if (this.data.percentage > 45 && this.data.percentage <= 70) {
					this.backgroundColor = '#6985F2'
				} else {
					this.backgroundColor = '#49D999'
				}
			}
		},
		computed: {
		}
	}
</script>

<style scoped lang="scss">
	.ring-data {
		font-size: 7px;
		display: flex;
		flex-direction: column;
		align-items: center;

		.loading {
			margin-top: 10px;
		}

		.unit {
			margin-top: 10px;
			color: #606266;
			text-align: center;
		}

		.number {
			margin-top: 10px;
			font-size: 24px;
			font-weight: bold;
			color: #1c9fe5;
			text-align: center;
		}
	}

	.loading {
		width: 8em;
		height: 8em;
		position: relative;
	}

	.loading .progress {
		position: absolute;
		width: 6em;
		height: 6em;
		font-weight: bold;
		background-color: white;
		border-radius: 50%;
		left: 1em;
		top: 1em;
		line-height: 6em;
		text-align: center;
	}

	.left,
	.right {
		width: 4em;
		height: 8em;
		overflow: hidden;
		position: relative;
		float: left;
		background-color: rgb(243, 241, 241);
	}

	.left {
		border-radius: 8em 0 0 8em;
	}

	.right {
		border-radius: 0 8em 8em 0;
	}

	.left:after,
	.right:after {
		content: '';
		position: absolute;
		display: block;
		width: 4em;
		height: 8em;
		background-color: white;
		border-radius: 8em 0 0 8em;
		background-color: var(--background-color);
	}

	.right:after {
		content: '';
		position: absolute;
		display: block;
		border-radius: 0 8em 8em 0;
	}

	.left:after {
		transform-origin: right center;
		// 0%-50%左侧
		transform: var(--left-rote);
	}

	.right:after {
		transform-origin: left center;
		// 50%-100%右侧
		transform: var(--right-rote);
	}
</style>



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,很高兴能够回答您的问题。针对您提出的问题,我可以给您一些提示和建议。 首先,环形 slide_menu 通常是一个圆形菜单,其中包含若干个选项。当用户点击菜单中的某个选项时,菜单会旋转并弹出详细信息或进入下一个页面。 实现环形 slide_menu 的基本思路是使用 Canvas 组件绘制一个圆形,并将菜单选项放置在圆形周围。然后,通过使用动画效果使菜单旋转并弹出详细信息或进入下一个页面。 以下是一个简单的示例代码,用于实现一个基本的环形 slide_menu: ``` <template> <div class="container"> <canvas ref="canvas" width="400" height="400"></canvas> <div class="options"> <div class="option" v-for="option in options" :key="option.id">{{ option.label }}</div> </div> </div> </template> <script> export default { data () { return { options: [ { id: 1, label: '选项1' }, { id: 2, label: '选项2' }, { id: 3, label: '选项3' }, { id: 4, label: '选项4' }, { id: 5, label: '选项5' } ] } }, mounted () { this.drawCircle() this.drawOptions() }, methods: { drawCircle () { const canvas = this.$refs.canvas const ctx = canvas.getContext('2d') const centerX = canvas.width / 2 const centerY = canvas.height / 2 const radius = 150 ctx.beginPath() ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI) ctx.stroke() ctx.closePath() }, drawOptions () { const canvas = this.$refs.canvas const options = this.options const ctx = canvas.getContext('2d') const centerX = canvas.width / 2 const centerY = canvas.height / 2 const radius = 150 const angleStep = (2 * Math.PI) / options.length const optionSize = options.length > 4 ? 30 : 40 // 控制option字体的大小 options.forEach((option, index) => { const angle = index * angleStep const x = centerX + Math.cos(angle) * radius const y = centerY + Math.sin(angle) * radius ctx.save() ctx.translate(x, y) ctx.beginPath() ctx.arc(0, 0, 5, 0, 2 * Math.PI) ctx.fill() ctx.closePath() ctx.font = `${optionSize}px Arial` ctx.textAlign = 'center' ctx.fillText(option.label, 0, 25) ctx.restore() }) } } } ``` 这是实现一个环形 slide_menu 的基础代码,您可以根据自己的需求进一步完善该组件的功能和效果。 希望这些提示和建议能够对您有所帮助。如果您还有其他问题,请继续向我提出,我会尽力回答和帮助您。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值