wujingjing
2024-07-25 ec939b38e899676e4dc117b1d4f3468da2607777
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<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>