myRing.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <view>
  3. <canvas :style="{width: canvasWidth + 'px',height:canvasHeight + 'px'}" canvas-id="homeownerCanvas"
  4. class="homeowner-canvas_charts" @error="canvasIdErrorCallback"></canvas>
  5. </view>
  6. </template>
  7. <script>
  8. export default {
  9. name: "myRing",
  10. props: ['info'],
  11. data() {
  12. return {
  13. ctx: null,
  14. canvasWidth: uni.upx2px(208),
  15. canvasHeight: uni.upx2px(208),
  16. radius: uni.upx2px(80),
  17. list: [{
  18. color2: "#EEDFFB",
  19. color1: "#8C37E6",
  20. val: 0,
  21. title: "深睡",
  22. },
  23. {
  24. color2: "#F6D6F5",
  25. color1: "#D138CF",
  26. val: 0,
  27. title: "浅睡",
  28. },
  29. {
  30. color2: "#FDDFE0",
  31. color1: "#F88082",
  32. val: 0,
  33. title: "快速眼动",
  34. },
  35. {
  36. color2: "#FEDE93",
  37. color1: "#FDBD27",
  38. val: 0,
  39. title: "清醒",
  40. },
  41. ],
  42. }
  43. },
  44. methods: {
  45. initData() {
  46. const sum = (Number(this.info.deepPre || 0) + Number(this.info.lightPre || 0) + Number(this.info.eyemovePre || 0) + Number(this.info.weakPre || 0)).toFixed(0)
  47. this.list[0].val = sum == 0 ? 0 : (Number(this.info.deepPre || 0) / sum).toFixed(2)
  48. this.list[1].val = sum == 0 ? 0 : (Number(this.info.lightPre || 0) / sum).toFixed(2)
  49. this.list[2].val = sum == 0 ? 0 : (Number(this.info.eyemovePre || 0) / sum).toFixed(2)
  50. this.list[3].val = sum == 0 ? 0 : (Number(this.info.weakPre || 0) / sum).toFixed(2)
  51. this.initCanvas('homeownerCanvas', this.list)
  52. },
  53. initCanvas(canvasID, data) {
  54. this.ctx = uni.createCanvasContext(canvasID, this)
  55. // 定义圆环的属性
  56. const centerX = this.canvasWidth / 2; // 圆心的x坐标
  57. const centerY = this.canvasHeight / 2; // 圆心的y坐标
  58. const radius = uni.upx2px(80); // 圆环的半径
  59. const lineWidth = uni.upx2px(28); // 圆环的宽度
  60. let tempAngle = 0;
  61. this.ctx.beginPath();
  62. this.ctx.arc(centerX, centerY, radius,0, Math.PI*2);
  63. this.ctx.setStrokeStyle("#eee");
  64. this.ctx.setLineWidth(lineWidth);
  65. this.ctx.stroke();
  66. for (let i = 0; i < data.length; i++) {
  67. //计算当前扇形角度 所占比例*360
  68. let angle = data[i].val * 360;
  69. //当前扇形起始绘制弧度 360 = 2π 等于6.28
  70. let startAngle = tempAngle * Math.PI / 180;
  71. //当前扇形借结束绘制弧度
  72. let endAngle = (tempAngle + angle) * Math.PI / 180;
  73. // 开始绘制第二条路径 成交 橘色
  74. this.ctx.beginPath();
  75. // this.ctx.setLineJoin('miter');
  76. this.ctx.setLineCap('round')
  77. this.ctx.arc(centerX, centerY, radius,startAngle-(Math.PI / 2), endAngle-(Math.PI / 2));
  78. // centerX - radius - lineWidth, centerY,centerX + radius + lineWidth,centerY
  79. let gradient = this.ctx.createLinearGradient(centerX, centerY - radius,centerX,centerY +radius);
  80. gradient.addColorStop(0, data[i].color2);
  81. gradient.addColorStop(1, data[i].color1);
  82. // this.ctx.setStrokeStyle(gradient);
  83. this.ctx.setStrokeStyle(data[i].color1);
  84. this.ctx.setLineWidth(lineWidth);
  85. this.ctx.stroke();
  86. //当前扇形结束绘制角度,即下一个扇形开始绘制角度
  87. tempAngle += angle;
  88. }
  89. this.ctx.setFillStyle("#757575");
  90. this.ctx.setFontSize(uni.upx2px(28));
  91. this.ctx.setTextAlign("center");
  92. this.ctx.setTextBaseline("middle");
  93. this.ctx.fillText("睡眠", uni.upx2px(104), uni.upx2px(88));
  94. this.ctx.fillText("比例", uni.upx2px(104), uni.upx2px(124));
  95. this.ctx.stroke();
  96. this.ctx.draw();
  97. },
  98. canvasIdErrorCallback: function(e) {
  99. console.error(e.detail.errMsg)
  100. },
  101. }
  102. }
  103. </script>
  104. <style lang="scss" scoped>
  105. .homeowner-canvas_charts {
  106. // transform: rotate(-90deg);
  107. }
  108. </style>