health-index.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. function gather(
  2. data: App.Health.Index.Data[],
  3. transform: (model: App.Health.Index.Model) => any,
  4. skip = false,
  5. link = { 血压: ['收缩压', '舒张压'], }
  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 === 'Y') continue;
  18. const model = createHealthIndex(item);
  19. const _ = ref[model.name] ?? { 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. return {
  34. ...ruler, min, max, precision,
  35. markLine: [
  36. { value: scope[0], style: {} },
  37. { value: scope[1], style: {} },
  38. ],
  39. value: values?.slice(-1)[0]?.value,
  40. }
  41. }
  42. export function healthIndex2Progress(model: App.Health.Index.Model[]): AnyObject[] {
  43. return model.filter((item) => item.values?.length).map(item => {
  44. const { scope, range, values } = item;
  45. const { value, abnormal, description = '' } = values?.slice(-1)[0]!
  46. // 修订 range[1] 值
  47. range[1] = Math.min(range[1], Math.floor(Math.max(value, scope[1]) * 1.3));
  48. const length = range[1] - range[0];
  49. const scopeOffsetLeft = `${(scope[0] - range[0]) * 100 / length}%`;
  50. const scopeOffsetRight = `${100 - (scope[1] - range[0]) * 100 / length}%`;
  51. let valueOffset = Math.floor((value - range[0]) * 100 / length);
  52. let valueOffsetLeft = '0';
  53. let valueOffsetRight = '0';
  54. let valueType;
  55. if (value > scope[1]) {
  56. valueType = 'big';
  57. valueOffsetLeft = scopeOffsetLeft;
  58. valueOffsetRight = `${100 - valueOffset}%`;
  59. } else if (value < scope[0]) {
  60. valueType = 'small';
  61. valueOffsetLeft = `${valueOffset}%`;
  62. valueOffsetRight = scopeOffsetRight;
  63. } else {
  64. valueType = 'normal';
  65. valueOffsetLeft = scopeOffsetLeft;
  66. valueOffsetRight = scopeOffsetRight;
  67. }
  68. const a = {
  69. name: item.name, unit: item.unit,
  70. value, min: scope[0], max: scope[1],
  71. abnormal, description: description?.replace(item.name, ''),
  72. scopeOffsetLeft, scopeOffsetRight,
  73. valueType, valueOffset: `${valueOffset}%`,
  74. valueOffsetLeft, valueOffsetRight,
  75. }
  76. // console.log(a, '12345-->', length);
  77. return a;
  78. })
  79. }
  80. export function createHealthIndex(data: App.Health.Index.Data): App.Health.Index.Model {
  81. const { quotaId: id, minVal, maxVal, inputMin, inputMax, inputPrecision, patientQuotaRecordDTOS, ...model } = data;
  82. const scope = [+minVal, +maxVal] as const;
  83. const range = [
  84. +inputMin!/*! || Math.floor(scope[0] * 0.75)*/,
  85. +inputMax!/*! || Math.floor(scope[1] * 1.25)*/,
  86. ]
  87. return {
  88. ...model, id,
  89. precision: ({ 0: 0.01, 1: 0.1, 2: 2, 3: 1 } as any)[inputPrecision ?? 3],
  90. range, scope,
  91. values: patientQuotaRecordDTOS?.map(item => {
  92. return { value: item.quotaVal, abnormal: item.abnormal, description: item.abnormalDesc, date: item.time2 }
  93. })?.filter(item => !!item.value),
  94. defaultValue: 12
  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. return gather(data, healthIndex2Ruler, true);
  99. }
  100. export function healthIndex2Chart(model: App.Health.Index.Model): AnyObject {
  101. return {
  102. name: model.name,
  103. type: 'line', smooth: true,
  104. data: model.values?.map(t => [t.date, t.value]),
  105. markLine: {
  106. data: model.scope.map(value => { return { yAxis: value } })
  107. }
  108. }
  109. }
  110. export function transformHealthIndex2Chart(data: App.Health.Index.Data[]): AnyObject[] {
  111. const list = gather(data, healthIndex2Chart)
  112. const charts = [];
  113. for (const item of list) {
  114. const xAxis = { type: 'category', axisLabel: { overflow: 'breakAll' } }
  115. const yAxis = { type: 'value', scale: true, }
  116. const series = item.options.filter((item: any) => item.data?.length)
  117. if (!series.length) continue;
  118. charts.push({
  119. id: item.id,
  120. title: { text: `${item.name}` },
  121. tooltip: { trigger: 'axis' },
  122. legend: { show: series.length > 1 },
  123. xAxis, yAxis,
  124. series,
  125. })
  126. }
  127. return charts;
  128. }