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

380 lines
8.7 KiB

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