vue自定义圆

2 篇文章 0 订阅

这里写自定义目录标题


```html
<template>
  <x-view style="width: inherit;text-align: center;">
    <svg id="circle" viewBox="0 0 188 188">
      <circle cx="94" cy="94" r="90" fill="none" :stroke-width="svgStrokeWidth" :stroke="inColor" />
      <circle stroke-linecap="round" cx="106" cy="94" r="90" :stroke-width="svgStrokeWidth" :stroke="ouColor"
        fill="none" transform="matrix(0,-1,1,0,0,200)" stroke-dasharray="0 1069"></circle>
      <text text-anchor="middle" dominant-baseline="middle" id="circle-text1" x="94" y="94" :fill="text1Color"
        style="font-size: 20px;">{{text1}}</text>
      <text text-anchor="middle" dominant-baseline="middle" id="circle-text2" x="94" y="114" :fill="text2Color"
        style="font-size:12px;;opacity: 0.75;">{{text2}}</text>
    </svg>
  </x-view>
</template>
<script>
export default {
  name: 'cqCircle',
  data() {
    return {
      per: this.percent,
      text1: this.title,
      text2: this.subtitle,
      text1Color: this.titleColor,
      text2Color: this.subTitleColor,
      inColor: this.innerColor,
      ouColor: this.outerColor,
      svgWidth: this.width,
      svgHeight: this.height,
      svgStrokeWidth: this.strokeWidth
    }
  },
  props: {
    percent: {
      type: Number,
      default: 0.5
    },
    strokeWidth: {
      type: Number,
      default: 5
    },
    title: {
      type: String,
      default: ""
    },
    subtitle: {
      type: String,
      default: ""
    },
    width: [String, Number],
    height: [String, Number],
    titleColor: {
      type: String,
      default: ""
    },
    subTitleColor: {
      type: String,
      default: ""
    },
    innerColor: {
      type: String,
      default: "#e5e9f2"
    },
    outerColor: {
      type: String,
      default: "#20a0ff"
    }
  },
  methods: {
    //初始化
    init() {
      let percent = this.per  //从接口获取到的圆环填充百分比
      let svg = document.getElementById("circle");
      let text1 = document.getElementById("circle-text1");
      let text2 = document.getElementById("circle-text2");
      let circle0 = document.querySelectorAll("circle")[0];
      let circle1 = document.querySelectorAll("circle")[1];
      let perimeter = circle1.getTotalLength() //圆环的周长
      circle1.setAttribute('stroke-dasharray', perimeter * percent + " " + perimeter * (1 - percent));
      //  svg.style.width = 'inherit';
      //  svg.style.width = 188 + 'px';
      // text1.style.fontSize = 12 + 'px'
      // text2.style.fontSize = 9 + 'px'
    },
    //设置内圈颜色
    setInnerColor(color) {
      circle0.setAttribute("stroke", color);
    },
    //设置外圈颜色
    setOuterColor(color) {
      circle1.setAttribute("stroke", color);
    };
    //设置顶部字体颜色
    setTopColor(color) {
      text1.setAttribute("fill", color);
    },
    //设置顶部字体颜色
    setBottomColor(color) {
      text2.setAttribute("fill", color);

    },
    //设置svg宽高

    setSvgHW(hw) {
      svg.style.width = hw * 1 + 'px'
      svg.style.height = hw * 1 + 'px'

    }
  },
  mounted() {
    this.init();
  },
  watch: {
    //设置百分比
    percent(val) {
      if (val) {
        this.per = val * 1;
        this.init();
      }
    },
    //设置宽 
    width(val) {
      if (val) {
        this.svgWidth = val * 1;
        this.init();
      }
    },
    //设置高 
    height(val) {
      if (val) {
        this.svgHeight = val * 1;
        this.init();
      }
    },
    //设置高 
    strokeWidth(val) {
      if (val) {
        this.svgStrokeWidth = val * 1;
        this.init();
      }
    }

  }
}
</script>
<style scoped>
#circle {
  width: 188px;
  height: 188px;
}
circle {
  transition: stroke-dasharray 800ms;
}
</style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值