gerson
2024-09-07 37179a65229455f540dc3ca62d31c4716e15d12d
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<template>
    <!-- 实时监测列表 -->
    <div ref="containerRef" class="w-full flex justify-center" v-resize="resizeHandler">
        <div class="inline-block">
            <div
                :class="`space-y-[${THICK_BORDER_WIDTH}px]`"
                :style="{
                    border: `${THICK_BORDER_WIDTH}px solid ${BORDER_COLOR}`,
                    backgroundColor: BORDER_COLOR,
                }"
            >
                <div class="flex-center font-bold text-base bg-[#8db4e2]" :style="{ height: `${CELL_HEIGHT}px` }">
                    {{ data.title }}
                </div>
                <div
                    v-for="(rowChunk, index) in currentRowChunkList"
                    :key="index"
                    :class="`space-y-[${THIN_BORDER_WIDTH}px]`"
                    :style="{
                        backgroundColor: BORDER_COLOR,
                    }"
                >
                    <!-- 表头,和表格内容第一行 -->
                    <div
                        :class="`space-y-[${THICK_BORDER_WIDTH}px]`"
                        :style="{
                            backgroundColor: BORDER_COLOR,
                        }"
                    >
                        <div
                            class="flex"
                            :class="`space-x-[${THICK_BORDER_WIDTH}px]`"
                            :style="{
                                backgroundColor: BORDER_COLOR,
                            }"
                        >
                            <div
                                v-for="(item, index) in maxColsNum"
                                :key="item"
                                :class="COL_HEADER_CELL_CLASS"
                                :style="{
                                    width: `${index === 0 ? firstColWidth : restColWidth}px`,
                                    height: `${CELL_HEIGHT}px`,
                                    backgroundColor: COL_HEADER_CELL_BG_COLOR,
                                }"
                            >
                                {{ index === 0 ? '' : rowChunk[index - 1]?.OTITLE }}
                            </div>
                        </div>
                        <MonitorContent
                            v-if="firstRow"
                            :firstColWidth="firstColWidth"
                            :restColWidth="restColWidth"
                            :title="firstRow.title"
                            :type="firstRow.id"
                            :values="rowChunk"
                            @itemClick="valueClick"
                        />
                    </div>
                    <!-- 剩余行 -->
                    <MonitorContent
                        v-for="(row, index) in restRows"
                        :key="index"
                        :firstColWidth="firstColWidth"
                        :restColWidth="restColWidth"
                        :title="row.title"
                        :type="row.id"
                        :values="rowChunk"
                        @itemClick="valueClick"
                    />
                </div>
            </div>
            <el-pagination
                style="margin-bottom: 0 !important"
                small
                hide-on-single-page
                v-model:current-page="pageIndex"
                v-model:page-size="pageSize"
                layout="total,prev,pager,next,jumper"
                :total="total"
                @current-change="handleCurrentChange"
            />
        </div>
        <RecordSetDialog v-model="chartDlgIsShow" :otype="chartDlgMapRow?.OTYPE" :oname="chartDlgMapRow?.ONAME" :indexName="indexName"/>
    </div>
</template>
 
<script setup lang="ts">
import _ from 'lodash';
import { computed, onActivated, onMounted, ref } from 'vue';
import MonitorContent from './MonitorContent.vue';
import { debounce, getTextWidth } from '/@/utils/util';
 
import { chatComProps } from '../../../common';
import {
    BORDER_COLOR,
    CELL_HEIGHT,
    CELL_MAX_WIDTH,
    COL_HEADER_CELL_BG_COLOR,
    COL_HEADER_CELL_CLASS,
    PAGE_HEIGHT,
    ROW_HEADER_CELL_MAX_WIDTH,
    THICK_BORDER_WIDTH,
    THIN_BORDER_WIDTH,
} from './constants';
import type { Monitor, MonitorValue } from './types';
import RecordSetDialog from '../recordSet/RecordSetDialog.vue';
 
const props = defineProps(chatComProps) as {
    data: Monitor;
};
const total = props.data?.values?.length ?? 0;
const pageIndex = ref(null);
const pageSize = ref(null);
 
const containerRef = ref<HTMLDivElement>(null);
const measureWidthOffset = 2;
/** @description 测量首列内容宽度 */
const rowHeaderCellContentWidth = computed(() => {
    if (!props.data.rows || props.data.rows.length === 0) return 0;
    let maxLen = 0;
    let maxTitle = '';
    for (const item of props.data.rows) {
        const { title } = item;
        const len = title.gblen();
        if (len > maxLen) {
            maxLen = len;
            maxTitle = title;
        }
    }
    let maxWidth = getTextWidth(maxTitle, {
        size: '0.875rem',
    });
 
    maxWidth += measureWidthOffset;
 
    if (maxWidth > ROW_HEADER_CELL_MAX_WIDTH) {
        maxWidth = ROW_HEADER_CELL_MAX_WIDTH;
    }
 
    return maxWidth;
});
/** @description 测量其他列内容宽度 */
const colHeaderCellContentWidth = computed(() => {
    if (!props.data.values || props.data.values.length === 0) return 0;
    let maxLen = 0;
    let maxTitle = '';
    for (const item of props.data.values) {
        const { OTITLE } = item;
        const len = OTITLE.gblen();
        if (len > maxLen) {
            maxLen = len;
            maxTitle = OTITLE;
        }
    }
 
    let maxWidth = getTextWidth(maxTitle, {
        size: '0.875rem',
    });
    maxWidth += measureWidthOffset;
 
    if (maxWidth > CELL_MAX_WIDTH) {
        maxWidth = CELL_MAX_WIDTH;
    }
 
    return maxWidth;
});
 
const firstColWidth = ref(rowHeaderCellContentWidth.value);
const restColWidth = ref(colHeaderCellContentWidth.value);
const calcMaxRowsNum = (groupCount: number, height, extraHeight = 0) => {
    return Math.floor(
        (height - 2 * THICK_BORDER_WIDTH - CELL_HEIGHT - extraHeight) /
            (CELL_HEIGHT * groupCount + 2 * THICK_BORDER_WIDTH + THIN_BORDER_WIDTH * (groupCount - 2))
    );
};
let maxColsNum = ref<number>(null);
const resizeEvent = ({ width, height }) => {
    if (width === 0 || height === 0) {
        return;
    }
    // 按最大宽度算最大列数
    maxColsNum.value = Math.floor(
        (width - THICK_BORDER_WIDTH + colHeaderCellContentWidth.value - rowHeaderCellContentWidth.value) /
            (colHeaderCellContentWidth.value + THICK_BORDER_WIDTH)
    );
 
    const currentWidth =
        (maxColsNum.value - 1) * colHeaderCellContentWidth.value +
        rowHeaderCellContentWidth.value +
        THICK_BORDER_WIDTH * (maxColsNum.value - 1) +
        THICK_BORDER_WIDTH * 2;
    let restWidth = width - currentWidth;
    if (restWidth > 0) {
        // 尽可能分给第一列
        if (rowHeaderCellContentWidth.value + restWidth > ROW_HEADER_CELL_MAX_WIDTH) {
            restWidth = rowHeaderCellContentWidth.value + restWidth - ROW_HEADER_CELL_MAX_WIDTH;
            firstColWidth.value = ROW_HEADER_CELL_MAX_WIDTH;
        } else {
            firstColWidth.value = rowHeaderCellContentWidth.value + restWidth;
            restWidth = 0;
        }
 
        // 其余分给其他列
        if (restWidth !== 0) {
            const averageWidth = restWidth / (maxColsNum.value - 1);
            const currentWidth = colHeaderCellContentWidth.value + averageWidth;
            restColWidth.value = currentWidth > CELL_MAX_WIDTH ? CELL_MAX_WIDTH : currentWidth;
        }
    }
 
    const groupCount = (props.data?.rows?.length ?? 0) + 1;
 
    // 按最大高度算最大行数
    let rowsNum = calcMaxRowsNum(groupCount, height);
 
    // 按最大列数铺
    const maxColsRowsNum = Math.ceil(total / maxColsNum.value);
    const isNeedPage = maxColsRowsNum > rowsNum;
    rowsNum = isNeedPage ? calcMaxRowsNum(groupCount, height, PAGE_HEIGHT) : maxColsRowsNum;
    // rowsNum 行,maxColsNum列,第一列不算
    pageSize.value = isNeedPage
        ? rowsNum * (maxColsNum.value - 1)
        : total % (maxColsNum.value - 1) === 0
        ? total
        : (Math.floor(total / (maxColsNum.value - 1)) + 1) * (maxColsNum.value - 1);
    pageIndex.value = 1;
    // isNeedPage 是否分页,rowsNum 行数,maxColsNum 列数,
};
const resizeHandler = debounce(resizeEvent);
 
const handleCurrentChange = () => {};
 
const firstRow = computed(() => props.data?.rows?.[0]);
const restRows = computed(() => props.data?.rows?.slice(1));
const pageChunkList = computed(() => {
    const chunkResult = _.chunk(props.data.values ?? [], pageSize.value);
    const last = chunkResult.at(-1);
    if (last) {
        const restNum = pageSize.value - last.length;
        const emptyData = _.fill(Array(restNum), {
            ONAME: '',
            OTIME: '',
            OTITLE: '',
            OTYPE: '',
        } as MonitorValue);
        const lastData = last.concat(emptyData);
        chunkResult[chunkResult.length - 1] = lastData;
    }
 
    return chunkResult;
});
const currentPageChunk = computed(() => {
    pageChunkList.value;
    if (pageIndex.value == null) return [];
    const pageChunk = pageChunkList.value[pageIndex.value - 1];
 
    return pageChunk;
});
 
const currentRowChunkList = computed(() => {
    if (!currentPageChunk.value || currentPageChunk.value.length === 0) return [];
    const chunkResult = _.chunk(currentPageChunk.value, maxColsNum.value - 1);
    return chunkResult;
});
 
//#region ====================== 点击看曲线 ======================
 
const chartDlgIsShow = ref(false);
const chartDlgMapRow = ref(null);
/** @description 指标名称 */
const indexName = ref(null);
const valueClick = (item,type) => {
    chartDlgMapRow.value = item;
    chartDlgIsShow.value = true;
    indexName.value = type;
};
//#endregion
 
// 计算最大列数
// (x-1)* cellWidth + rowHeaderCellContentWidth.value+thickBorderWidth*(x-1)+thickBorderWidth*2 <= width;
 
// x*(cellWidth+thickBorderWidth)+thickBorderWidth - cellWidth + rowHeaderCellContentWidth.value<=width;
// x<= (width-thickBorderWidth + cellWidth-rowHeaderCellContentWidth.value)/(cellWidth+thickBorderWidth);
 
// const groupCount = (TEST_DATA?.rows?.length ?? 0) + 1;
// 计算最大行数
// y * (cellHeight * groupCount) +
//     (y - 1) * (2 * thickBorderWidth) +cellHeight+thickBorderWidth
//     thickBorderWidth +
//     thickBorderWidth * 2 +
//     y * (groupCount - 2) * thinBorderWidth <=
//     height;
 
// (cellHeight * groupCount + 2 * thickBorderWidth + thinBorderWidth(groupCount - 2)  * y + thickBorderWidth <=height;
 
// y<= (height-thickBorderWidth)/(cellHeight * groupCount + 2 * thickBorderWidth + thinBorderWidth(groupCount - 2) )
 
onMounted(() => {
    resizeEvent({
        width: containerRef.value.clientWidth,
        height: 0.7 * document.body.clientHeight,
    });
});
</script>
<style scoped lang="scss">
:deep(.space-y-\[2px\] > :not([hidden]) ~ :not([hidden])) {
    --tw-space-y-reverse: 0;
    margin-top: calc(2px * calc(1 - var(--tw-space-y-reverse)));
    margin-bottom: calc(2px * var(--tw-space-y-reverse));
}
:deep(.space-y-\[1px\] > :not([hidden]) ~ :not([hidden])) {
    --tw-space-y-reverse: 0;
    margin-top: calc(1px * calc(1 - var(--tw-space-y-reverse)));
    margin-bottom: calc(1px * var(--tw-space-y-reverse));
}
 
:deep(.space-x-\[2px\] > :not([hidden]) ~ :not([hidden])) {
    --tw-space-x-reverse: 0;
    margin-right: calc(2px * var(--tw-space-x-reverse));
    margin-left: calc(2px * calc(1 - var(--tw-space-x-reverse)));
}
:deep(.space-x-\[1px\] > :not([hidden]) ~ :not([hidden])) {
    --tw-space-x-reverse: 0;
    margin-right: calc(1px * var(--tw-space-x-reverse));
    margin-left: calc(1px * calc(1 - var(--tw-space-x-reverse)));
}
</style>