123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- import { ProjectInfo, RemarkInfo, TableItemType, TopicAreaInfo } from "@/type";
- import { Graph } from "@antv/x6";
- export const render = (graph: Graph, project: ProjectInfo) => {
- const { tables, relations, topicAreas, remarks } = project;
- // 渲染表格
- const renderTable = (tableItem: TableItemType) => {
- graph.addNode({
- shape: "table-node",
- x: 300,
- y: 100,
- width: 220,
- height: 69,
- id: tableItem.table.id,
- data: tableItem,
- zIndex: 3,
- ports: {
- groups: {
- // 字段名前连接桩
- columnPort: {
- markup: [
- {
- tagName: "rect",
- selector: "rect",
- },
- {
- tagName: "circle",
- selector: "circle",
- },
- ],
- position: {
- name: "absolute",
- args: {
- x: 12,
- y: 42,
- },
- },
- },
- },
- },
- });
- };
- // 渲染主题区域
- const renderTopicArea = (topicArea: TopicAreaInfo) => {
- graph.addNode({
- shape: "topic-node",
- x: 300,
- y: 100,
- width: 200,
- height: 200,
- id: topicArea.id,
- data: topicArea,
- zIndex: 0,
- });
- };
- // 渲染备注
- const renderRemark = (remark: RemarkInfo) => {
- const notice = graph?.addNode({
- shape: "notice-node",
- x: 300,
- y: 100,
- width: 200,
- height: 200,
- id: remark.id,
- data: remark,
- zIndex: 1,
- });
- };
- // 渲染关系
- const renderRelationEdge = (
- id: string,
- source: {
- tableId: string;
- columnId: string;
- },
- target: {
- tableId: string;
- columnId: string;
- }
- ) => {
- // 添加关系连线
- const relationEdge = graph?.addEdge({
- id,
- router: {
- name: "er",
- args: {
- offset: "center",
- direction: "H",
- },
- },
- connector: { name: "rounded" },
- attrs: {
- line: {
- stroke: "#333",
- strokeWidth: 1,
- targetMarker: null,
- },
- },
- source: {
- cell: source.tableId,
- port: source.columnId + "_port2",
- anchor: "left",
- },
- target: {
- cell: target.tableId,
- port: target.columnId + "_port2",
- anchor: "left",
- },
- data: {
- type: "relation",
- },
- defaultLabel: {
- markup: [
- {
- tagName: "circle",
- selector: "bg",
- },
- {
- tagName: "text",
- selector: "txt",
- },
- ],
- attrs: {
- txt: {
- fill: "#fff",
- textAnchor: "middle",
- textVerticalAnchor: "middle",
- },
- bg: {
- ref: "txt",
- fill: "#333",
- r: 10,
- strokeWidth: 0,
- },
- },
- },
- });
- relationEdge?.appendLabel({
- attrs: {
- txt: {
- text: 1,
- },
- },
- position: {
- distance: 25,
- },
- });
- relationEdge?.appendLabel({
- attrs: {
- txt: {
- text: 1,
- },
- },
- position: {
- distance: -25,
- },
- });
- };
- const cells = graph.getCells();
- const allIds = [
- ...tables.map((table) => table.table.id),
- ...topicAreas.map((topicArea) => topicArea.id),
- ...remarks.map((remark) => remark.id),
- ...relations.map((relation) => relation.id),
- ];
- // 移除空数据节点
- cells.forEach((cell) => {
- if (!allIds.includes(cell.id)) {
- cell.remove();
- }
- });
- tables.forEach((tableItem) => {
- if (!graph.getCellById(tableItem.table.id)) {
- renderTable(tableItem);
- }
- });
- topicAreas.forEach((topicArea) => {
- if (!graph.getCellById(topicArea.id)) {
- renderTopicArea(topicArea);
- }
- });
- remarks.forEach((remark) => {
- if (!graph.getCellById(remark.id)) {
- renderRemark(remark);
- }
- });
- // setTimeout(() => {
-
- // }, 100);
- relations.forEach((relation) => {
- if (!graph.getCellById(relation.id)) {
- renderRelationEdge(
- relation.id,
- {
- tableId: relation.primaryTable,
- columnId: relation.primaryKey,
- },
- {
- tableId: relation.foreignTable,
- columnId: relation.foreignKey,
- }
- );
- }
- });
- };
|