renderer.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import { ProjectInfo, RemarkInfo, TableItemType, TopicAreaInfo } from "@/type";
  2. import { Graph } from "@antv/x6";
  3. export const render = (graph: Graph, project: ProjectInfo) => {
  4. const { tables, relations, topicAreas, remarks } = project;
  5. // 渲染表格
  6. const renderTable = (tableItem: TableItemType) => {
  7. graph.addNode({
  8. shape: "table-node",
  9. x: 300,
  10. y: 100,
  11. width: 220,
  12. height: 69,
  13. id: tableItem.table.id,
  14. data: tableItem,
  15. zIndex: 3,
  16. ports: {
  17. groups: {
  18. // 字段名前连接桩
  19. columnPort: {
  20. markup: [
  21. {
  22. tagName: "rect",
  23. selector: "rect",
  24. },
  25. {
  26. tagName: "circle",
  27. selector: "circle",
  28. },
  29. ],
  30. position: {
  31. name: "absolute",
  32. args: {
  33. x: 12,
  34. y: 42,
  35. },
  36. },
  37. },
  38. },
  39. },
  40. });
  41. };
  42. // 渲染主题区域
  43. const renderTopicArea = (topicArea: TopicAreaInfo) => {
  44. graph.addNode({
  45. shape: "topic-node",
  46. x: 300,
  47. y: 100,
  48. width: 200,
  49. height: 200,
  50. id: topicArea.id,
  51. data: topicArea,
  52. zIndex: 0,
  53. });
  54. };
  55. // 渲染备注
  56. const renderRemark = (remark: RemarkInfo) => {
  57. const notice = graph?.addNode({
  58. shape: "notice-node",
  59. x: 300,
  60. y: 100,
  61. width: 200,
  62. height: 200,
  63. id: remark.id,
  64. data: remark,
  65. zIndex: 1,
  66. });
  67. };
  68. // 渲染关系
  69. const renderRelationEdge = (
  70. id: string,
  71. source: {
  72. tableId: string;
  73. columnId: string;
  74. },
  75. target: {
  76. tableId: string;
  77. columnId: string;
  78. }
  79. ) => {
  80. // 添加关系连线
  81. const relationEdge = graph?.addEdge({
  82. id,
  83. router: {
  84. name: "er",
  85. args: {
  86. offset: "center",
  87. direction: "H",
  88. },
  89. },
  90. connector: { name: "rounded" },
  91. attrs: {
  92. line: {
  93. stroke: "#333",
  94. strokeWidth: 1,
  95. targetMarker: null,
  96. },
  97. },
  98. source: {
  99. cell: source.tableId,
  100. port: source.columnId + "_port2",
  101. anchor: "left",
  102. },
  103. target: {
  104. cell: target.tableId,
  105. port: target.columnId + "_port2",
  106. anchor: "left",
  107. },
  108. data: {
  109. type: "relation",
  110. },
  111. defaultLabel: {
  112. markup: [
  113. {
  114. tagName: "circle",
  115. selector: "bg",
  116. },
  117. {
  118. tagName: "text",
  119. selector: "txt",
  120. },
  121. ],
  122. attrs: {
  123. txt: {
  124. fill: "#fff",
  125. textAnchor: "middle",
  126. textVerticalAnchor: "middle",
  127. },
  128. bg: {
  129. ref: "txt",
  130. fill: "#333",
  131. r: 10,
  132. strokeWidth: 0,
  133. },
  134. },
  135. },
  136. });
  137. relationEdge?.appendLabel({
  138. attrs: {
  139. txt: {
  140. text: 1,
  141. },
  142. },
  143. position: {
  144. distance: 25,
  145. },
  146. });
  147. relationEdge?.appendLabel({
  148. attrs: {
  149. txt: {
  150. text: 1,
  151. },
  152. },
  153. position: {
  154. distance: -25,
  155. },
  156. });
  157. };
  158. const cells = graph.getCells();
  159. const allIds = [
  160. ...tables.map((table) => table.table.id),
  161. ...topicAreas.map((topicArea) => topicArea.id),
  162. ...remarks.map((remark) => remark.id),
  163. ...relations.map((relation) => relation.id),
  164. ];
  165. // 移除空数据节点
  166. cells.forEach((cell) => {
  167. if (!allIds.includes(cell.id)) {
  168. cell.remove();
  169. }
  170. });
  171. tables.forEach((tableItem) => {
  172. if (!graph.getCellById(tableItem.table.id)) {
  173. renderTable(tableItem);
  174. }
  175. });
  176. topicAreas.forEach((topicArea) => {
  177. if (!graph.getCellById(topicArea.id)) {
  178. renderTopicArea(topicArea);
  179. }
  180. });
  181. remarks.forEach((remark) => {
  182. if (!graph.getCellById(remark.id)) {
  183. renderRemark(remark);
  184. }
  185. });
  186. // setTimeout(() => {
  187. // }, 100);
  188. relations.forEach((relation) => {
  189. if (!graph.getCellById(relation.id)) {
  190. renderRelationEdge(
  191. relation.id,
  192. {
  193. tableId: relation.primaryTable,
  194. columnId: relation.primaryKey,
  195. },
  196. {
  197. tableId: relation.foreignTable,
  198. columnId: relation.foreignKey,
  199. }
  200. );
  201. }
  202. });
  203. };