wujingjing
2024-07-25 022e0a3889e6510cd1bcc1ca6f18f00b3a62b638
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
<template>
    <div class="h-[20rem] w-full" v-resize="chartContainerResize">
        <div ref="chartRef"></div>
    </div>
</template>
 
<script setup lang="ts">
import type * as echarts from 'echarts';
import _ from 'lodash';
import { ref } from 'vue';
import { SCATTER_SYMBOL_SIZE, chatComProps, getChatChartOption } from '../../../common';
import { useDrawChatChart } from '../../../hooks/useDrawChatChart';
const chartRef = ref<HTMLDivElement>(null);
const defaultDisplayType = 'line';
 
const props = defineProps(chatComProps);
 
const drawChart = () => {
    const data = props.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: 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 });
</script>
<style scoped lang="scss"></style>