canvas.vue 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <!-- 环形图组件 -->
  2. <template>
  3. <view style="position: relative;width:90px;height: 90px;background-color: #ffffff;">
  4. <canvas canvas-id="firstCanvas1" id="firstCanvas1"></canvas>
  5. <canvas canvas-id="firstCanvas2" id="firstCanvas2"></canvas>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. name: "canvasChart",
  11. data() {
  12. return {};
  13. },
  14. props: {
  15. value: {}, //属性:数值,0-1之间
  16. },
  17. created() {
  18. let value = isNaN(Number(this.value)) ? 0.00001 : Number(this.value)==0?0.00001:Number(this.value)
  19. var context = uni.createCanvasContext('firstCanvas1', this)
  20. context.setLineWidth(15)
  21. context.setStrokeStyle("#f6f6f6")
  22. context.arc(45, 45, 35, 0, 2 * Math.PI, true)
  23. context.stroke()
  24. context.draw()
  25. var context2 = uni.createCanvasContext('firstCanvas2', this)
  26. context2.setLineWidth(15)
  27. context2.setStrokeStyle("#01bcc3")
  28. context2.arc(45, 45, 35, (1 + (value * 2)) * Math.PI, 1 * Math.PI, true)
  29. context2.stroke()
  30. context2.draw()
  31. }
  32. }
  33. </script>
  34. <style>
  35. canvas {
  36. width: 90px;
  37. height: 90px;
  38. position: absolute;
  39. }
  40. </style>