BarAndLine.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <div v-if="spinning" class="spinning">
  3. <a-spin :spinning="true" />
  4. </div>
  5. <div v-if="!spinning" ref="chartRef" :style="{ height, width }"></div>
  6. </template>
  7. <script lang="ts">
  8. import { defineComponent, PropType, ref, Ref, reactive, watchEffect, watch, nextTick } from 'vue';
  9. import { useECharts } from '/@/hooks/web/useECharts';
  10. import { getTableHeaderColumns } from '/@/hooks/web/useWebColumns';
  11. import EchartsUtil from '/@/utils/echartsUtil';
  12. export default defineComponent({
  13. name: 'BarAndLine',
  14. props: {
  15. chartsColumns: {
  16. type: Array,
  17. default: () => [],
  18. },
  19. chartsColumnsType: {
  20. type: String,
  21. },
  22. chartsType: {
  23. type: String,
  24. default: 'listMonitor',
  25. },
  26. dataSource: {
  27. type: Array,
  28. default: () => [],
  29. },
  30. option: {
  31. type: Object,
  32. default: () => ({}),
  33. },
  34. xAxisPropType: {
  35. type: String,
  36. required: true,
  37. },
  38. width: {
  39. type: String as PropType<string>,
  40. default: '100%',
  41. },
  42. height: {
  43. type: String as PropType<string>,
  44. default: 'calc(100vh - 78px)',
  45. },
  46. },
  47. emits: ['refresh'],
  48. setup(props, { emit }) {
  49. const spinning = ref<boolean>(true);
  50. const chartRef = ref<HTMLDivElement | null>(null);
  51. const { setOptions, echarts } = useECharts(chartRef as Ref<HTMLDivElement>);
  52. const chartData = props.chartsColumnsType ? getTableHeaderColumns(props.chartsColumnsType) : [];
  53. let chartsColumns = props.chartsColumns.length > 0 ? props.chartsColumns : chartData;
  54. const option = reactive({
  55. name: '',
  56. color: ['#7B68EE', '#0000CD', '#6495ED', '#00BFFF', '#AFEEEE', '#008080', '#00FA9A', '#2E8B57', '#FAFAD2', '#DAA520'],
  57. tooltip: {},
  58. grid: {},
  59. toolbox: {
  60. feature: {
  61. saveAsImage: {
  62. iconStyle: {
  63. normal: {
  64. borderColor: '#ffffff',
  65. },
  66. },
  67. show: true,
  68. },
  69. },
  70. },
  71. dataZoom: {},
  72. legend: {},
  73. timeline: null,
  74. xAxis: null,
  75. yAxis: null,
  76. series: null,
  77. });
  78. let optionUtil;
  79. watchEffect(() => {
  80. props.dataSource && props.dataSource.length > 0 && option.series && initCharts();
  81. });
  82. watch([() => props.chartsType, () => props.chartsColumns], ([newChartsType, newChartsColumns]) => {
  83. spinning.value = true;
  84. chartsColumns = newChartsColumns;
  85. optionUtil.initChartOption(newChartsType, newChartsColumns);
  86. spinning.value = false;
  87. initCharts(true);
  88. emit('refresh');
  89. });
  90. function initChartsOption() {
  91. optionUtil = new EchartsUtil(Object.assign(option, props.option));
  92. optionUtil.initChartOption(props.chartsType, chartsColumns);
  93. }
  94. initChartsOption();
  95. function initCharts(isRefresh = false) {
  96. //轴数据
  97. if (option.series && option.series.length === chartsColumns.length) {
  98. let xAxisData = props.dataSource.map((item) => item[props.xAxisPropType]);
  99. [...chartsColumns].forEach((propType: any, index) => {
  100. option.series[index].data = props.dataSource.map((item) => item[propType.dataIndex] || 0);
  101. });
  102. option.xAxis[0].data = xAxisData;
  103. console.log('echarts监测列表数据', option.xAxis[0].data)
  104. setOptions(option, isRefresh);
  105. }
  106. }
  107. setTimeout(() => {
  108. spinning.value = false;
  109. }, 1000);
  110. return { chartRef, spinning };
  111. },
  112. });
  113. </script>
  114. <style lang="less" scoped>
  115. .spinning {
  116. display: flex;
  117. width: 100%;
  118. height: 100%;
  119. justify-content: center;
  120. align-items: center;
  121. }
  122. </style>