wujingjing
2024-07-04 89323ef5405d9e1721b85afcba55984feb9d1942
src/components/chat/chatComponents/summaryCom/SummaryCom.vue
@@ -1,6 +1,176 @@
<template>
    <div></div>
   <div class="min-w-[100ch]">
      <template v-if="summaryList && summaryList.length > 0">
         <div class="ml-10" v-for="(item, idx) in summaryList" :key="idx">
            <h3>{{ item.title }}</h3>
            <el-table class="mt-2" :data="item.values" :style="{ width: `${columnsWidth * (item.values?.length ?? 1)}px` }">
               <el-table-column v-for="(col, index) in item.values" :label="col.title" :key="index">
                  <template #default="scope">
                     {{ col?.value }}
                  </template>
               </el-table-column>
            </el-table>
         </div>
      </template>
      <div class="flex-column w-full" v-if="recordSetList && recordSetList.length > 0">
         <el-select class="flex-0 w-36 ml-auto mr-16" v-model="selectChartType" @change="selectChartTypeChange">
            <el-option
               v-for="item in Object.keys(chartTypeMapName)"
               :key="item"
               :value="parseInt(item)"
               :label="chartTypeMapName[item]"
            ></el-option>
         </el-select>
         <div class="min-h-[48rem] flex-auto w-full">
            <div ref="chartRefList" v-for="(item, index) in recordSetList" :key="index"></div>
         </div>
      </div>
   </div>
</template>
<script setup lang="ts"></script>
<script setup lang="ts">
import * as echarts from 'echarts';
import _ from 'lodash';
import { computed, onMounted, ref } from 'vue';
import { ChartTypeEnum, chartTypeMapEchart, chartTypeMapName } from '../types';
import { axisLabelFormatter } from '/@/utils/chart';
const props = defineProps(['data']);
const selectChartType = ref<ChartTypeEnum>(ChartTypeEnum.Line);
const columnsWidth = 120;
const chartRef = ref<HTMLDivElement[] | HTMLDivElement>(null);
const chartRefList = computed(() => {
   let refList: HTMLDivElement[] = [];
   if (!chartRef.value) {
      refList = [];
   } else {
      const first = chartRef.value?.[0];
      if (first) {
         refList = chartRef.value as unknown as HTMLDivElement[];
      } else {
         refList = [chartRef.value as unknown as HTMLDivElement];
      }
   }
   return refList;
});
const recordSetList = computed(() => props.data.filter((item) => item.type === 'recordset'));
const summaryList = computed(() => props.data.filter((item) => item.type === 'summary'));
const drawAllChart = () => {
   chartInstanceList.map((item, index) => {
      drawChart(item, recordSetList.value[index]);
   });
};
const selectChartTypeChange = () => {
   drawAllChart();
};
const drawChart = (instance, data) => {
   const xType = 'time';
   let timeIndex = data.cols.findIndex((item) => item.type === 'time');
   if (timeIndex === -1) {
      timeIndex = 0;
   }
   const timeCol = data.cols[timeIndex];
   let valueIndex = data.cols.findIndex((item) => item.type === 'value');
   if (valueIndex === -1) {
      valueIndex = 2;
   }
   const valueCol = data.cols[valueIndex];
   let nameCol = null;
   let groupedValues = null;
   if (data.chart === 'muli_line') {
      let nameIndex = data.cols.findIndex((item) => item.type === 'name');
      if (nameIndex === -1) {
         nameIndex = 1;
      }
      nameCol = data.cols[nameIndex];
      groupedValues = _.groupBy(data.values, (item) => item[nameIndex]);
   } else if (data.chart === 'single_line') {
      groupedValues = {
         default: data.values,
      };
   } else {
      // 默认都当muli_line
      let nameIndex = data.cols.findIndex((item) => item.type === 'name');
      if (nameIndex === -1) {
         nameIndex = 1;
      }
      nameCol = data.cols[nameIndex];
      groupedValues = _.groupBy(data.values, (item) => item[nameIndex]);
   }
   const seriesData = Object.keys(groupedValues).map((item) => {
      const values = groupedValues[item];
      return {
         name: item === 'default' ? '' : item,
         data: values.map((item) => [item[timeIndex], item[valueIndex]]),
         type: chartTypeMapEchart[selectChartType.value],
      };
   });
   instance.setOption({
      grid: {
         // bottom: 120,
         // right: '15%',
         bottom: '15%',
      },
      tooltip: {
         show: true,
      },
      title: {
         text: data?.title,
         left: 'center',
      },
      xAxis: {
         name: timeCol?.title,
         type: xType,
      },
      yAxis: {
         name: valueCol?.title,
         type: 'value',
         axisLabel: {
            formatter: axisLabelFormatter,
         },
      },
      series: seriesData,
      dataZoom: {
         type: 'inside',
      },
   });
};
let chartInstanceList: echarts.ECharts[] = null;
onMounted(() => {
   setTimeout(() => {
      const parent = chartRefList.value[0]?.parentElement;
      const data = props.data;
      if (!parent) {
         return;
      } else {
      }
      const parentBound = parent.getBoundingClientRect();
      let divideCount = 1;
      const width = parentBound.width / divideCount;
      chartInstanceList = chartRefList.value.map((item) => {
         return echarts.init(item, undefined, {
            width: width,
            height: parentBound.height,
         });
      });
      drawAllChart();
   }, 1000);
});
</script>
<style scoped lang="scss"></style>