<template>
|
<div class="w-full">
|
<div class="flex space-x-2 mb-4" v-if="data?.params && data?.params.length > 0">
|
<component
|
v-model="paramsValueList[index].value"
|
v-for="(item, index) in data?.params"
|
:key="item.id"
|
:id="item.id"
|
:is="recordSetMapCom[item.type]"
|
:data="item"
|
:originData="originData"
|
@change="(val) => handleQueryChange(val, item)"
|
:disabled="chartLoading"
|
></component>
|
</div>
|
<div class="h-[20rem]" v-resize="chartContainerResize" v-loading="chartLoading">
|
<div ref="chartRef"></div>
|
</div>
|
</div>
|
</template>
|
|
<script setup lang="ts">
|
import type * as echarts from 'echarts';
|
import _ from 'lodash';
|
import type { PropType } from 'vue';
|
import { ref, watch } from 'vue';
|
import { SCATTER_SYMBOL_SIZE, chatComProps, getChatChartOption } from '../../../common';
|
import { useDrawChatChart } from '../../../hooks/useDrawChatChart';
|
import type { RecordSet, RecordSetParamsItem } from './types';
|
import { recordSetMapCom } from './types';
|
import { filterQuery } from '/@/api/ai/chat';
|
|
const chartRef = ref<HTMLDivElement>(null);
|
const defaultDisplayType = 'line';
|
|
// const props = defineProps({
|
// data: {
|
// type: Object as PropType<RecordSet>,
|
// },
|
// });
|
|
const props = defineProps({
|
data: {
|
type: Object as PropType<any>,
|
},
|
originData: {
|
type: Object as PropType<any>,
|
},
|
summaryIndex: {
|
type: Number,
|
},
|
}) as {
|
data: RecordSet;
|
};
|
const chartLoading = ref(false);
|
const paramsValueList = ref(props.data?.params);
|
|
let groupedValues = null;
|
let timeIndex = undefined;
|
let valueIndex = undefined;
|
const drawChart = () => {
|
const data = props.data;
|
const xType = 'time';
|
timeIndex = data.cols.findIndex((item) => item.type === 'time');
|
if (timeIndex === -1) {
|
timeIndex = 0;
|
}
|
const timeCol = data.cols[timeIndex];
|
|
valueIndex = data.cols.findIndex((item) => item.type === 'value');
|
if (valueIndex === -1) {
|
valueIndex = 2;
|
}
|
const valueCol = data.cols[valueIndex];
|
|
let nameCol = null;
|
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: defaultDisplayType,
|
symbol: 'none',
|
smooth: true,
|
};
|
});
|
const combineOption = _.defaultsDeep(getChatChartOption(), {
|
grid: {
|
bottom: 20,
|
},
|
toolbox: {
|
show: true,
|
feature: {
|
myBar: {
|
onclick: () => {
|
chartInstance.value.setOption({
|
series: seriesData.map((item) => ({
|
...item,
|
type: 'bar',
|
symbol: 'none',
|
})),
|
});
|
},
|
},
|
|
myScatter: {
|
onclick: () => {
|
chartInstance.value.setOption({
|
series: seriesData.map((item) => ({
|
...item,
|
type: 'scatter',
|
symbol: 'circle',
|
symbolSize: SCATTER_SYMBOL_SIZE,
|
})),
|
});
|
},
|
},
|
myLine: {
|
onclick: () => {
|
chartInstance.value.setOption({
|
series: seriesData.map((item) => ({
|
...item,
|
type: 'line',
|
symbol: 'none',
|
smooth: true,
|
})),
|
});
|
},
|
},
|
},
|
},
|
|
title: {
|
text: data?.title,
|
},
|
xAxis: {
|
name: timeCol?.title,
|
},
|
yAxis: {
|
name: valueCol?.title,
|
},
|
series: seriesData,
|
} as echarts.EChartsOption);
|
chartInstance.value.setOption(combineOption);
|
};
|
const { chartContainerResize, chartInstance } = useDrawChatChart({ chartRef, drawChart });
|
|
// 更换列表
|
const changeMap = new Map<string,string>(null);
|
|
const handleQueryChange = async (val: string, item: RecordSetParamsItem) => {
|
if (!val) return;
|
const historyId = (props as any).originData.historyId;
|
const summaryIndex = (props as any).summaryIndex;
|
let res = null;
|
|
try {
|
|
changeMap.set(item.id,val);
|
const paramsObj = {};
|
for (const [key,value] of changeMap) {
|
paramsObj[key] = value;
|
}
|
const params = {
|
history_id: historyId,
|
query_index: summaryIndex,
|
param_json: JSON.stringify(paramsObj),
|
};
|
res = await filterQuery(params);
|
chartLoading.value = true;
|
} finally {
|
chartLoading.value = false;
|
}
|
|
const title = res?.values?.title;
|
const values = res?.values?.values ?? [];
|
chartInstance.value.setOption({
|
title: {
|
text: title,
|
},
|
series:
|
groupedValues &&
|
Object.keys(groupedValues).map(() => {
|
return {
|
data: values.map((item) => [item[timeIndex], item[valueIndex]]),
|
};
|
}),
|
});
|
};
|
</script>
|
<style scoped lang="scss"></style>
|