health-index.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. function gather(
  2. data: App.Health.Index.Data[],
  3. transform: (model: App.Health.Index.Model) => any,
  4. skip = false,
  5. link = { 血压: [1, 2], }
  6. ) {
  7. const ref = ((link) => {
  8. const ref: Record<string, { id: string; name: string; }> = {};
  9. Object.entries(link).forEach(([name, keys]) => {
  10. const _ = { id: name, name };
  11. keys.forEach(key => { ref[key] = _; })
  12. })
  13. return ref;
  14. })(link);
  15. const cache = new Map<{ id: string; name: string; }, App.Health.Index.Ruler[]>();
  16. for (const item of data) {
  17. if (skip && item.isAuto) continue;
  18. const model = createHealthIndex(item);
  19. const _ = ref[model.id] ?? { id: model.id, name: model.name };
  20. if (ref[model.id]) model.name = model.name.replace(_.name, '');
  21. if (cache.has(_)) {
  22. cache.get(_)?.push(transform(model))
  23. } else {
  24. cache.set(_, [transform(model)])
  25. }
  26. }
  27. return [...cache].map(gather => ({ ...gather[0], options: gather[1] }))
  28. }
  29. export function healthIndex2Ruler(model: App.Health.Index.Model): App.Health.Index.Ruler {
  30. let { scope, range, precision, values, ...ruler } = model;
  31. const min = range?.[0] ?? Math.floor(scope[0] * 0.5);
  32. const max = range?.[1] ?? Math.floor(scope[1] * 1.5);
  33. precision = ((diff) => {
  34. if (diff < 5) return 0.01;
  35. if (diff < 10) return 0.1;
  36. else return 1;
  37. })(max - min)
  38. return {
  39. ...ruler, min, max, precision,
  40. markLine: [
  41. { value: scope[0], style: {} },
  42. { value: scope[1], style: {} },
  43. ],
  44. value: values?.slice(-1)[0]?.value,
  45. }
  46. }
  47. export function healthIndex2Progress(model: App.Health.Index.Model[]): AnyObject[] {
  48. return model.filter((item) => item.values?.length).map(item => {
  49. const { scope, range, values } = item;
  50. const { value, abnormal, description = '' } = values?.slice(-1)[0]!
  51. const length = range[1] - range[0];
  52. const scopeOffsetLeft = `${Math.floor((scope[0] - range[0]) * 100 / length)}%`;
  53. const scopeOffsetRight = `${100 - Math.floor((scope[1] - range[0]) * 100 / length)}%`;
  54. let valueOffset = Math.floor((value - range[0]) * 100 / length);
  55. let valueOffsetLeft = '0';
  56. let valueOffsetRight = '0';
  57. let valueType;
  58. if (value > scope[1]) {
  59. valueType = 'big';
  60. valueOffsetLeft = scopeOffsetLeft;
  61. valueOffsetRight = `${100 - valueOffset}%`;
  62. } else if (value < scope[0]) {
  63. valueType = 'small';
  64. valueOffsetLeft = `${valueOffset}%`;
  65. valueOffsetRight = scopeOffsetRight;
  66. } else {
  67. valueType = 'normal';
  68. valueOffsetLeft = scopeOffsetLeft;
  69. valueOffsetRight = scopeOffsetRight;
  70. }
  71. return {
  72. name: item.name, unit: item.unit,
  73. value, min: scope[0], max: scope[1],
  74. abnormal, description: description?.replace(item.name, ''),
  75. scopeOffsetLeft, scopeOffsetRight,
  76. valueType, valueOffset: `${valueOffset}%`,
  77. valueOffsetLeft, valueOffsetRight,
  78. }
  79. })
  80. }
  81. export function createHealthIndex(data: App.Health.Index.Data): App.Health.Index.Model {
  82. const { quotaId: id, minVal, maxVal, inputMin, inputMax, inputPrecision, patientQuotaRecordDTOS, ...model } = data;
  83. const scope = [+minVal, +maxVal] as const;
  84. const range = [
  85. +inputMin! || Math.floor(scope[0] * 0.75),
  86. +inputMax! || Math.floor(scope[1] * 1.25),
  87. ]
  88. return {
  89. ...model, id,
  90. precision: (+inputPrecision!) || 1,
  91. range, scope,
  92. values: patientQuotaRecordDTOS?.map(item => {
  93. return { value: +item.quotaVal, abnormal: item.abnormal, description: item.abnormalDesc, date: item.time2 }
  94. })?.filter(item => !!item.value),
  95. } as App.Health.Index.Model
  96. }
  97. export function transformHealthIndex2Ruler(data: App.Health.Index.Data[]): { id: string, name: string, options: App.Health.Index.Ruler[] }[] {
  98. // const ref = ((link) => {
  99. // const ref: Record<string, { id: string; name: string; }> = {};
  100. // Object.entries(link).forEach(([name, keys]) => {
  101. // const _ = { id: name, name };
  102. // keys.forEach(key => { ref[key] = _; })
  103. // })
  104. // return ref;
  105. // })({ 血压: [1, 2], })
  106. // const cache = new Map<{ id: string; name: string; }, App.Health.Index.Ruler[]>();
  107. // for (const item of data) {
  108. // if (item.isAuto) continue;
  109. // const model = createHealthIndex(item);
  110. // const _ = ref[model.id] ?? { id: model.id, name: model.name };
  111. // if (ref[model.id]) model.name = model.name.replace(_.name, '');
  112. // if (cache.has(_)) {
  113. // cache.get(_)?.push(healthIndex2Ruler(model))
  114. // } else {
  115. // cache.set(_, [healthIndex2Ruler(model)])
  116. // }
  117. // }
  118. // return [...cache].map(gather => ({ ...gather[0], options: gather[1] }))
  119. return gather(data, healthIndex2Ruler, true);
  120. }
  121. export function healthIndex2Chart(model: App.Health.Index.Model): AnyObject {
  122. return {
  123. name: model.name,
  124. type: 'line', smooth: true,
  125. data: model.values?.map(t => [t.date, t.value]),
  126. markLine: {
  127. data: model.scope.map(value => { return { yAxis: value } })
  128. }
  129. }
  130. }
  131. export function transformHealthIndex2Chart(data: App.Health.Index.Data[]): AnyObject[] {
  132. const list = gather(data, healthIndex2Chart)
  133. const charts = [];
  134. for (const item of list) {
  135. const xAxis = { type: 'category', axisLabel: { overflow: 'breakAll' } }
  136. const yAxis = { type: 'value', scale: true, }
  137. const series = item.options.filter((item: any) => item.data?.length)
  138. if (!series.length) continue;
  139. charts.push({
  140. id: item.id,
  141. title: { text: `${item.name}` },
  142. tooltip: { trigger: 'axis' },
  143. legend: { show: series.length > 1 },
  144. xAxis, yAxis,
  145. series,
  146. })
  147. }
  148. return charts;
  149. }