普兆健康管家前端代码仓库
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.

376 lines
8.3 KiB

  1. <template>
  2. <view class="box">
  3. <canvas class="radar-bg" :canvas-id="`cpbg${code}`" type="2d"></canvas>
  4. <canvas class="radar-fg" :canvas-id="`cpfg${code}`" type="2d"></canvas>
  5. <div
  6. v-for="(item, index) in score"
  7. :key="index"
  8. :class="['axis', `axis-${index}`]"
  9. >
  10. <div class="flex title">
  11. <div class="line"></div>
  12. <div class="label">{{ axis[index] }}</div>
  13. </div>
  14. <div class="flex desc">
  15. 指数<text class="highlight">{{ item }}</text>
  16. </div>
  17. </div>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. props: {
  23. score: {
  24. type: Array,
  25. default() {
  26. return []
  27. }
  28. }
  29. },
  30. data() {
  31. return {
  32. code: Math.floor(Math.random() * 100).toString(),
  33. axis: ['饮食', '运动', '心理', '体质', '作息']
  34. }
  35. },
  36. watch: {
  37. score: {
  38. handler(val) {
  39. console.log('watch score', val)
  40. this.$nextTick(() => {
  41. this.drawFg(val)
  42. })
  43. },
  44. deep: true
  45. },
  46. },
  47. mounted() {
  48. this.drawBg();
  49. },
  50. methods: {
  51. drawRing(ctx, x, y, startAngle, r) {
  52. let x0 = x + r * Math.cos(startAngle)
  53. let y0 = y - r * Math.sin(startAngle)
  54. let lineStartX = x0
  55. let lineStartY = y0
  56. let lineEndX = null
  57. let lineEndY = null
  58. ctx.beginPath(); //开始一个新的路径
  59. ctx.moveTo(lineStartX, lineStartY);
  60. for (let i = 1; i < 5; i++) {
  61. let angle = startAngle - Math.PI * 2 * i / 5
  62. lineEndX = x + r * Math.cos(angle)
  63. lineEndY = y - r * Math.sin(angle)
  64. ctx.lineTo(lineEndX, lineEndY);
  65. ctx.stroke();
  66. lineStartX = lineEndX
  67. lineStartY = lineEndY
  68. }
  69. lineEndX = x0
  70. lineEndY = y0
  71. ctx.moveTo(lineStartX, lineStartY);
  72. ctx.lineTo(lineEndX, lineEndY);
  73. ctx.stroke();
  74. ctx.closePath();
  75. },
  76. drawLine(ctx, x, y, startAngle, R, r) {
  77. let lines = new Array(5).fill(0).map(() => [{}, {}])
  78. let arr = [R, r]
  79. arr.forEach((len, index) => {
  80. for (let i = 0; i < 5; i++) {
  81. let angle = startAngle - Math.PI * 2 * i / 5
  82. lines[i][index].x = x + len * Math.cos(angle)
  83. lines[i][index].y = y - len * Math.sin(angle)
  84. }
  85. })
  86. lines.forEach(line => {
  87. const [point1, point2] = line
  88. ctx.beginPath(); //开始一个新的路径
  89. ctx.moveTo(point1.x, point1.y);
  90. ctx.lineTo(point2.x, point2.y);
  91. ctx.stroke();
  92. ctx.closePath();
  93. })
  94. },
  95. drawBg() {
  96. uni.createSelectorQuery().in(this)
  97. .select('.radar-bg')
  98. .fields({
  99. node: true,
  100. size: true
  101. })
  102. .exec(async (res) => {
  103. const canvas = res[0].node
  104. // Canvas 画布的实际绘制宽高
  105. const width = res[0].width
  106. const height = res[0].height
  107. //根据dpr调整
  108. const dpr = wx.getWindowInfo().pixelRatio
  109. canvas.width = width * dpr
  110. canvas.height = height * dpr
  111. let Ratio = width / 526
  112. // 渲染上下文
  113. const ctx = canvas.getContext('2d')
  114. ctx.scale(dpr, dpr)
  115. ctx.clearRect(0, 0, width, height)
  116. let x = width / 2
  117. let y = height / 2
  118. let startAngle = Math.PI / 2
  119. ctx.lineWidth = 2 * Ratio;
  120. ctx.save()
  121. ctx.lineWidth = 3 * Ratio;
  122. ctx.strokeStyle = '#DACEFE';
  123. ctx.shadowOffsetX = 0;
  124. ctx.shadowOffsetY = 0;
  125. ctx.shadowBlur = 6;
  126. ctx.shadowColor = "rgba(211, 210, 229, 0.6)";
  127. this.drawRing(ctx, x, y, startAngle, 132 * Ratio)
  128. ctx.fillStyle = '#F9F7FF'
  129. ctx.fill()
  130. ctx.restore()
  131. ctx.save()
  132. ctx.setLineDash([2]);
  133. ctx.strokeStyle = '#E1E0E6';
  134. let dividers = [100, 68, 36, 8]
  135. dividers.forEach(r => {
  136. this.drawRing(ctx, x, y, startAngle, r * Ratio)
  137. })
  138. ctx.restore()
  139. // ctx.setLineDash([]);
  140. ctx.strokeStyle = '#E1E0E6';
  141. this.drawLine(ctx, x, y, startAngle, 132 * Ratio, 8 * Ratio)
  142. })
  143. },
  144. drawFg(arr) {
  145. uni.createSelectorQuery().in(this)
  146. .select('.radar-fg')
  147. .fields({
  148. node: true,
  149. size: true
  150. })
  151. .exec(async (res) => {
  152. console.log('radar-fg', res)
  153. const canvas = res[0].node
  154. // Canvas 画布的实际绘制宽高
  155. const width = res[0].width
  156. const height = res[0].height
  157. //根据dpr调整
  158. const dpr = wx.getWindowInfo().pixelRatio
  159. canvas.width = width * dpr
  160. canvas.height = height * dpr
  161. let Ratio = width / 526
  162. // 渲染上下文
  163. const ctx = canvas.getContext('2d')
  164. ctx.scale(dpr, dpr)
  165. ctx.clearRect(0, 0, width, height)
  166. let x = width / 2
  167. let y = height / 2
  168. let R = 100 * Ratio
  169. console.log('R', R)
  170. let startAngle = Math.PI / 2
  171. ctx.lineWidth = 3 * Ratio;
  172. let gradient = ctx.createLinearGradient(x - R, y, x + R, y);
  173. gradient.addColorStop('0', '#7451DE');
  174. gradient.addColorStop('1.0', '#B1A4FF');
  175. ctx.strokeStyle = gradient;
  176. ctx.shadowOffsetX = 0;
  177. ctx.shadowOffsetY = 0;
  178. ctx.shadowBlur = 23.9;
  179. ctx.shadowColor = "rgba(99, 52, 238, 0.4)";
  180. let points = arr.map((score, i) => {
  181. let r = R * score / 100
  182. let angle = startAngle - Math.PI * 2 * i / 5
  183. return {
  184. x: x + r * Math.cos(angle),
  185. y: y - r * Math.sin(angle)
  186. }
  187. })
  188. let lineStartX = points[0].x
  189. let lineStartY = points[0].y
  190. let lineEndX = null
  191. let lineEndY = null
  192. ctx.beginPath(); //开始一个新的路径
  193. ctx.moveTo(lineStartX, lineStartY);
  194. for (let i = 1; i < 5; i++) {
  195. lineEndX = points[i].x
  196. lineEndY = points[i].y
  197. ctx.lineTo(lineEndX, lineEndY);
  198. ctx.stroke();
  199. lineStartX = lineEndX
  200. lineStartY = lineEndY
  201. }
  202. lineEndX = points[0].x
  203. lineEndY = points[0].y
  204. ctx.moveTo(lineStartX, lineStartY);
  205. ctx.lineTo(lineEndX, lineEndY);
  206. ctx.stroke();
  207. ctx.closePath();
  208. ctx.fillStyle = 'rgba(188, 167, 255, 0.2)'
  209. ctx.fill()
  210. })
  211. },
  212. },
  213. }
  214. </script>
  215. <style scoped lang="scss">
  216. $size: 526rpx;
  217. .box {
  218. position: relative;
  219. width: $size;
  220. height: $size;
  221. .radar-bg,
  222. .radar-fg {
  223. position: absolute;
  224. top: 0;
  225. left: 0;
  226. width: 100%;
  227. height: 100%;
  228. }
  229. }
  230. .axis {
  231. position: absolute;
  232. width: 128rpx;
  233. .title {
  234. justify-content: flex-start;
  235. }
  236. .line {
  237. width: 4rpx;
  238. height: 18rpx;
  239. border-radius: 2rpx;
  240. margin-right: 8rpx;
  241. }
  242. .label {
  243. font-family: PingFang SC;
  244. font-weight: 400;
  245. font-size: 24rpx;
  246. line-height: 1;
  247. color: #252545;
  248. }
  249. .desc {
  250. font-family: PingFang SC;
  251. font-weight: 400;
  252. font-size: 24rpx;
  253. line-height: 1;
  254. color: #8B8B8B;
  255. .highlight {
  256. margin: 0 8rpx;
  257. font-weight: 600;
  258. font-size: 32rpx;
  259. line-height: 1.4;
  260. color: #7451DE;
  261. }
  262. }
  263. &-0 {
  264. top: 54rpx;
  265. left: 232rpx;
  266. .title {
  267. padding-left: 11rpx;
  268. }
  269. .line {
  270. background: #ECB501;
  271. }
  272. }
  273. &-1 {
  274. top: 205rpx;
  275. right: 0;
  276. .line {
  277. background: #009CEF;
  278. }
  279. }
  280. &-2 {
  281. bottom: 66rpx;
  282. right: 74rpx;
  283. .line {
  284. background: #DA629F;
  285. }
  286. }
  287. &-3 {
  288. bottom: 66rpx;
  289. left: 76rpx;
  290. .title {
  291. padding-right: 22rpx;
  292. justify-content: flex-end;
  293. }
  294. .line {
  295. background: #43B741;
  296. }
  297. }
  298. &-4 {
  299. top: 205rpx;
  300. left: 0rpx;
  301. .title {
  302. padding-right: 22rpx;
  303. justify-content: flex-end;
  304. }
  305. .line {
  306. background: #EB7F09;
  307. }
  308. }
  309. }
  310. </style>