record-index.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import * as echarts from '../../ec-canvas/echarts.min';
  2. import Theme from './chalk.theme';
  3. import { healthIndexReportMethod } from './request';
  4. echarts.registerTheme(...Theme);
  5. // module/health/pages/record-index/record-index.ts
  6. Component({
  7. lifetimes: {
  8. async attached() {
  9. try {
  10. await this._initRect();
  11. await this._getData();
  12. } catch (error) {
  13. console.error('record-index component error:', error);
  14. }
  15. },
  16. detached() {
  17. // 清理图表实例
  18. if (this.data.charts) {
  19. this.data.charts.forEach((chart: any) => {
  20. if (chart && chart.dispose) {
  21. chart.dispose();
  22. }
  23. });
  24. }
  25. }
  26. },
  27. properties: {
  28. },
  29. data: {
  30. gap: 0,
  31. scale: 1,
  32. rect: { width: 0, height: 0 },
  33. charts: [] as string[],
  34. loading: true,
  35. error: false
  36. },
  37. methods: {
  38. async _initRect() {
  39. try {
  40. const { windowWidth, pixelRatio } = wx.getWindowInfo()
  41. const { right } = wx.getMenuButtonBoundingClientRect();
  42. const gap = windowWidth - right;
  43. const width = windowWidth - gap * 2;
  44. const height = Math.floor(width * 3 / 4);
  45. this.setData({
  46. rect: { width, height, },
  47. scale: pixelRatio,
  48. gap
  49. });
  50. } catch (error) {
  51. console.error('_initRect error:', error);
  52. throw error;
  53. }
  54. },
  55. async _getData() {
  56. try {
  57. this.setData({ loading: true, error: false });
  58. const charts = await healthIndexReportMethod();
  59. if (!charts || charts.length === 0) {
  60. this.setData({ loading: false, error: true });
  61. return;
  62. }
  63. const chartConfigs = charts.map((option: any) => {
  64. return {
  65. id: option.id,
  66. onInit(canvas: any, width: number, height: number, dpr: number) {
  67. const ec = echarts.init(canvas, Theme[0], {
  68. width: width,
  69. height: height,
  70. devicePixelRatio: dpr
  71. });
  72. canvas.setChart(ec);
  73. ec.setOption(option);
  74. return ec;
  75. }
  76. }
  77. });
  78. this.setData({
  79. charts: chartConfigs,
  80. loading: false
  81. });
  82. } catch (error) {
  83. console.error('_getData error:', error);
  84. this.setData({ loading: false, error: true });
  85. }
  86. }
  87. }
  88. })