LineMulti.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. chartData: {
  11. type: Array,
  12. default: () => [],
  13. required: true,
  14. },
  15. option: {
  16. type: Object,
  17. default: () => ({}),
  18. },
  19. xAxisPropType: {
  20. type: String,
  21. required: true,
  22. },
  23. propTypeArr: {
  24. type: Map,
  25. default: () => new Map(),
  26. required: true,
  27. },
  28. type: {
  29. type: String as PropType<string>,
  30. default: 'line',
  31. },
  32. width: {
  33. type: String as PropType<string>,
  34. default: '100%',
  35. },
  36. height: {
  37. type: String as PropType<string>,
  38. default: 'calc(100vh - 78px)',
  39. },
  40. },
  41. emits: ['click'],
  42. setup(props, { emit }) {
  43. const chartRef = ref<HTMLDivElement | null>(null);
  44. const { setOptions, getInstance } = useECharts(chartRef as Ref<HTMLDivElement>);
  45. const option = reactive({
  46. tooltip: {
  47. trigger: 'axis',
  48. axisPointer: {
  49. type: 'shadow',
  50. label: {
  51. show: true,
  52. backgroundColor: '#333',
  53. },
  54. },
  55. },
  56. legend: {
  57. top: 10,
  58. },
  59. grid: {
  60. top: 60,
  61. },
  62. xAxis: {
  63. type: 'category',
  64. data: [],
  65. },
  66. yAxis: {
  67. type: 'value',
  68. },
  69. series: [],
  70. });
  71. watchEffect(() => {
  72. props.chartData && initCharts();
  73. });
  74. function initCharts() {
  75. if (props.option) {
  76. Object.assign(option, props.option);
  77. }
  78. //图例类型
  79. // let typeArr = Array.from(new Set(props.chartData.map((item) => item.type)));
  80. //轴数据
  81. let xAxisData = Array.from(new Set(props.chartData.map((item) => item[props.xAxisPropType])));
  82. let seriesData = [];
  83. [...props.propTypeArr.keys()].forEach((type) => {
  84. let obj = { name: props.propTypeArr.get(type), type: props.type };
  85. let chartArr = props.chartData.filter((item) => type === item.type);
  86. //data数据
  87. obj['data'] = props.chartData.map((item) => item[type]);
  88. seriesData.push(obj);
  89. });
  90. option.series = seriesData;
  91. option.xAxis.data = xAxisData;
  92. setOptions(option, false);
  93. getInstance()?.off('click', onClick);
  94. getInstance()?.on('click', onClick);
  95. }
  96. function onClick(params) {
  97. emit('click', params);
  98. }
  99. return { chartRef };
  100. },
  101. });
  102. </script>