BarMulti.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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: 'BarMulti',
  9. props: {
  10. chartData: {
  11. type: Array,
  12. default: () => [],
  13. required: true,
  14. },
  15. option: {
  16. type: Object,
  17. default: () => ({}),
  18. },
  19. type: {
  20. type: String as PropType<string>,
  21. default: 'bar',
  22. },
  23. xAxisPropType: {
  24. type: String,
  25. required: true,
  26. },
  27. propTypeArr: {
  28. type: Map,
  29. default: () => new Map(),
  30. required: true,
  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. textStyle: {
  59. color: '#ffffffee',
  60. },
  61. },
  62. grid: {
  63. left: 60,
  64. right: 50,
  65. bottom: 50,
  66. },
  67. xAxis: {
  68. type: 'category',
  69. data: [],
  70. },
  71. yAxis: {
  72. type: 'value',
  73. nameTextStyle: {
  74. fontSize: 14,
  75. },
  76. },
  77. series: [],
  78. });
  79. watchEffect(() => {
  80. props.chartData && initCharts();
  81. });
  82. function initCharts() {
  83. if (props.option) {
  84. Object.assign(option, props.option);
  85. }
  86. //图例类型
  87. // let typeArr = Array.from(new Set(props.chartData.map((item) => item.type)));
  88. //轴数据
  89. let xAxisData = Array.from(new Set(props.chartData.map((item) => item[props.xAxisPropType])));
  90. let seriesData = [];
  91. [...props.propTypeArr.keys()].forEach((type) => {
  92. let obj = { name: props.propTypeArr.get(type), type: props.type };
  93. //data数据
  94. obj['data'] = props.chartData.map((item) => item[type]);
  95. seriesData.push(obj);
  96. });
  97. option.series = seriesData;
  98. option.xAxis.data = xAxisData;
  99. setOptions(option, false);
  100. getInstance()?.off('click', onClick);
  101. getInstance()?.on('click', onClick);
  102. }
  103. function onClick(params) {
  104. emit('click', params);
  105. }
  106. return { chartRef };
  107. },
  108. });
  109. </script>