index.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. Component({
  2. properties: {
  3. statusList: {
  4. type: Array,
  5. value: [],
  6. },
  7. dataset: { type: Object },
  8. },
  9. data: {
  10. dashedCounts: [],
  11. },
  12. lifetimes: {
  13. ready() {
  14. this.calcAllDashes();
  15. },
  16. },
  17. methods: {
  18. onBodyModel(event: any) {
  19. const position = event.mark?.position;
  20. if (position) this.triggerEvent("position", { position });
  21. },
  22. calcAllDashes() {
  23. const query = wx.createSelectorQuery().in(this);
  24. const dashedCounts: any[] = [];
  25. const itemCount = this.data.statusList.length;
  26. const systemInfo = wx.getSystemInfoSync();
  27. const rpx2px = systemInfo.windowWidth / 750;
  28. for (let i = 0; i < itemCount; i++) {
  29. query.select(`#item-${i}`).boundingClientRect();
  30. }
  31. query.exec((rects) => {
  32. if (rects) {
  33. rects.forEach((rect: any, i: number) => {
  34. if (rect) {
  35. // 换算成 rpx
  36. const heightRpx = rect.height / rpx2px;
  37. // 14rpx为圆圈高度
  38. const lineHeight = heightRpx - 14;
  39. const dashHeight = 10;
  40. const gap = 4;
  41. let count = Math.floor(lineHeight / (dashHeight + gap));
  42. if (count < 1) count = 1;
  43. dashedCounts[i] = Array.from({ length: count }, (_, j) => j);
  44. } else {
  45. dashedCounts[i] = Array.from({ length: 10 }, (_, j) => j);
  46. }
  47. });
  48. this.setData({ dashedCounts });
  49. }
  50. });
  51. },
  52. },
  53. observers: {
  54. statusList: function () {
  55. this.calcAllDashes();
  56. },
  57. },
  58. });