health-index.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. function gather(
  2. data: App.Health.Index.Data[],
  3. transform: (model: App.Health.Index.Model, index?: number) => any,
  4. skip = false,
  5. // link = { 血压: ['收缩压', '舒张压'], }
  6. link = {}
  7. ) {
  8. const ref = ((link) => {
  9. const ref: Record<string, { id: string; name: string; }> = {};
  10. Object.entries(link).forEach(([name, keys]) => {
  11. const _ = { id: name, name };
  12. (keys as AnyArray).forEach(key => { ref[key] = _; })
  13. })
  14. return ref;
  15. })(link);
  16. const cache = new Map<{ id: string; name: string; }, App.Health.Index.Chart[]>();
  17. for (const item of data) {
  18. if (skip && item.isAuto) continue;
  19. const model = createHealthIndex(item);
  20. const _ = ref[model.name] ?? { id: model.id, name: model.name };
  21. if (ref[model.id]) model.name = model.name.replace(_.name, '');
  22. if (cache.has(_)) {
  23. cache.get(_)?.push(transform(model, cache.get(_)?.length));
  24. } else {
  25. cache.set(_, [transform(model)]);
  26. }
  27. }
  28. return [...cache].map(gather => ({ ...gather[0], options: gather[1] }))
  29. }
  30. export function createHealthIndex(data: App.Health.Index.Data): App.Health.Index.Model {
  31. const { quotaId: id, minVal, maxVal, inputMin, inputMax, inputPrecision, patientQuotaRecordDTOS, ...model } = data;
  32. const scope = [+minVal, +maxVal] as const;
  33. const range = [
  34. +inputMin! || Math.floor(scope[0] * 0.75),
  35. +inputMax! || Math.floor(scope[1] * 1.25),
  36. ]
  37. return {
  38. ...model, id,
  39. precision: (+inputPrecision!) || 1,
  40. range, scope,
  41. values: patientQuotaRecordDTOS?.map(item => {
  42. return { value: +item.quotaVal, abnormal: item.abnormal, description: item.abnormalDesc, date: item.time2 }
  43. })?.filter(item => !!item.value),
  44. } as App.Health.Index.Model
  45. }
  46. export function healthIndex2Chart(model: App.Health.Index.Model, index = 0): AnyObject {
  47. const data = model.values?.map(t => [t.date, t.value]) ?? [];
  48. const yAxis = {
  49. name: model.unit,
  50. nameTextStyle: { color: '#ccc' },
  51. type: 'value', scale: true,
  52. min: model.range[0],
  53. max: Math.min(
  54. model.range[1],
  55. Math.floor(Math.max(model.scope[1], ...data.map((item: AnyArray) => item[1])) * 2)
  56. ),
  57. minInterval: model.precision >= 1 ? model.precision : void 0,
  58. axisLine: { show: true }
  59. }
  60. const series = {
  61. name: model.name,
  62. yAxisIndex: 0,
  63. type: 'line', smooth: true,
  64. data,
  65. markLine: {
  66. data: model.scope.map(value => { return { yAxis: value } })
  67. }
  68. }
  69. const visualMap = {
  70. show: false,
  71. type: 'piecewise',
  72. pieces: [
  73. { gt: model.scope[1] },
  74. { gte: model.scope[0], lte: model.scope[1] },
  75. { lt: model.scope[0] }
  76. ]
  77. }
  78. return { yAxis, series, visualMap }
  79. }
  80. export function transformHealthIndex2Chart(data: App.Health.Index.Data[]): AnyObject[] {
  81. const list = gather(data, healthIndex2Chart)
  82. const charts = [];
  83. for (const item of list) {
  84. const xAxis = { type: 'category', axisLabel: { overflow: 'breakAll' } }
  85. const yAxis = [], series = [], visualMap = [];
  86. for (const option of item.options) {
  87. yAxis.push(option.yAxis);
  88. visualMap.push(option.visualMap);
  89. if (option.series.data?.length) series.push(option.series);
  90. }
  91. if (!series.length) continue;
  92. charts.push({
  93. id: item.id,
  94. title: { text: `${item.name}` },
  95. tooltip: { trigger: 'axis' },
  96. legend: { show: series.length > 1 },
  97. grid: { bottom: 25 },
  98. xAxis, yAxis: yAxis[0],
  99. series, visualMap,
  100. })
  101. }
  102. return charts;
  103. }