风险测评小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
2.5 KiB

  1. <template>
  2. <view class="flex progress_box" :style="style">
  3. <view>{{ label || '' }}</view>
  4. <canvas class="progress_bar" :canvas-id="`cpbar${code}`" type="2d"></canvas>
  5. <view class="value">{{ value || 0 }}</view>
  6. </view>
  7. </template>
  8. <script>
  9. const START = - Math.PI * 3 / 2 + Math.PI * 1 / 4 - Math.PI * 1 / 24
  10. const END = Math.PI * 1 / 4 + Math.PI * 1 / 24
  11. export default {
  12. props: {
  13. label: {
  14. type: String,
  15. default: '',
  16. },
  17. value: {
  18. type: String | Number,
  19. default: 0,
  20. },
  21. color: {
  22. type: String,
  23. default: '#014FA2'
  24. }
  25. },
  26. data() {
  27. return {
  28. code: Math.floor(Math.random() * 100).toString()
  29. }
  30. },
  31. computed: {
  32. style() {
  33. return `color: ${this.color};`
  34. }
  35. },
  36. mounted() {
  37. this.drawProgress(this.color)
  38. },
  39. methods: {
  40. drawProgress(color) {
  41. uni.createSelectorQuery().in(this)
  42. .select('.progress_bar')
  43. .fields({
  44. node: true,
  45. size: true
  46. })
  47. .exec(async (res) => {
  48. const canvas = res[0].node
  49. // Canvas 画布的实际绘制宽高
  50. const width = res[0].width
  51. const height = res[0].height
  52. //根据dpr调整
  53. const dpr = wx.getWindowInfo().pixelRatio
  54. canvas.width = width * dpr
  55. canvas.height = height * dpr
  56. let Ratio = width / 126
  57. // 渲染上下文
  58. const ctx = canvas.getContext('2d')
  59. ctx.scale(dpr, dpr)
  60. ctx.clearRect(0, 0, width, height)
  61. let w = 14 * Ratio
  62. ctx.lineWidth = w; // 设置圆环的宽度
  63. ctx.strokeStyle = color; // 设置圆环的颜色
  64. // ctx.lineCap = 'round'; // 设置圆环端点的形状
  65. ctx.beginPath();
  66. let x = width / 2
  67. let y = height / 2
  68. let r = 62 * Ratio - w / 2
  69. ctx.arc(x, y, r, START, END, false);
  70. ctx.stroke();
  71. })
  72. },
  73. },
  74. }
  75. </script>
  76. <style lang="scss" scoped>
  77. $size: 126rpx;
  78. .progress_box {
  79. position: relative;
  80. width: $size;
  81. // height: $size;
  82. height: 154rpx;
  83. align-items: flex-end;
  84. font-size: 22rpx;
  85. line-height: 40rpx;
  86. }
  87. .progress_bar {
  88. position: absolute;
  89. top: 0;
  90. left: 0;
  91. width: $size;
  92. height: $size;
  93. }
  94. .value {
  95. position: absolute;
  96. top: 40rpx;
  97. left: 50%;
  98. transform: translateX(-50%);
  99. font-size: 32rpx;
  100. font-weight: 600;
  101. }
  102. </style>