yangyin
2024-07-17 b065f956b99dc1afc27e67a21e5b5480ccc55738
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<template>
    <div style="width: calc(100ch - 28px)">
        <template v-if="data && data.length > 0">
            <template v-if="summaryList && summaryList.length > 0">
                <div class="w-full" v-for="(item, idx) in summaryList" :key="idx">
                    <h3>{{ item.title }}</h3>
                    <el-table ref="tableRefList" class="w-full" :data="[{}]">
                        <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"
                :class="{ 'mt-10': summaryList && summaryList.length > 0 }"
                v-if="recordSetList && recordSetList.length > 0"
            >
                <div class="min-h-[48rem] flex-auto w-full">
                    <div ref="chartRefList" v-for="(item, index) in recordSetList" :key="index"></div>
                </div>
            </div>
            <div v-if="urlList && urlList.length > 0" class="w-full">
                <HTMLCom :data="`${SERVE_URL}/views/project/ch/demo/Overview.html`" v-for="(item, index) in urlList" :key="index"></HTMLCom>
            </div>
        </template>
        <div v-else class="text-red-500">{{ error }}</div>
    </div>
</template>
 
<script setup lang="ts">
import * as echarts from 'echarts';
import type { TableInstance } from 'element-plus';
import _ from 'lodash';
import { computed, onMounted, ref } from 'vue';
import { AnswerType } from '../../model/types';
import { PATH_ICON, SCATTER_SYMBOL_SIZE, timeDataOptionToContent } from '../common';
import HTMLCom from '../htmlCom/HTMLCom.vue';
import { ChartTypeEnum, chartTypeMapEchart } from '../types';
import { SERVE_URL } from '/@/constants/index';
import { axisLabelFormatter } from '/@/utils/chart';
const props = defineProps(['data', 'error']);
 
const selectChartType = ref<ChartTypeEnum>(ChartTypeEnum.Line);
 
const columnsWidth = 120;
 
const chartRef = ref<HTMLDivElement[] | HTMLDivElement>(null);
const tableRef = ref<TableInstance>(null);
const tableRefList = computed(() => {
    let refList: TableInstance[] = [];
    if (!tableRef.value) {
        refList = [];
    } else {
        const first = tableRef.value?.[0];
        if (first) {
            refList = tableRef.value as unknown as TableInstance[];
        } else {
            refList = [tableRef.value as unknown as TableInstance];
        }
    }
 
    return refList;
});
 
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 === AnswerType.RecordSet));
const summaryList = computed(() => props.data.filter((item) => item.type === AnswerType.Summary));
const urlList = computed(() => props.data.filter((item) => item.type === AnswerType.Url));
 
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],
            symbol: 'none',
            smooth: true,
        };
    });
    instance.setOption({
        grid: {
            // bottom: 120,
            // right: '15%',
            bottom: '5%',
        },
        toolbox: {
            show: true,
            feature: {
                dataZoom: {
                    yAxisIndex: 'none',
                },
 
                myBar: {
                    title: '转化为柱状图',
                    show: true,
                    icon: PATH_ICON.bar,
                    onclick: () => {
                        instance.setOption({
                            series: seriesData.map((item) => ({
                                ...item,
                                type: 'bar',
                                symbol: 'none',
                            })),
                        });
                    },
                },
 
                myScatter: {
                    title: '转化为散点图',
                    show: true,
                    icon: PATH_ICON.scatter,
                    onclick: () => {
                        instance.setOption({
                            series: seriesData.map((item) => ({
                                ...item,
                                type: 'scatter',
                                symbol: 'circle',
                                symbolSize: SCATTER_SYMBOL_SIZE,
                            })),
                        });
                    },
                },
                myLine: {
                    title: '转化为曲线图',
                    show: true,
                    icon: PATH_ICON.line,
                    onclick: () => {
                        instance.setOption({
                            series: seriesData.map((item) => ({
                                ...item,
                                type: 'line',
                                symbol: 'none',
                                smooth: true,
                            })),
                        });
                    },
                },
                dataView: {
                    readOnly: true,
                    optionToContent: timeDataOptionToContent,
                },
                saveAsImage: {},
            },
        },
 
        tooltip: {
            show: true,
            trigger: 'axis',
        },
        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',
        },
    });
};
 
const doTableLayout = () => {
    tableRefList.value.forEach((item) => {
        item.doLayout();
    });
};
let chartInstanceList: echarts.ECharts[] = null;
onMounted(() => {
    setTimeout(() => {
        const parent = chartRefList.value[0]?.parentElement;
        const data = props.data;
        setTimeout(() => {
            doTableLayout();
        }, 3000);
 
        if (!parent) {
            return;
        }
 
        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>