LineMulti.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. export default defineComponent({
  8. name: 'LineMulti',
  9. props: {
  10. /**
  11. * 图表数据,配合xAxisPropType、propTypeArr使用
  12. */
  13. chartData: {
  14. type: Array,
  15. default: () => [],
  16. required: true,
  17. },
  18. option: {
  19. type: Object,
  20. default: () => ({}),
  21. },
  22. /**
  23. * x轴数据对应的prop,将从chartData中映射prop到一个Set中。
  24. *
  25. * @example
  26. * ```js
  27. * const props = { xAxisPropType: 'name', chartData: [{ name: 'a' }, { name: 'b' }] }
  28. *
  29. * // truns out
  30. * option.xAxis.data = ['a', 'b'];
  31. * ```
  32. */
  33. xAxisPropType: {
  34. type: String,
  35. required: true,
  36. },
  37. /**
  38. * 提供一个Map,该Map将用于生成基于chartData的echart.series数据。
  39. *
  40. * @example
  41. * ```js
  42. * const props = { propTypeArr: new Map([['valueA', '值A']]), chartData: [{ valueA: 'a' }, { valueA: 'b' }] }
  43. *
  44. * // truns out
  45. * option.series = [{ name: '值A', data: ['a', 'b'] }]
  46. * ```
  47. */
  48. propTypeArr: {
  49. type: Map,
  50. default: () => new Map(),
  51. required: true,
  52. },
  53. type: {
  54. type: String as PropType<string>,
  55. default: 'line',
  56. },
  57. width: {
  58. type: String as PropType<string>,
  59. default: '100%',
  60. },
  61. height: {
  62. type: String as PropType<string>,
  63. default: 'calc(100vh - 78px)',
  64. },
  65. },
  66. emits: ['click'],
  67. setup(props, { emit }) {
  68. const chartRef = ref<HTMLDivElement | null>(null);
  69. const { setOptions, getInstance } = useECharts(chartRef as Ref<HTMLDivElement>);
  70. const option = reactive({
  71. tooltip: {
  72. trigger: 'axis',
  73. axisPointer: {
  74. type: 'shadow',
  75. label: {
  76. show: true,
  77. backgroundColor: '#333',
  78. },
  79. },
  80. },
  81. legend: {
  82. top: 10,
  83. },
  84. grid: {
  85. top: 60,
  86. },
  87. xAxis: {
  88. type: 'category',
  89. data: [],
  90. },
  91. yAxis: {
  92. type: 'value',
  93. },
  94. series: [],
  95. });
  96. watchEffect(() => {
  97. props.chartData && initCharts();
  98. });
  99. function initCharts() {
  100. if (props.option) {
  101. Object.assign(option, props.option);
  102. }
  103. //图例类型
  104. // let typeArr = Array.from(new Set(props.chartData.map((item) => item.type)));
  105. //轴数据
  106. let xAxisData = Array.from(new Set(props.chartData.map((item) => item[props.xAxisPropType])));
  107. let seriesData = [];
  108. [...props.propTypeArr.keys()].forEach((filed) => {
  109. let obj = { name: props.propTypeArr.get(filed), type: props.type };
  110. // let chartArr = props.chartData.filter((item) => filed === item.type);
  111. //data数据
  112. obj['data'] = props.chartData.map((item) => item[filed]);
  113. seriesData.push(obj);
  114. });
  115. option.series = seriesData;
  116. option.xAxis.data = xAxisData;
  117. setOptions(option, false);
  118. getInstance()?.off('click', onClick);
  119. getInstance()?.on('click', onClick);
  120. }
  121. function onClick(params) {
  122. emit('click', params);
  123. }
  124. return { chartRef };
  125. },
  126. });
  127. </script>