自定义环形电量图

本文介绍了一个uniapp小程序中实现环形加载组件的代码示例,包括设置背景颜色和旋转角度以显示进度。在创建和改变样式方面,根据数据的百分比调整环形图的填充。同时,针对CSS变量(var)在uniapp小程序中可能不生效的问题进行了讨论,提出了这是一个待解决的技术挑战。
摘要由CSDN通过智能技术生成
<template>
  <div class="ring-data">
    <div class="loading">
      <div class="left" ref="left" :style="style"></div>
      <div class="right" ref="right" :style="style"></div>
      <div class="progress">{{ data.percentage }}%</div>
    </div>
    <div class="unit">{{ data.type }}</div>
    <div class="number" :style="textColor">{{ data.number }}</div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      style: {
        '--bg-color': '#1c9fe5',
        '--left': `rotateZ(180deg)`,
        '--right': `rotateZ(180deg)`
      }
    }
  },
  props: {
    data: {
      type: Object,
      default () {
        return {}
      },
      require: true
    }
  },
  created: function () {
    this.changeStyle()
  },
  methods: {
    changeStyle () {
      // 改变环形比例
      if (this.data.percentage < 50) {
        this.style['--left'] = `rotateZ(${180 -
          (this.data.percentage / 100) * 180}deg)`
      } else if (this.data.percentage === 50) {
        this.style['--left'] = `rotateZ(0deg)`
      } else if (this.data.percentage < 100) {
        this.style['--left'] = `rotateZ(0deg)`
        this.style['--right'] = `rotateZ(${180 -
          ((this.data.percentage - 50) / 100) * 180}deg)`
      } else if (this.data.percentage === 100) {
        this.style['--left'] = `rotateZ(0deg)`
        this.style['--right'] = `rotateZ(0deg)`
      }
      // 改变环形颜色
      if (this.data.percentage <= 10) {
        this.style['--bg-color'] = '#F55C5C'
      } else if (this.data.percentage > 10 && this.data.percentage <= 45) {
        this.style['--bg-color'] = '#FF9352'
      } else if (this.data.percentage > 45 && this.data.percentage <= 70) {
        this.style['--bg-color'] = '#6985F2'
      } else {
        this.style['--bgColor'] = '#49D999'
      }
    }
  },
  computed: {
     textColor() {
       if (this.data.type === '审计企业') {
         return { color: '#1c9fe5' }
      } else if (this.data.type === '询证企业') {
         return { color: '#17bc29' }
       } else if (this.data.type === '两者') {
        return { color: '#f9d11d' }
       }
     }
  }
}
</script>

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

  .loading {
    margin-top: 20px;
    // font-size: 10px;
  }

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

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

.loading {
  // margin: 100px auto;
  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(--bg-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);
}

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

效果如图:
在这里插入图片描述
但是uniapp的小程序暂未生效,考虑是css的var写法原因,暂未找到解决方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值