普兆健康管家后端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.5 KiB

  1. <template>
  2. <div ref="chartRef" :style="{ height, width }"></div>
  3. </template>
  4. <script lang="ts">
  5. import { defineComponent, PropType, ref, Ref, reactive, watchEffect } from 'vue';
  6. import { useECharts } from '/@/hooks/web/useECharts';
  7. import { cloneDeep } from 'lodash-es';
  8. export default defineComponent({
  9. name: 'LineMulti',
  10. props: {
  11. chartData: {
  12. type: Array,
  13. default: () => [],
  14. required: true,
  15. },
  16. option: {
  17. type: Object,
  18. default: () => ({}),
  19. },
  20. type: {
  21. type: String as PropType<string>,
  22. default: 'line',
  23. },
  24. width: {
  25. type: String as PropType<string>,
  26. default: '100%',
  27. },
  28. height: {
  29. type: String as PropType<string>,
  30. default: 'calc(100vh - 78px)',
  31. },
  32. },
  33. emits: ['click'],
  34. setup(props, { emit }) {
  35. const chartRef = ref<HTMLDivElement | null>(null);
  36. const { setOptions, getInstance } = useECharts(chartRef as Ref<HTMLDivElement>);
  37. const option = reactive({
  38. tooltip: {
  39. trigger: 'axis',
  40. axisPointer: {
  41. type: 'shadow',
  42. label: {
  43. show: true,
  44. backgroundColor: '#333',
  45. },
  46. },
  47. },
  48. legend: {
  49. top: 30,
  50. },
  51. grid: {
  52. top: 60,
  53. },
  54. xAxis: {
  55. type: 'category',
  56. data: [],
  57. },
  58. yAxis: {
  59. type: 'value',
  60. },
  61. series: [],
  62. });
  63. watchEffect(() => {
  64. props.chartData && initCharts();
  65. });
  66. function initCharts() {
  67. if (props.option) {
  68. Object.assign(option, cloneDeep(props.option));
  69. }
  70. //图例类型
  71. let typeArr = Array.from(new Set(props.chartData.map((item) => item.type)));
  72. //轴数据
  73. let xAxisData = Array.from(new Set(props.chartData.map((item) => item.name)));
  74. let seriesData = [];
  75. typeArr.forEach((type) => {
  76. let obj: any = { name: type, type: props.type };
  77. // update-begin--author:liaozhiyang---date:20240407---for:【QQYUN-8762】首页默认及echars颜色调整
  78. const findItem: any = props.chartData.find((item: any) => item.type == type);
  79. if (findItem && findItem.color) {
  80. obj.color = findItem.color;
  81. }
  82. // update-end--author:liaozhiyang---date:20240407---for:【QQYUN-8762】首页默认及echars颜色调整
  83. // update-begin-author:liusq date:2023-7-12 for: [issues/613] LineMulti 在数据不对齐时,横坐标计算错误
  84. let data = [];
  85. xAxisData.forEach((x) => {
  86. let dataArr = props.chartData.filter((item) => type === item.type && item.name == x);
  87. if (dataArr && dataArr.length > 0) {
  88. data.push(dataArr[0].value);
  89. } else {
  90. data.push(null);
  91. }
  92. });
  93. // update-end-author:liusq date:2023-7-12 for: [issues/613] LineMulti 在数据不对齐时,横坐标计算错误
  94. //data数据
  95. obj['data'] = data;
  96. seriesData.push(obj);
  97. });
  98. option.series = seriesData;
  99. option.xAxis.data = xAxisData;
  100. console.log('option', option);
  101. setOptions(option);
  102. getInstance()?.off('click', onClick);
  103. getInstance()?.on('click', onClick);
  104. }
  105. function onClick(params) {
  106. emit('click', params);
  107. }
  108. return { chartRef };
  109. },
  110. });
  111. </script>