record-index.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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: 375, height: 300 },
  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, });
  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: 400,
  69. height: 280,
  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. })