import { getGraph, syncGraphFromTables } from './manager.js'; import { showHtmlModal } from '../../ui/page-window.js'; let echartsLoaded = false; async function loadECharts() { if (echartsLoaded) return; if (window.echarts) { echartsLoaded = true; return; } return new Promise((resolve, reject) => { const script = document.createElement('script'); script.src = 'https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js'; script.onload = () => { echartsLoaded = true; resolve(); }; script.onerror = reject; document.head.appendChild(script); }); } export async function showGraphVisualization() { // 确保数据是最新的 syncGraphFromTables(); const modalHtml = `
图谱引擎加载失败,请检查网络连接。
'); } } }); } function renderChart(chart) { const graphData = getGraph(); if (!graphData.nodes || graphData.nodes.length === 0) { chart.clear(); chart.setOption({ title: { text: '暂无数据', subtext: '请确保已启用“角色”相关表格,并且表格中有数据。', left: 'center', top: 'center', textStyle: { color: '#888' } } }); return; } // Transform data for ECharts const nodes = graphData.nodes.map(n => ({ id: n.id, name: n.label, symbolSize: n.type === 'user' ? 40 : 25, itemStyle: { color: n.type === 'user' ? '#9e8aff' : '#4caf50', shadowBlur: 10, shadowColor: 'rgba(0,0,0,0.3)' }, label: { show: true, position: 'right', color: '#eee', formatter: '{b}' }, // Store original metadata data: n })); const links = graphData.edges.map(e => ({ source: e.source, target: e.target, value: e.relation, label: { show: true, formatter: '{c}', color: '#aaa', fontSize: 10 }, lineStyle: { curveness: 0.2, color: 'rgba(255,255,255,0.4)' } })); const option = { backgroundColor: 'transparent', tooltip: { trigger: 'item', backgroundColor: 'rgba(50,50,50,0.9)', borderColor: '#777', textStyle: { color: '#fff' }, formatter: function (params) { if (params.dataType === 'node') { const meta = params.data.data.metadata || {}; let info = meta.info || '暂无信息'; // Truncate long info if (info.length > 100) info = info.substring(0, 100) + '...'; return `