Add initial implementation of txt2graph tool for knowledge graph generation
- Created a new Streamlit application for visualizing knowledge graphs. - Implemented text extraction from PDF, Markdown, and TXT files. - Developed graph building logic using Zep Cloud API. - Added support for custom entity types and relationships. - Included interactive HTML visualization for generated graphs. - Updated .gitignore to include new directories and files. - Added example environment configuration file (.env.example) for API key setup. - Created README.md with installation and usage instructions. - Introduced various utility scripts and styles for enhanced functionality.
This commit is contained in:
1 parent
38e3d05b1d
commit
9657061b26
21 files changed
+3115
-1
No files matched your search
@@ -0,0 +1,464 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Knowledge Graph - graph_6e3697873495400d</title>
|
||||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts.min.js"></script>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
|
||||
<style>
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
||||
background: linear-gradient(135deg, #0f0f1a 0%, #1a1a2e 50%, #16213e 100%);
|
||||
min-height: 100vh;
|
||||
color: #e0e0e0;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.header {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
z-index: 100;
|
||||
background: rgba(15, 15, 26, 0.85);
|
||||
backdrop-filter: blur(20px);
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
||||
padding: 16px 24px;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.header-left {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.logo {
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
border-radius: 10px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-weight: 700;
|
||||
font-size: 18px;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.title {
|
||||
font-size: 1.4rem;
|
||||
font-weight: 600;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
-webkit-background-clip: text;
|
||||
-webkit-text-fill-color: transparent;
|
||||
background-clip: text;
|
||||
}
|
||||
|
||||
.stats {
|
||||
display: flex;
|
||||
gap: 24px;
|
||||
}
|
||||
|
||||
.stat-item {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.stat-value {
|
||||
font-size: 1.5rem;
|
||||
font-weight: 700;
|
||||
color: #667eea;
|
||||
}
|
||||
|
||||
.stat-label {
|
||||
font-size: 0.75rem;
|
||||
color: #888;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 1px;
|
||||
}
|
||||
|
||||
#chart {
|
||||
width: 100vw;
|
||||
height: 100vh;
|
||||
padding-top: 80px;
|
||||
}
|
||||
|
||||
.legend {
|
||||
position: fixed;
|
||||
top: 90px;
|
||||
right: 20px;
|
||||
background: rgba(15, 15, 26, 0.9);
|
||||
backdrop-filter: blur(10px);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 12px;
|
||||
padding: 16px;
|
||||
z-index: 99;
|
||||
max-height: calc(100vh - 120px);
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.legend h3 {
|
||||
font-size: 0.85rem;
|
||||
font-weight: 600;
|
||||
margin-bottom: 12px;
|
||||
color: #fff;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
||||
padding-bottom: 8px;
|
||||
}
|
||||
|
||||
.legend-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
padding: 6px 0;
|
||||
cursor: pointer;
|
||||
transition: opacity 0.2s;
|
||||
}
|
||||
|
||||
.legend-item:hover {
|
||||
opacity: 0.7;
|
||||
}
|
||||
|
||||
.legend-color {
|
||||
width: 14px;
|
||||
height: 14px;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.legend-text {
|
||||
font-size: 0.8rem;
|
||||
color: #ccc;
|
||||
}
|
||||
|
||||
.legend-count {
|
||||
margin-left: auto;
|
||||
font-size: 0.75rem;
|
||||
color: #888;
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
padding: 2px 8px;
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
.tooltip {
|
||||
position: fixed;
|
||||
display: none;
|
||||
background: rgba(15, 15, 26, 0.95);
|
||||
backdrop-filter: blur(10px);
|
||||
border: 1px solid rgba(102, 126, 234, 0.3);
|
||||
border-radius: 12px;
|
||||
padding: 16px;
|
||||
max-width: 350px;
|
||||
z-index: 1000;
|
||||
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
|
||||
.tooltip-title {
|
||||
font-size: 1rem;
|
||||
font-weight: 600;
|
||||
color: #fff;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.tooltip-type {
|
||||
display: inline-block;
|
||||
font-size: 0.7rem;
|
||||
padding: 3px 8px;
|
||||
border-radius: 4px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.tooltip-summary {
|
||||
font-size: 0.85rem;
|
||||
color: #aaa;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.controls {
|
||||
position: fixed;
|
||||
bottom: 20px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
z-index: 99;
|
||||
}
|
||||
|
||||
.control-btn {
|
||||
background: rgba(15, 15, 26, 0.9);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
color: #fff;
|
||||
padding: 10px 20px;
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
font-size: 0.85rem;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.control-btn:hover {
|
||||
background: rgba(102, 126, 234, 0.3);
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar {
|
||||
width: 6px;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-track {
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-thumb {
|
||||
background: rgba(255, 255, 255, 0.2);
|
||||
border-radius: 3px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="header">
|
||||
<div class="header-left">
|
||||
<div class="logo">KG</div>
|
||||
<div class="title">Knowledge Graph Visualization</div>
|
||||
</div>
|
||||
<div class="stats">
|
||||
<div class="stat-item">
|
||||
<div class="stat-value">220</div>
|
||||
<div class="stat-label">Nodes</div>
|
||||
</div>
|
||||
<div class="stat-item">
|
||||
<div class="stat-value">401</div>
|
||||
<div class="stat-label">Edges</div>
|
||||
</div>
|
||||
<div class="stat-item">
|
||||
<div class="stat-value">8</div>
|
||||
<div class="stat-label">Types</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="legend">
|
||||
<h3>Entity Types</h3>
|
||||
|
||||
<div class="legend-item" onclick="toggleCategory('Entity')">
|
||||
<div class="legend-color" style="background: #546570;"></div>
|
||||
<span class="legend-text">Entity</span>
|
||||
<span class="legend-count">127</span>
|
||||
</div>
|
||||
<div class="legend-item" onclick="toggleCategory('Media')">
|
||||
<div class="legend-color" style="background: #9a60b4;"></div>
|
||||
<span class="legend-text">Media</span>
|
||||
<span class="legend-count">20</span>
|
||||
</div>
|
||||
<div class="legend-item" onclick="toggleCategory('Organization')">
|
||||
<div class="legend-color" style="background: #5470c6;"></div>
|
||||
<span class="legend-text">Organization</span>
|
||||
<span class="legend-count">32</span>
|
||||
</div>
|
||||
<div class="legend-item" onclick="toggleCategory('Event')">
|
||||
<div class="legend-color" style="background: #fc8452;"></div>
|
||||
<span class="legend-text">Event</span>
|
||||
<span class="legend-count">5</span>
|
||||
</div>
|
||||
<div class="legend-item" onclick="toggleCategory('Location')">
|
||||
<div class="legend-color" style="background: #fac858;"></div>
|
||||
<span class="legend-text">Location</span>
|
||||
<span class="legend-count">7</span>
|
||||
</div>
|
||||
<div class="legend-item" onclick="toggleCategory('Person')">
|
||||
<div class="legend-color" style="background: #ee6666;"></div>
|
||||
<span class="legend-text">Person</span>
|
||||
<span class="legend-count">23</span>
|
||||
</div>
|
||||
<div class="legend-item" onclick="toggleCategory('Product')">
|
||||
<div class="legend-color" style="background: #73c0de;"></div>
|
||||
<span class="legend-text">Product</span>
|
||||
<span class="legend-count">5</span>
|
||||
</div>
|
||||
<div class="legend-item" onclick="toggleCategory('Company')">
|
||||
<div class="legend-color" style="background: #91cc75;"></div>
|
||||
<span class="legend-text">Company</span>
|
||||
<span class="legend-count">1</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="chart"></div>
|
||||
|
||||
<div class="tooltip" id="tooltip"></div>
|
||||
|
||||
<div class="controls">
|
||||
<button class="control-btn" onclick="resetView()">Reset View</button>
|
||||
<button class="control-btn" onclick="togglePhysics()">Toggle Physics</button>
|
||||
<button class="control-btn" onclick="toggleLabels()">Toggle Labels</button>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const nodesData = [{"id": "fff9bf5e-8c84-440d-bb3d-9970d851b142", "name": "论坛", "symbolSize": 30.0, "symbol": "circle", "category": 0, "itemStyle": {"color": "#546570"}, "label": {"show": true}, "summary": "洞察引擎分析师在论坛中总结发言,指出高校为“舆情降温”牺牲公正,毁掉年轻人未来并点燃公众对高教体系的怒火。", "nodeType": "Entity"}, {"id": "fddf65ea-d80a-426b-8a49-049bd54ea706", "name": "平台", "symbolSize": 30.0, "symbol": "circle", "category": 0, "itemStyle": {"color": "#546570"}, "label": {"show": true}, "summary": "“快而浅”的回应策略可能触发平台的“冲突加权”机制,加剧舆论反噬,导致公众诉求升级为“求制度变革”。", "nodeType": "Entity"}, {"id": "fb752c4a-cef0-4f89-9978-46eb521004f9", "name": "Twitter", "symbolSize": 56.0, "symbol": "rect", "category": 1, "itemStyle": {"color": "#9a60b4"}, "label": {"show": true}, "summary": "Twitter是海外舆论战场,#WHUScandal 在Twitter趋势榜最高排名第7位,约1,800万阅读。该平台被校友和留学生用于提出批评或发起澄清话题。", "nodeType": "Media"}, {"id": "f8fdd6a1-af76-4ebb-84df-2ce9f706cca1", "name": "双一流", "symbolSize": 55.00000000000001, "symbol": "roundRect", "category": 2, "itemStyle": {"color": "#5470c6"}, "label": {"show": true}, "summary": "建议公开“双一流”经费的使用明细,并推动财政透明与预算共治,以增强品牌核心竞争力。", "nodeType": "Organization"}, {"id": "f8e388ad-de9a-4393-b3a7-edc439711bcb", "name": "图书馆性骚扰诬告案", "symbolSize": 42.0, "symbol": "circle", "category": 0, "itemStyle": {"color": "#546570"}, "label": {"show": true}, "summary": "图书馆性骚扰诬告案是武汉大学核心负面议题之一,核心事件影响力指数为65.4(较同类事件均值高10.8%),微博央级媒体参与度为58.6%。", "nodeType": "Entity"}, {"id": "f87fca52-7254-450c-8f25-171f59c60f8d", "name": "Media & Insight Engine Cross-Analysis", "symbolSize": 33.0, "symbol": "circle", "category": 0, "itemStyle": {"color": "#546570"}, "label": {"show": true}, "summary": "Media & Insight Engine Cross-Analysis focused on the dynamic evolution of public opinion and the disastrous consequences of the university's handling of the library sexual harassment false accusation case.", "nodeType": "Entity"}, {"id": "f83e5850-4891-4076-9099-38c9306e189b", "name": "法院", "symbolSize": 33.0, "symbol": "circle", "category": 0, "itemStyle": {"color": "#546570"}, "label": {"show": true}, "summary": "武汉经开区人民法院于2025年7月25日一审判决肖某某行为系“抓痒”而非性骚扰,驳回杨景媛全部诉讼请求。该判决是舆论反转的关键证据。", "nodeType": "Entity"}, {"id": "f770af8f-da7d-4d58-8468-d2507986ab52", "name": "学术不端行为处理细则", "symbolSize": 36.0, "symbol": "circle", "category": 0, "itemStyle": {"color": "#546570"}, "label": {"show": true}, "summary": "武汉大学的《学术不端行为处理细则》规定,对举报应在5个工作日内初审,30个工作日内完成调查,但校方应对指控反应迟缓,未在规定时限内发布正式调查声明。", "nodeType": "Entity"}, {"id": "f61eb6aa-55bd-4c74-bb75-a818c8fb6c79", "name": "樱花", "symbolSize": 39.0, "symbol": "circle", "category": 0, "itemStyle": {"color": "#546570"}, "label": {"show": true}, "summary": "在舆情危机前,公众提及武汉大学的核心联想包括“樱花”、“最美大学”等,构建了理想化学府形象;危机后“985滤镜破碎”。", "nodeType": "Entity"}, {"id": "f54f6f58-5f33-4286-a9f8-5d6c8d8ed77f", "name": "华北电力大学教师性骚扰事件", "symbolSize": 46.2, "symbol": "star", "category": 3, "itemStyle": {"color": "#fc8452"}, "label": {"show": true}, "summary": "“华北电力大学教师性骚扰事件”的核心事件影响力指数为52.7,低于图书馆性骚扰诬告案和“和服赏樱”冲突事件(均为65.4)。", "nodeType": "Event"}, {"id": "f4cbcf05-8261-4d1f-88aa-6b9c6dfbe781", "name": "广州", "symbolSize": 40.0, "symbol": "diamond", "category": 4, "itemStyle": {"color": "#fac858"}, "label": {"show": true}, "summary": "广州(属于北上广深一线城市群体)是全国舆论场中“维权”和“制度性质疑”的主力,将武汉大学的个案泛化为对高等教育体制的普遍质疑。", "nodeType": "Location"}, {"id": "f4116c68-ed03-4284-85d7-59202c8ca253", "name": "湖北省教育厅", "symbolSize": 55.00000000000001, "symbol": "roundRect", "category": 2, "itemStyle": {"color": "#5470c6"}, "label": {"show": true}, "summary": "湖北省教育厅是武汉大学品牌声誉深度分析报告中列出的权威来源清单中的一个官方机构。", "nodeType": "Organization"}, {"id": "f3b77991-4e80-4ba0-8a0e-08abbed28b7f", "name": "后勤危机", "symbolSize": 33.0, "symbol": "circle", "category": 0, "itemStyle": {"color":Line truncated
|
||||
const linksData = [{"source": "3f8b3975-cfbf-487d-99f9-2e8a70e73552", "target": "1d611811-802f-458f-8de5-0bd5c0999faf", "name": "RELATES_TO", "fact": "留学生主导了在海外社交平台形成的“第四战场”的讨论,该讨论始于事件发酵期间。"}, {"source": "50c4a22e-0f37-4faa-8bf4-ceee4f96646b", "target": "7617970a-6549-4abd-8ee1-ac70d81cca12", "name": "RELATES_TO", "fact": "清博大数据被归类为专业分析与研究机构。"}, {"source": "c4f2eb13-8760-4616-b1cb-629228856e9e", "target": "a28e64f4-57c9-455a-a2ce-688fd4e2e852", "name": "ACTIVE_DURING", "fact": "外省学生/网民的活跃时段是全天。"}, {"source": "1b72a9fa-8935-4d28-8775-a36bfd67a6c6", "target": "11a713f1-0ccd-47e4-9897-5048a0eea1b4", "name": "CAUSES_NEGATIVELY_AFFECTS", "fact": "财政结构失衡可能导致学生产生强烈的相对剥夺感"}, {"source": "6dfafde1-3d17-4b19-9ed8-f765fe011369", "target": "22aa3005-1b96-4693-b06f-f5685b9b905e", "name": "SUFFERS_FROM", "fact": "武汉大学的声誉经历了“985滤镜破碎”的集体认知,表明其作为985高校的形象受到了损害"}, {"source": "ae0d7e09-1d5a-40b3-b6a2-cae9728017e1", "target": "6dfafde1-3d17-4b19-9ed8-f765fe011369", "name": "REPORTS", "fact": "京报网 (Jingbao Net) is listed as an authority media source."}, {"source": "70b9f7f5-dc9c-4e6e-aec6-ef52de9583b6", "target": "595ed9e2-8a1f-46fa-9213-23f0292ba1e5", "name": "INITIATED_LEGAL_PROCEEDINGS_AGAINST", "fact": "杨景媛于2024年6月正式起诉肖某某,事件进入司法程序。"}, {"source": "6dfafde1-3d17-4b19-9ed8-f765fe011369", "target": "6895e45a-0bcf-46b6-abb3-06767a42133b", "name": "RELATES_TO", "fact": "武汉大学的舆情事件涉及知乎,知乎在舆情传播链中扮演思辨角色。"}, {"source": "7bbca488-5b46-4126-a5f9-bab3ccbfa1a2", "target": "7bbca488-5b46-4126-a5f9-bab3ccbfa1a2", "name": "COLLABORATES", "fact": "华中科技大学 is actively recruiting 武大 faculty using incentives like providing talent apartments."}, {"source": "6dfafde1-3d17-4b19-9ed8-f765fe011369", "target": "eb7c87ca-36ed-4847-b8c6-522a1e7e3ba4", "name": "MONITORS_OCCURRENCE_OF", "fact": "武汉大学将对“图书馆事件”等核心负面议题进行长期关键词监测,警惕其被再次激活"}, {"source": "c4f2eb13-8760-4616-b1cb-629228856e9e", "target": "a28e64f4-57c9-455a-a2ce-688fd4e2e852", "name": "HOLDS_MORAL_HIGH_GROUND", "fact": "外省用户在舆论中占据道德高地,将事件定义为关乎全国考生的教育公平问题。"}, {"source": "af6011d4-f2a0-4a5a-991d-f98a635ea88a", "target": "4638cb73-a11a-4655-aeaa-1ca4af4a0597", "name": "RELATES_TO", "fact": "武汉大学在计算机科学领域拥有顶尖科研成果和人才储备。"}, {"source": "b2a6c7af-3c87-4061-aa20-5ee5a69dd935", "target": "6dfafde1-3d17-4b19-9ed8-f765fe011369", "name": "HAS_POSITION_ON_ISSUE", "fact": "本地居民对武汉大学持护校立场,比例为49.7%。"}, {"source": "70b9f7f5-dc9c-4e6e-aec6-ef52de9583b6", "target": "595ed9e2-8a1f-46fa-9213-23f0292ba1e5", "name": "FILES_LAWSUIT_AGAINST", "fact": "杨景媛正式起诉肖某某,事件进入司法程序。"}, {"source": "c513d7ae-1d99-429a-bdc3-f4fc8e5337ae", "target": "55fe2036-45ee-44bf-850e-39fd0e2f4231", "name": "PARTICIPATED_IN", "fact": "Third-party testing agencies participated in the multi-party rights protection model related to the formaldehyde incident."}, {"source": "44c7f7e2-1534-4c76-98cd-066c921c7520", "target": "98da8592-9544-478d-85b5-bd825d4f922b", "name": "ACTIVE_DURING", "fact": "海外校友活跃于北美/欧洲时区。"}, {"source": "69e7ad4e-3907-44dc-8626-4a7928e262a8", "target": "1a24bda8-fda8-46ff-9fe6-b7898cc368bc", "name": "BELIEVES_STUDENT_ACTION_IS", "fact": "45岁家长和80后校友认为学生的维权行为是被境外势力利用的结果"}, {"source": "8d829233-e3e7-422d-b649-4ff4dc0fba95", "target": "a28e64f4-57c9-455a-a2ce-688fd4e2e852", "name": "FOCUSES_ON", "fact": "教育从业者的核心关切是高校治理范式和同行声援。"}, {"source": "6dfafde1-3d17-4b19-9ed8-f765fe011369", "target": "38e5c63a-2ca5-4594-b37f-74e36a43d084", "name": "SHOULD_CREATE_INITIATIVE", "fact": "武大可以发起“我的武大我监督”校园开放日和发布“校园治理白皮书”,以改革自身"}, {"source": "e18e2166-acd7-4be4-994a-3a111dc2b9e4", "target": "6dfafde1-3d17-4b19-9ed8-f765fe011369", "name": "WORKS_FOR", "fact": "辛某的父母是武汉大学的退休职工,辛某本人是校外人员,此事引发了关于特权的质疑。"}, {"source": "70b9f7f5-dc9c-4e6e-aec6-ef52de9583b6", "target": "a2c4aaf2-1ebd-4f07-a4e8-bde2270ad488", "name": "SYSTEMIC_DISTRUST", "fact": "网民的愤怒升级为对高等教育质量的系统性不信任"}, {"source": "6dfafde1-3d17-4b19-9ed8-f765fe011369", "target": "7bbca488-5b46-4126-a5f9-bab3ccbfa1a2", "name": "RELATES_TO", "fact": "武汉大学在湖北省考生的第一志愿率预测将首次低于同城的Line truncated
|
||||
const categories = [{"name": "Entity", "itemStyle": {"color": "#546570"}}, {"name": "Media", "itemStyle": {"color": "#9a60b4"}}, {"name": "Organization", "itemStyle": {"color": "#5470c6"}}, {"name": "Event", "itemStyle": {"color": "#fc8452"}}, {"name": "Location", "itemStyle": {"color": "#fac858"}}, {"name": "Person", "itemStyle": {"color": "#ee6666"}}, {"name": "Product", "itemStyle": {"color": "#73c0de"}}, {"name": "Company", "itemStyle": {"color": "#91cc75"}}];
|
||||
|
||||
let physicsEnabled = true;
|
||||
let labelsEnabled = true;
|
||||
|
||||
const chart = echarts.init(document.getElementById('chart'), null, {
|
||||
renderer: 'canvas'
|
||||
});
|
||||
|
||||
const option = {
|
||||
backgroundColor: 'transparent',
|
||||
tooltip: {
|
||||
show: false
|
||||
},
|
||||
legend: {
|
||||
show: false
|
||||
},
|
||||
animationDuration: 1500,
|
||||
animationEasingUpdate: 'quinticInOut',
|
||||
series: [{
|
||||
type: 'graph',
|
||||
layout: 'force',
|
||||
data: nodesData,
|
||||
links: linksData,
|
||||
categories: categories,
|
||||
roam: true,
|
||||
draggable: true,
|
||||
label: {
|
||||
show: true,
|
||||
position: 'bottom',
|
||||
fontSize: 11,
|
||||
color: '#fff',
|
||||
textBorderColor: 'transparent',
|
||||
formatter: function(params) {
|
||||
const name = params.data.name;
|
||||
return name.length > 12 ? name.substring(0, 12) + '...' : name;
|
||||
}
|
||||
},
|
||||
labelLayout: {
|
||||
hideOverlap: true
|
||||
},
|
||||
emphasis: {
|
||||
focus: 'adjacency',
|
||||
lineStyle: {
|
||||
width: 4
|
||||
},
|
||||
label: {
|
||||
show: true,
|
||||
fontSize: 14,
|
||||
fontWeight: 'bold'
|
||||
}
|
||||
},
|
||||
lineStyle: {
|
||||
color: 'source',
|
||||
curveness: 0.2,
|
||||
opacity: 0.6,
|
||||
width: 1.5
|
||||
},
|
||||
edgeLabel: {
|
||||
show: false,
|
||||
fontSize: 9,
|
||||
formatter: function(params) {
|
||||
return params.data.name;
|
||||
}
|
||||
},
|
||||
force: {
|
||||
repulsion: 800,
|
||||
gravity: 0.1,
|
||||
edgeLength: [100, 250],
|
||||
friction: 0.6
|
||||
},
|
||||
scaleLimit: {
|
||||
min: 0.2,
|
||||
max: 5
|
||||
}
|
||||
}]
|
||||
};
|
||||
|
||||
chart.setOption(option);
|
||||
|
||||
// 自定义tooltip
|
||||
const tooltip = document.getElementById('tooltip');
|
||||
|
||||
chart.on('mouseover', function(params) {
|
||||
if (params.dataType === 'node') {
|
||||
const data = params.data;
|
||||
const config = {"Person": {"color": "#ee6666", "symbol": "circle", "symbolSize": 45}, "Company": {"color": "#91cc75", "symbol": "roundRect", "symbolSize": 50}, "Organization": {"color": "#5470c6", "symbol": "roundRect", "symbolSize": 50}, "Location": {"color": "#fac858", "symbol": "diamond", "symbolSize": 40}, "Product": {"color": "#73c0de", "symbol": "triangle", "symbolSize": 38}, "Event": {"color": "#fc8452", "symbol": "star", "symbolSize": 42}, "Media": {"color": "#9a60b4", "symbol": "rect", "symbolSize": 40}, "Preference": {"color": "#ea7ccc", "symbol": "circle", "symbolSize": 35}, "Topic": {"color": "#3ba272", "symbol": "circle", "symbolSize": 35}, "Object": {"color": "#6e7074", "symbol": "circle", "symbolSize": 32}, "Entity": {"color": "#546570", "symbol": "circle", "symbolSize": 30}}[data.nodeType] || {"color": "#61a0a8", "symbol": "circle", "symbolSize": 30};
|
||||
|
||||
tooltip.innerHTML = `
|
||||
<div class="tooltip-title">${data.name}</div>
|
||||
<div class="tooltip-type" style="background: ${config.color}33; color: ${config.color};">${data.nodeType}</div>
|
||||
<div class="tooltip-summary">${data.summary}</div>
|
||||
`;
|
||||
tooltip.style.display = 'block';
|
||||
} else if (params.dataType === 'edge') {
|
||||
const data = params.data;
|
||||
tooltip.innerHTML = `
|
||||
<div class="tooltip-title">${data.name || 'Relationship'}</div>
|
||||
<div class="tooltip-summary">${data.fact}</div>
|
||||
`;
|
||||
tooltip.style.display = 'block';
|
||||
}
|
||||
});
|
||||
|
||||
chart.on('mouseout', function() {
|
||||
tooltip.style.display = 'none';
|
||||
});
|
||||
|
||||
document.addEventListener('mousemove', function(e) {
|
||||
if (tooltip.style.display === 'block') {
|
||||
const x = e.clientX + 15;
|
||||
const y = e.clientY + 15;
|
||||
const rect = tooltip.getBoundingClientRect();
|
||||
|
||||
tooltip.style.left = (x + rect.width > window.innerWidth) ? (e.clientX - rect.width - 15) + 'px' : x + 'px';
|
||||
tooltip.style.top = (y + rect.height > window.innerHeight) ? (e.clientY - rect.height - 15) + 'px' : y + 'px';
|
||||
}
|
||||
});
|
||||
|
||||
// 控制函数
|
||||
function resetView() {
|
||||
chart.dispatchAction({
|
||||
type: 'restore'
|
||||
});
|
||||
}
|
||||
|
||||
function togglePhysics() {
|
||||
physicsEnabled = !physicsEnabled;
|
||||
chart.setOption({
|
||||
series: [{
|
||||
force: {
|
||||
layoutAnimation: physicsEnabled
|
||||
}
|
||||
}]
|
||||
});
|
||||
}
|
||||
|
||||
function toggleLabels() {
|
||||
labelsEnabled = !labelsEnabled;
|
||||
chart.setOption({
|
||||
series: [{
|
||||
label: {
|
||||
show: labelsEnabled
|
||||
}
|
||||
}]
|
||||
});
|
||||
}
|
||||
|
||||
function toggleCategory(categoryName) {
|
||||
// 可以实现类型过滤功能
|
||||
console.log('Toggle category:', categoryName);
|
||||
}
|
||||
|
||||
// 响应式
|
||||
window.addEventListener('resize', function() {
|
||||
chart.resize();
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user