record-index.ts 2.4 KB

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