ring-progress.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <view class="rp-wrap" :style="wrapStyle">
  3. <!-- SVG 环形:圆环 stroke-dasharray 实现进度 -->
  4. <svg :viewBox="viewBox" class="rp-svg" :style="svgStyle">
  5. <circle :cx="center" :cy="center" :r="radius" :stroke="trackColor" :stroke-width="strokeW" fill="none" />
  6. <circle
  7. :cx="center"
  8. :cy="center"
  9. :r="radius"
  10. :stroke="barColor"
  11. :stroke-width="strokeW"
  12. fill="none"
  13. :stroke-linecap="'round'"
  14. :stroke-dasharray="dashArray"
  15. :stroke-dashoffset="dashOffset"
  16. :transform="'rotate(-90 ' + center + ' ' + center + ')'"
  17. />
  18. </svg>
  19. <view class="rp-center">
  20. <text class="rp-value" :style="{ fontSize: valueFont }">{{ valueText }}</text>
  21. <text v-if="label" class="rp-label">{{ label }}</text>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. /**
  27. * RingProgress — 环形进度(原子级,SVG 自绘,rpx 自适应)
  28. * 用途:学时达标 / 认证进度 / 综合评级环
  29. * 契约:percent 0-100;valueText 居中数字;label 下方文字
  30. */
  31. export default {
  32. name: 'RingProgress',
  33. props: {
  34. percent: { type: Number, default: 0 },
  35. // 展示尺寸(px,组件内部按 rpx 换算)
  36. size: { type: Number, default: 160 },
  37. color: { type: String, default: '#2E6FA8' },
  38. label: { type: String, default: '' },
  39. valueText: { type: String, default: '' }
  40. },
  41. data() {
  42. return {
  43. trackColor: '#E3ECF4',
  44. strokeW: 10
  45. }
  46. },
  47. computed: {
  48. center() {
  49. return this.size / 2
  50. },
  51. radius() {
  52. return (this.size - this.strokeW) / 2
  53. },
  54. viewBox() {
  55. return '0 0 ' + this.size + ' ' + this.size
  56. },
  57. svgStyle() {
  58. return {
  59. width: this.size + 'px',
  60. height: this.size + 'px'
  61. }
  62. },
  63. wrapStyle() {
  64. return {
  65. width: this.size + 'px',
  66. height: this.size + 'px'
  67. }
  68. },
  69. circumference() {
  70. return 2 * Math.PI * this.radius
  71. },
  72. dashArray() {
  73. return this.circumference + ''
  74. },
  75. dashOffset() {
  76. var p = this.percent
  77. if (p < 0) p = 0
  78. if (p > 100) p = 100
  79. return this.circumference * (1 - p / 100)
  80. },
  81. barColor() {
  82. return this.color
  83. },
  84. valueFont() {
  85. return Math.round(this.size * 0.24) + 'px'
  86. }
  87. }
  88. }
  89. </script>
  90. <style scoped>
  91. .rp-wrap {
  92. position: relative;
  93. display: flex;
  94. align-items: center;
  95. justify-content: center;
  96. }
  97. .rp-svg {
  98. display: block;
  99. }
  100. .rp-center {
  101. position: absolute;
  102. left: 0;
  103. right: 0;
  104. top: 0;
  105. bottom: 0;
  106. display: flex;
  107. flex-direction: column;
  108. align-items: center;
  109. justify-content: center;
  110. }
  111. .rp-value {
  112. font-weight: 700;
  113. color: #1F4E79;
  114. line-height: 1.2;
  115. }
  116. .rp-label {
  117. font-size: 20rpx;
  118. color: #3B5A78;
  119. margin-top: 4rpx;
  120. }
  121. </style>