wujingjing
2024-07-16 015898efa6b1149cbb79fb2b2898ea6b13c20d2e
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
<template>
    <el-tabs v-model="activeName" class="demo-tabs min-w-[38rem] flex-column" @tab-click="handleClick">
        <el-tab-pane class="h-full" label="Chart" name="first">
            <!-- <el-select class="flex-0 w-36" 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="flex-auto">
                <div ref="chartRef" class="h-full"></div>
            </div>
        </el-tab-pane>
        <el-tab-pane class="h-full" label="Data" name="second">
            <el-table :data="data.values" style="width: 100%">
                <el-table-column v-for="(item, index) in data?.names" :label="item" :key="index">
                    <template #default="scope">
                        {{ scope.row[index] }}
                    </template>
                </el-table-column>
            </el-table>
        </el-tab-pane>
    </el-tabs>
</template>
<script lang="ts" setup>
import * as echarts from 'echarts';
import type { TabsPaneContext } from 'element-plus';
import type { PropType } from 'vue';
import { onMounted, ref } from 'vue';
import { ChartTypeEnum, chartTypeMapEchart, chartTypeMapName } from '../types';
import type { RecordSetValues } from '/@/api/ai/chat';
import { dateRegex } from '/@/utils/toolsValidate';
import { PATH_ICON, SCATTER_SYMBOL_SIZE, timeDataOptionToContent } from '../common';
const activeName = ref('first');
const chartRef = ref<HTMLDivElement>(null);
const selectChartType = ref<ChartTypeEnum>(ChartTypeEnum.Line);
 
const props = defineProps({
    data: {
        type: Object as PropType<RecordSetValues>,
    },
});
 
const selectChartTypeChange = () => {
    drawChart();
};
 
const handleClick = (tab: TabsPaneContext, event: Event) => {};
 
let chartInstance: echarts.ECharts = null;
 
const getXType = (xData?: any[]) => {
    const xValue = xData?.[0];
    if (xValue == null) return 'value';
    if (typeof xValue === 'number') {
        return 'value';
    } else if (typeof xValue === 'string') {
        const isValidDate = dateRegex.test(xValue);
        return isValidDate ? 'time' : 'category';
    } else {
        throw '图表x轴数据类型异常';
    }
};
 
const drawChart = () => {
    const xData = props.data.values?.map((item) => item[0]);
    const xType = getXType(xData);
    chartInstance.setOption({
        grid: {
            // bottom: 120,
            right: '15%',
            bottom: '5%',
        },
        tooltip: {
            show: true,
            trigger: 'axis',
        },
        toolbox: {
            show: true,
            feature: {
                dataZoom: {
                    yAxisIndex: 'none',
                },
                myBar: {
                    title: '转化为柱状图',
                    show: true,
                    icon: PATH_ICON.bar,
                    onclick: () => {
                        chartInstance.setOption({
                            series: {
                                data: props.data.values,
                                type: 'bar',
                                symbol: 'none',
                            },
                        });
                    },
                },
 
                myScatter: {
                    title: '转化为散点图',
                    show: true,
                    icon: PATH_ICON.scatter,
                    onclick: () => {
                        chartInstance.setOption({
                            data: props.data.values,
                            type: 'scatter',
                            symbol: 'circle',
                            symbolSize: SCATTER_SYMBOL_SIZE,
                        });
                    },
                },
                myLine: {
                    title: '转化为曲线图',
                    show: true,
                    icon: PATH_ICON.line,
                    onclick: () => {
                        chartInstance.setOption({
                            data: props.data.values,
                            type: 'line',
                            symbol: 'none',
                            smooth: true,
                        });
                    },
                },
                dataView: {
                    readOnly: true,
                    optionToContent: timeDataOptionToContent,
                },
                saveAsImage: {},
            },
        },
 
        title: {
            text: props.data.title,
            left: 'center',
        },
        xAxis: {
            name: props.data?.names[0],
            type: xType,
        },
        yAxis: {
            name: props.data?.names[1],
            type: 'value',
        },
        series: [
            {
                data: props.data.values,
                symbol: 'none',
                smooth: true,
                type: chartTypeMapEchart[selectChartType.value],
            },
        ],
        dataZoom: {
            type: 'inside',
        },
    });
};
 
onMounted(() => {
    setTimeout(() => {
        const parent = chartRef.value.parentElement;
        if (!parent) {
        }
        const parentBound = parent.getBoundingClientRect();
        chartInstance = echarts.init(chartRef.value, undefined, {
            width: parentBound.width,
            height: parentBound.height,
        });
 
        drawChart();
    }, 300);
});
</script>
 
<style scoped lang="scss">
.el-tabs {
    :deep(.el-tabs__header) {
        flex: 0 0 auto;
    }
    :deep(.el-tabs__content) {
        flex: 1;
        .el-tab-pane {
            display: flex;
            flex-direction: column;
            min-height: 24rem;
        }
    }
}
</style>