myArcbar.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <view>
  3. <canvas canvas-id="chartBox2" id="chartBox2" style="width: 570rpx;height: 280rpx;"></canvas>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. data() {
  9. return {
  10. cbox1: null, //画布实例对象
  11. arrayList: [
  12. {
  13. percent: 0,
  14. lineColor: '#FFE3D2',
  15. linePercentColor: "#FD741D",
  16. name: "活动热量"
  17. },
  18. {
  19. percent: 0,
  20. lineColor: '#FFF4D3',
  21. linePercentColor: "#FDC925",
  22. name: "今日步数"
  23. },
  24. {
  25. percent: 0,
  26. lineColor: '#DAF1FE',
  27. linePercentColor: "#49B9FC",
  28. name: "锻炼时长"
  29. },
  30. {
  31. percent: 0,
  32. lineColor: '#E2F2E1',
  33. linePercentColor: "#6DBF68",
  34. name: "活动小时数"
  35. }
  36. ],
  37. dataList:[],
  38. radius: 0,
  39. padding: 0,
  40. lineWidth: 0,
  41. }
  42. },
  43. mounted() {
  44. this.initCircle()
  45. },
  46. methods: {
  47. calcNum(a,b) {
  48. const num = Number(b||0) > 0 ? (Number(a||0) / Number(b||0)).toFixed(2) : 0
  49. return num > 1 ? 1 : num
  50. },
  51. initCircle(data){
  52. if(data) {
  53. this.arrayList[0].percent = this.calcNum(data.calorie,data.targetCalorie)
  54. this.arrayList[1].percent = this.calcNum(data.step,data.targetStep)
  55. this.arrayList[2].percent = this.calcNum(data.sport,data.targetSport)
  56. this.arrayList[3].percent = this.calcNum(data.activity,data.targetActivity)
  57. }
  58. //数据值
  59. this.dataList = Array.isArray(this.arrayList) ? this.arrayList : [];
  60. //直径
  61. this.radius = uni.upx2px(540);
  62. //四周内填充
  63. this.padding = uni.upx2px(6);
  64. //线宽度
  65. this.lineWidth = uni.upx2px(48);
  66. this.ctx = uni.createCanvasContext('chartBox2')
  67. this.drawStart()
  68. },
  69. //开始绘制
  70. drawStart(){
  71. //清空画面
  72. this.ctx.clearRect(0, 0, this.radius, this.radius);
  73. //循环绘制出每个圆环
  74. for(var i in this.dataList){
  75. //绘制圆环
  76. this.drawCircle(this.dataList[i], parseInt(i));
  77. }
  78. //绘制图形
  79. this.ctx.draw();
  80. },
  81. /**
  82. * 绘制圆环
  83. */
  84. drawCircle(_param, _i){
  85. //计算实际半径
  86. let _radius = this.radius/2-((this.padding+this.lineWidth)*(_i+1));
  87. //开始绘制圆环
  88. this.ctx.beginPath();
  89. this.ctx.arc(this.radius/2,this.radius/2, _radius,Math.PI,0, false);
  90. this.ctx.lineWidth = this.lineWidth;
  91. this.ctx.strokeStyle = _param.lineColor;
  92. this.ctx.lineCap = 'round';
  93. this.ctx.stroke();
  94. //绘制高亮进度条
  95. this.ctx.beginPath();
  96. this.ctx.lineCap = 'round';
  97. const endAngle = _param.percent > 0 ? (Math.PI * _param.percent)-Math.PI : Math.PI;
  98. this.ctx.arc(
  99. this.radius/2,this.radius/2,
  100. _radius,Math.PI, endAngle,false
  101. );
  102. this.ctx.strokeStyle = _param.linePercentColor;
  103. this.ctx.stroke();
  104. //恢复之前保存的绘图上下文
  105. this.ctx.restore();
  106. }
  107. }
  108. }
  109. </script>
  110. <style lang="scss" scoped>
  111. </style>