-
zhangwei
3 天以前 7674a4144ba793196607578f8a57771d6beb22c7
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
<template>
    <div class="sys-databaseVisual-container" style="height: calc(100vh - 60px)">
        <RelationGraph ref="graphRef" :options="graphOptions" :on-node-click="onNodeClick" :on-line-click="onLineClick">
            <template #graph-plug>
                <div
                    style="
                        z-index: 300;
                        position: absolute;
                        left: 10px;
                        top: calc(100% - 50px);
                        font-size: 12px;
                        background-color: #ffffff;
                        border: #efefef solid 1px;
                        border-radius: 10px;
                        width: 260px;
                        height: 40px;
                        display: flex;
                        align-items: center;
                        justify-content: center;
                    "
                >
                    图例:
                    <div>
                        一对多
                        <div style="height: 5px; width: 80px; background-color: rgba(0, 255, 0, 0.5)"></div>
                    </div>
                    <div style="margin-left: 10px">
                        一对一
                        <div style="height: 5px; width: 80px; background-color: rgba(255, 0, 0, 0.5)"></div>
                    </div>
                </div>
            </template>
 
            <template #canvas-plug>
                <!--- You can put some elements that are not allowed to be dragged here --->
            </template>
 
            <template #node="{ node }">
                <div style="width: 500px; background-color: #f39930">
                    <div style="height: 30px; display: flex; align-items: center; justify-content: center">{{ node.text }} - 【{{ node.data.columns.length }}列】</div>
                    <table class="c-data-table">
                        <tr>
                            <th>列名</th>
                            <th>类型</th>
                            <th>长度</th>
                            <th>描述</th>
                        </tr>
                        <template v-for="column of node.data.columns" :key="column.columnName">
                            <tr>
                                <td>
                                    <div :id="`${node.id}-${column.columnName}`">{{ column.columnName }}</div>
                                </td>
                                <td>{{ column.dataType }}</td>
                                <td>{{ column.dataLength }}</td>
                                <td>{{ column.columnDescription }}</td>
                            </tr>
                        </template>
                    </table>
                </div>
            </template>
        </RelationGraph>
    </div>
</template>
 
<script lang="ts" setup name="databaseVisual">
import { onMounted, reactive, ref } from 'vue';
import { useRoute } from 'vue-router';
 
import RelationGraph from 'relation-graph/vue3';
import type { RGOptions, RGNode, RGLine, RGLink, RGUserEvent, RGJsonData, RelationGraphComponent } from 'relation-graph/vue3';
 
import { getAPI } from '/@/utils/axios-utils';
import { SysDatabaseApi } from '/@/api-services/api';
 
const route = useRoute();
const state = reactive({
    loading: false,
    configId: '' as any,
});
 
const graphRef = ref<RelationGraphComponent | null>(null);
const graphOptions: RGOptions = {
    defaultJunctionPoint: 'border',
};
 
onMounted(async () => {
    state.configId = route.query.configId;
    showGraph();
});
 
// 获取可视化表和字段
const showGraph = async () => {
    var res = await getAPI(SysDatabaseApi).apiSysDatabaseVisualDbTableGet();
    const visualTableList: any = res.data.result?.visualTableList;
    const visualColumnList: any = res.data.result?.visualColumnList;
    const columnRelationList: any = res.data.result?.columnRelationList;
 
    const graphNodes = visualTableList.map((table: any) => {
        const { tableName, tableComents, x, y } = table;
        return {
            id: tableName,
            text: tableComents,
            x,
            y,
            nodeShape: 1,
            data: {
                columns: visualColumnList.filter((col: any) => col.tableName === table.tableName),
            },
        };
    });
    const graphLines = columnRelationList.map((relation: any) => {
        return {
            from: relation.sourceTableName + '-' + relation.sourceColumnName, // HtmlElement id
            to: relation.targetTableName + '-' + relation.targetColumnName, // HtmlElement id
            color: relation.type === 'ONE_TO_ONE' ? 'rgba(255,0,0,0.5)' : 'rgba(0,255,0,0.5)',
            text: '',
            fromJunctionPoint: 'left',
            toJunctionPoint: 'lr',
            lineShape: 6,
            lineWidth: 3,
        };
    });
    const graphJsonData: RGJsonData = {
        nodes: graphNodes,
        lines: [],
        elementLines: graphLines,
    };
    const graphInstance = graphRef.value?.getInstance();
    if (graphInstance) {
        await graphInstance.setJsonData(graphJsonData);
        await graphInstance.moveToCenter();
        await graphInstance.zoomToFit();
    }
};
 
const onNodeClick = (nodeObject: RGNode, $event: RGUserEvent) => {
    console.log('onNodeClick:', nodeObject);
};
 
const onLineClick = (lineObject: RGLine, linkObject: RGLink, $event: RGUserEvent) => {
    console.log('onLineClick:', lineObject);
};
</script>
 
<style lang="scss" scoped>
::v-deep(.relation-graph) {
    .rel-node-shape-1 {
        overflow: hidden;
    }
}
.c-data-table {
    background-color: #ffffff;
    border-collapse: collapse;
    width: 100%;
}
.c-data-table td,
.c-data-table th {
    border: 1px solid #f39930;
    color: #333333;
    padding: 5px;
    padding-left: 20px;
    padding-right: 20px;
}
.c-data-table td div,
.c-data-table th div {
    background-color: var(--el-color-primary-light-3);
    color: #ffffff;
    border-radius: 5px;
}
</style>