knowledge-graph-visualization-backup.blade.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <div>
  2. <div
  3. x-data="{
  4. initGraph() {
  5. const data = @js($graphData);
  6. const container = document.getElementById('mountNode');
  7. const width = container.scrollWidth;
  8. const height = container.scrollHeight || 600;
  9. if (!window.G6) {
  10. console.error('G6 not loaded');
  11. return;
  12. }
  13. const graph = new G6.Graph({
  14. container: 'mountNode',
  15. width,
  16. height,
  17. modes: {
  18. default: ['drag-canvas', 'zoom-canvas', 'drag-node'],
  19. },
  20. layout: {
  21. type: 'dagre',
  22. rankdir: 'LR',
  23. align: 'UL',
  24. controlPoints: true,
  25. nodesepFunc: () => 1,
  26. ranksepFunc: () => 1,
  27. },
  28. defaultNode: {
  29. type: 'rect',
  30. size: [150, 50],
  31. style: {
  32. radius: 5,
  33. fill: '#C6E5FF',
  34. stroke: '#5B8FF9',
  35. lineWidth: 2,
  36. },
  37. labelCfg: {
  38. style: {
  39. fill: '#000',
  40. fontSize: 12,
  41. },
  42. },
  43. },
  44. defaultEdge: {
  45. type: 'polyline',
  46. style: {
  47. radius: 20,
  48. offset: 45,
  49. endArrow: true,
  50. lineWidth: 2,
  51. stroke: '#C2C8D5',
  52. },
  53. },
  54. });
  55. // Transform data to G6 format
  56. const nodes = data.nodes.map(node => ({
  57. id: node.kp_code,
  58. label: node.cn_name || node.kp_code,
  59. ...node
  60. }));
  61. const edges = data.edges.map(edge => ({
  62. source: edge.source,
  63. target: edge.target,
  64. label: edge.relation_type,
  65. ...edge
  66. }));
  67. graph.data({ nodes, edges });
  68. graph.render();
  69. // Resize handling
  70. if (typeof window !== 'undefined')
  71. window.onresize = () => {
  72. if (!graph || graph.get('destroyed')) return;
  73. if (!container || !container.scrollWidth || !container.scrollHeight) return;
  74. graph.changeSize(container.scrollWidth, container.scrollHeight);
  75. };
  76. }
  77. }"
  78. x-init="
  79. if (!window.G6) {
  80. const script = document.createElement('script');
  81. script.src = 'https://gw.alipayobjects.com/os/lib/antv/g6/4.8.24/dist/g6.min.js';
  82. script.onload = initGraph;
  83. document.head.appendChild(script);
  84. } else {
  85. initGraph();
  86. }
  87. "
  88. class="w-full h-[600px] bg-white dark:bg-gray-800 rounded-lg shadow p-4"
  89. >
  90. <div id="mountNode" class="w-full h-full"></div>
  91. </div>
  92. </div>