main
HuangHai 1 week ago
parent 4f2cd8ff51
commit 241d4241b4

@ -82,15 +82,17 @@ async def rag(request: fastapi.Request):
- 必须包含右侧信息面板和3D节点效果
2. **设计规范**
- 标题在以醒目字体字号在界面顶部中间位置显示最好有渐变效果
- 主色调深蓝色渐变背景
- 视觉特效3D立体节点非平面+ 发光选中效果
- 文字要求节点内文字绝对居中
- 文字要求节点内文字水平居中,垂直居中
- 布局响应式支持窗口缩放
3. **数据要求**
- 数据结构网络关系图
- 关系分类[至少3种关系类型]
- 节点属性[如类型/描述/重要性]
- 关系线描述需要有关系线的不同颜色描述的图例
4. **交互细节**
- 悬停显示人物简介弹窗

@ -0,0 +1,456 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>刘邦集团人物关系网络</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
<style>
body {
margin: 0;
padding: 0;
font-family: 'Microsoft YaHei', sans-serif;
background: linear-gradient(135deg, #0a1a3a, #1a3a6a);
color: #fff;
overflow: hidden;
}
#header {
text-align: center;
padding: 20px 0;
background: linear-gradient(90deg, rgba(10,26,58,0.8), rgba(26,58,106,0.8));
margin-bottom: 20px;
}
#title {
font-size: 32px;
font-weight: bold;
background: linear-gradient(90deg, #f9d423, #f83600);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
text-shadow: 0 0 10px rgba(248, 54, 0, 0.3);
}
#container {
display: flex;
height: calc(100vh - 100px);
}
#graph {
flex: 3;
height: 100%;
position: relative;
}
#sidebar {
flex: 1;
background: rgba(10, 26, 58, 0.7);
padding: 20px;
border-left: 1px solid #2a4a7a;
overflow-y: auto;
}
.node {
cursor: pointer;
filter: drop-shadow(0 0 5px rgba(255, 255, 255, 0.3));
}
.node text {
fill: #fff;
font-size: 12px;
text-anchor: middle;
dominant-baseline: middle;
pointer-events: none;
}
.link {
stroke-opacity: 0.6;
}
.link.military {
stroke: #ff5252;
}
.link.political {
stroke: #4caf50;
}
.link.family {
stroke: #ff9800;
}
.legend {
position: absolute;
bottom: 20px;
right: 20px;
background: rgba(10, 26, 58, 0.8);
padding: 10px;
border-radius: 5px;
border: 1px solid #2a4a7a;
}
.legend-item {
display: flex;
align-items: center;
margin-bottom: 5px;
}
.legend-color {
width: 20px;
height: 3px;
margin-right: 10px;
}
.tooltip {
position: absolute;
padding: 10px;
background: rgba(10, 26, 58, 0.9);
border: 1px solid #2a4a7a;
border-radius: 5px;
pointer-events: none;
max-width: 300px;
font-size: 14px;
z-index: 100;
}
#layout-controls {
position: absolute;
top: 20px;
left: 20px;
background: rgba(10, 26, 58, 0.8);
padding: 10px;
border-radius: 5px;
border: 1px solid #2a4a7a;
}
button {
background: #2a4a7a;
color: #fff;
border: none;
padding: 5px 10px;
margin: 2px;
border-radius: 3px;
cursor: pointer;
}
button:hover {
background: #3a5a8a;
}
.sidebar-title {
font-size: 18px;
margin-bottom: 10px;
border-bottom: 1px solid #2a4a7a;
padding-bottom: 5px;
}
.sidebar-content {
font-size: 14px;
line-height: 1.5;
}
.relationship-list {
margin-top: 20px;
}
.relationship-item {
margin-bottom: 10px;
padding-bottom: 10px;
border-bottom: 1px dashed #2a4a7a;
}
.relationship-type {
font-weight: bold;
color: #4caf50;
}
</style>
</head>
<body>
<div id="header">
<div id="title">刘邦集团核心人物关系网络</div>
</div>
<div id="container">
<div id="graph"></div>
<div id="sidebar">
<div class="sidebar-title">人物详情</div>
<div class="sidebar-content" id="node-details">
点击节点查看人物详细信息
</div>
<div class="relationship-list">
<div class="sidebar-title">关系列表</div>
<div id="relationship-details"></div>
</div>
</div>
</div>
<div class="legend">
<div class="legend-item">
<div class="legend-color" style="background: #ff5252;"></div>
<span>军事关系</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background: #4caf50;"></div>
<span>政治关系</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background: #ff9800;"></div>
<span>家族关系</span>
</div>
</div>
<div id="layout-controls">
<button id="force-layout">力导向布局</button>
<button id="radial-layout">辐射状布局</button>
<button id="circular-layout">环形布局</button>
<button id="grid-layout">网格布局</button>
</div>
<div class="tooltip" id="tooltip"></div>
<script>
const width = document.getElementById('graph').clientWidth;
const height = document.getElementById('graph').clientHeight;
const svg = d3.select("#graph")
.append("svg")
.attr("width", width)
.attr("height", height);
const simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(d => d.id).distance(100))
.force("charge", d3.forceManyBody().strength(-500))
.force("center", d3.forceCenter(width / 2, height / 2))
.force("collision", d3.forceCollide().radius(d => Math.sqrt(d.size) * 10 + 10));
const data = {
nodes: [
{ id: "1", name: "刘邦", type: "person", size: 174, description: "汉朝开国皇帝,从平民起义到建立汉朝,经历坎坷,重用人才但晚年多疑。" },
{ id: "2", name: "吕后", type: "person", size: 53, description: "刘邦正妻,汉朝第一位皇后,政治手段强硬,刘邦死后掌握大权。" },
{ id: "12", name: "项伯", type: "person", size: 42, description: "项羽的叔父,曾在鸿门宴中保护刘邦,对刘邦有救命之恩。" },
{ id: "8", name: "项庄", type: "person", size: 12, description: "项羽堂弟,鸿门宴中奉命刺杀刘邦的剑客。" },
{ id: "11", name: "曹无伤", type: "person", size: 10, description: "刘邦部下,背叛刘邦向项羽告密,后被刘邦处死。" },
{ id: "10", name: "刘仲", type: "person", size: 6, description: "刘邦兄长,因战败被贬为合阳侯。" },
{ id: "9", name: "列侯宗室", type: "organization", size: 2, description: "汉代贵族集团代表,参与权力斗争。" },
{ id: "5", name: "刘姓宗室", type: "organization", size: 2, description: "汉朝皇族成员,部分品行不良者被儒术派官员从族籍中除名。" },
{ id: "6", name: "吕氏家族", type: "organization", size: 4, description: "吕后时期的核心权力集团,掌控朝政。" },
{ id: "3", name: "刘姓诸侯", type: "organization", size: 6, description: "刘姓诸侯联合铲除吕氏家族势力。" },
{ id: "13", name: "功臣、列侯、将军", type: "category", size: 1, description: "武官集团,在朝会中按次序站在西侧,面向东方。" }
],
links: [
{ source: "1", target: "2", type: "family", description: "夫妻关系,早期共同创业,后期因刘邦宠幸戚夫人而关系恶化" },
{ source: "1", target: "12", type: "political", description: "政治盟友,项伯多次在关键时刻帮助刘邦" },
{ source: "1", target: "8", type: "military", description: "敌对关系,项庄曾在鸿门宴试图刺杀刘邦" },
{ source: "1", target: "11", type: "military", description: "部下背叛,曹无伤向项羽告密导致鸿门宴危机" },
{ source: "1", target: "10", type: "family", description: "兄弟关系,刘仲因战败被刘邦贬职" },
{ source: "1", target: "9", type: "political", description: "统治关系,刘邦依靠列侯宗室巩固政权" },
{ source: "1", target: "5", type: "family", description: "宗族关系,刘邦依靠刘姓宗室维护统治" },
{ source: "2", target: "6", type: "family", description: "家族关系,吕后重用吕氏家族成员" },
{ source: "2", target: "3", type: "political", description: "敌对关系,刘姓诸侯联合反对吕后专权" },
{ source: "1", target: "13", type: "political", description: "君臣关系,刘邦依靠功臣集团建立汉朝" },
{ source: "6", target: "3", type: "military", description: "权力斗争,吕氏家族与刘姓诸侯的军事对抗" }
]
};
const link = svg.append("g")
.selectAll("line")
.data(data.links)
.enter().append("line")
.attr("class", d => `link ${d.type}`)
.attr("stroke-width", 2);
const node = svg.append("g")
.selectAll("g")
.data(data.nodes)
.enter().append("g")
.attr("class", "node")
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
node.each(function(d) {
const g = d3.select(this);
// 3D node effect
g.append("circle")
.attr("r", d => Math.sqrt(d.size) * 3 + 20)
.attr("fill", d => {
if (d.type === "person") return "#3498db";
if (d.type === "organization") return "#e74c3c";
return "#2ecc71";
})
.attr("stroke", "#fff")
.attr("stroke-width", 2)
.attr("opacity", 0.9)
.attr("filter", "url(#glow)");
// Gradient effect for 3D
g.append("circle")
.attr("r", d => Math.sqrt(d.size) * 3 + 18)
.attr("fill", "url(#node-gradient)")
.attr("opacity", 0.6);
g.append("text")
.attr("dy", 4)
.text(d => d.name);
});
svg.append("defs").append("filter")
.attr("id", "glow")
.append("feGaussianBlur")
.attr("stdDeviation", "2")
.attr("result", "coloredBlur");
svg.append("defs").append("filter")
.attr("id", "glow-selected")
.append("feGaussianBlur")
.attr("stdDeviation", "4")
.attr("result", "coloredBlur");
svg.append("defs").append("radialGradient")
.attr("id", "node-gradient")
.attr("cx", "30%")
.attr("cy", "30%")
.attr("r", "70%")
.selectAll("stop")
.data([
{offset: "0%", color: "white", opacity: 0.8},
{offset: "100%", color: "white", opacity: 0}
])
.enter().append("stop")
.attr("offset", d => d.offset)
.attr("stop-color", d => d.color)
.attr("stop-opacity", d => d.opacity);
simulation
.nodes(data.nodes)
.on("tick", ticked);
simulation.force("link")
.links(data.links);
function ticked() {
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);
node
.attr("transform", d => `translate(${d.x},${d.y})`);
}
function dragstarted(event, d) {
if (!event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(event, d) {
d.fx = event.x;
d.fy = event.y;
}
function dragended(event, d) {
if (!event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
// Tooltip and interactions
const tooltip = d3.select("#tooltip");
node.on("mouseover", function(event, d) {
d3.select(this).select("circle").attr("filter", "url(#glow-selected)");
tooltip
.style("left", (event.pageX + 10) + "px")
.style("top", (event.pageY + 10) + "px")
.style("opacity", 1)
.html(`<strong>${d.name}</strong><br>${d.description}`);
})
.on("mouseout", function() {
d3.select(this).select("circle").attr("filter", "url(#glow)");
tooltip.style("opacity", 0);
})
.on("click", function(event, d) {
// Update sidebar
d3.select("#node-details").html(`
<h3>${d.name}</h3>
<p>${d.description}</p>
<p><strong>类型:</strong> ${d.type}</p>
<p><strong>重要性:</strong> ${d.size}</p>
`);
// Update relationships
const relationships = data.links.filter(l => l.source.id === d.id || l.target.id === d.id);
const relationshipHtml = relationships.map(r => {
const otherNodeId = r.source.id === d.id ? r.target.id : r.source.id;
const otherNode = data.nodes.find(n => n.id === otherNodeId);
return `
<div class="relationship-item">
<div><strong>${otherNode.name}</strong></div>
<div class="relationship-type">${r.type === "family" ? "家族" : r.type === "political" ? "政治" : "军事"}关系</div>
<div>${r.description}</div>
</div>
`;
}).join("");
d3.select("#relationship-details").html(relationshipHtml || "暂无关系数据");
});
// Layout controls
d3.select("#force-layout").on("click", function() {
simulation.force("charge", d3.forceManyBody().strength(-500))
.force("center", d3.forceCenter(width / 2, height / 2))
.alphaTarget(0.3)
.restart();
});
d3.select("#radial-layout").on("click", function() {
simulation.force("center", null)
.force("radial", d3.forceRadial(height / 3, width / 2, height / 2))
.alphaTarget(0.3)
.restart();
});
d3.select("#circular-layout").on("click", function() {
const radius = Math.min(width, height) / 3;
const angle = 2 * Math.PI / data.nodes.length;
data.nodes.forEach((d, i) => {
d.fx = width / 2 + radius * Math.cos(i * angle);
d.fy = height / 2 + radius * Math.sin(i * angle);
});
simulation.alphaTarget(0.3).restart();
});
d3.select("#grid-layout").on("click", function() {
const cols = Math.ceil(Math.sqrt(data.nodes.length));
const cellSize = Math.min(width, height) / (cols + 1);
data.nodes.forEach((d, i) => {
const row = Math.floor(i / cols);
const col = i % cols;
d.fx = (col + 1) * cellSize;
d.fy = (row + 1) * cellSize;
});
simulation.alphaTarget(0.3).restart();
});
// Responsive resize
window.addEventListener("resize", function() {
const newWidth = document.getElementById('graph').clientWidth;
const newHeight = document.getElementById('graph').clientHeight;
svg.attr("width", newWidth).attr("height", newHeight);
simulation.force("center", d3.forceCenter(newWidth / 2, newHeight / 2));
simulation.alphaTarget(0.3).restart();
});
</script>
</body>
</html>

@ -0,0 +1,392 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>刘邦集团人物关系图谱</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
<style>
body {
margin: 0;
padding: 0;
font-family: 'Arial', sans-serif;
background: linear-gradient(135deg, #1a2a6c, #3a7bd5);
color: white;
overflow: hidden;
}
#title {
text-align: center;
font-size: 2.5em;
margin: 20px 0;
background: linear-gradient(to right, #ff8a00, #e52e71);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
text-shadow: 0 0 10px rgba(255,255,255,0.3);
}
#container {
display: flex;
height: calc(100vh - 100px);
}
#graph {
flex: 3;
height: 100%;
}
#panel {
flex: 1;
background: rgba(0,0,0,0.7);
padding: 20px;
overflow-y: auto;
border-left: 1px solid rgba(255,255,255,0.1);
}
.node {
stroke: #fff;
stroke-width: 1.5px;
filter: drop-shadow(0 0 5px rgba(255,255,255,0.5));
}
.link {
stroke-opacity: 0.6;
}
.node text {
pointer-events: none;
font-size: 12px;
text-anchor: middle;
dominant-baseline: central;
fill: white;
font-weight: bold;
}
.tooltip {
position: absolute;
padding: 10px;
background: rgba(0,0,0,0.8);
border-radius: 5px;
pointer-events: none;
max-width: 300px;
font-size: 14px;
line-height: 1.4;
}
#controls {
position: absolute;
bottom: 20px;
left: 20px;
z-index: 100;
}
button {
background: rgba(255,255,255,0.2);
color: white;
border: 1px solid rgba(255,255,255,0.4);
padding: 8px 12px;
margin-right: 10px;
border-radius: 4px;
cursor: pointer;
transition: all 0.3s;
}
button:hover {
background: rgba(255,255,255,0.4);
}
.highlight {
stroke: gold !important;
stroke-width: 3px !important;
filter: drop-shadow(0 0 10px gold) !important;
}
#panel h2 {
border-bottom: 1px solid rgba(255,255,255,0.2);
padding-bottom: 10px;
margin-top: 0;
}
#panel-content {
margin-top: 20px;
}
.relationship-item {
margin-bottom: 15px;
padding-bottom: 15px;
border-bottom: 1px dashed rgba(255,255,255,0.1);
}
</style>
</head>
<body>
<h1 id="title">刘邦集团人物关系图谱</h1>
<div id="container">
<div id="graph"></div>
<div id="panel">
<h2>详细信息</h2>
<div id="panel-content">
点击节点查看详细信息
</div>
</div>
</div>
<div id="controls">
<button id="force">力导向</button>
<button id="radial">辐射状</button>
<button id="circular">环形</button>
<button id="grid">网格</button>
</div>
<div class="tooltip"></div>
<script>
const width = document.getElementById('graph').clientWidth;
const height = document.getElementById('graph').clientHeight;
const svg = d3.select("#graph").append("svg")
.attr("width", width)
.attr("height", height);
const simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(d => d.id).distance(100))
.force("charge", d3.forceManyBody().strength(-500))
.force("center", d3.forceCenter(width / 2, height / 2))
.force("collision", d3.forceCollide().radius(60));
const data = {
nodes: [
{ id: "刘邦", type: "帝王", desc: "汉高祖", rank: 10 },
{ id: "吕后", type: "皇后", desc: "刘邦妻子", rank: 9 },
{ id: "萧何", type: "大臣", desc: "丞相", rank: 8 },
{ id: "张良", type: "谋士", desc: "战略家", rank: 8 },
{ id: "韩信", type: "将军", desc: "军事家", rank: 8 },
{ id: "项羽", type: "对手", desc: "西楚霸王", rank: 7 },
{ id: "曹参", type: "将军", desc: "开国功臣", rank: 6 },
{ id: "樊哙", type: "将军", desc: "刘邦妹夫", rank: 6 },
{ id: "项伯", type: "关系", desc: "项羽叔父", rank: 5 },
{ id: "英布", type: "诸侯", desc: "淮南王", rank: 5 },
{ id: "彭越", type: "诸侯", desc: "梁王", rank: 5 },
{ id: "张耳", type: "诸侯", desc: "赵王", rank: 4 },
{ id: "灌婴", type: "将军", desc: "骑兵统帅", rank: 4 },
{ id: "周勃", type: "将军", desc: "太尉", rank: 4 },
{ id: "陈平", type: "谋士", desc: "智囊", rank: 4 },
{ id: "审食其", type: "近臣", desc: "吕后亲信", rank: 3 },
{ id: "刘盈", type: "皇室", desc: "汉惠帝", rank: 3 },
{ id: "刘肥", type: "皇室", desc: "齐王", rank: 3 },
{ id: "刘如意", type: "皇室", desc: "赵王", rank: 3 }
],
links: [
{ source: "刘邦", target: "吕后", type: "婚姻", value: 5 },
{ source: "刘邦", target: "萧何", type: "君臣", value: 8 },
{ source: "刘邦", target: "张良", type: "君臣", value: 8 },
{ source: "刘邦", target: "韩信", type: "君臣", value: 7 },
{ source: "刘邦", target: "项羽", type: "敌对", value: 9 },
{ source: "刘邦", target: "曹参", type: "君臣", value: 6 },
{ source: "刘邦", target: "樊哙", type: "亲属", value: 5 },
{ source: "刘邦", target: "项伯", type: "联盟", value: 4 },
{ source: "刘邦", target: "英布", type: "分封", value: 5 },
{ source: "刘邦", target: "彭越", type: "分封", value: 5 },
{ source: "刘邦", target: "张耳", type: "分封", value: 4 },
{ source: "刘邦", target: "灌婴", type: "君臣", value: 4 },
{ source: "刘邦", target: "周勃", type: "君臣", value: 4 },
{ source: "刘邦", target: "陈平", type: "君臣", value: 4 },
{ source: "刘邦", target: "审食其", type: "任用", value: 3 },
{ source: "刘邦", target: "刘盈", type: "父子", value: 4 },
{ source: "刘邦", target: "刘肥", type: "父子", value: 3 },
{ source: "刘邦", target: "刘如意", type: "父子", value: 3 },
{ source: "吕后", target: "审食其", type: "亲信", value: 6 },
{ source: "吕后", target: "韩信", type: "敌对", value: 7 },
{ source: "吕后", target: "彭越", type: "敌对", value: 6 },
{ source: "吕后", target: "刘盈", type: "母子", value: 5 },
{ source: "吕后", target: "刘如意", type: "敌对", value: 7 },
{ source: "项羽", target: "项伯", type: "亲属", value: 5 },
{ source: "张良", target: "项伯", type: "友情", value: 4 }
]
};
const color = d3.scaleOrdinal()
.domain(["帝王", "皇后", "大臣", "谋士", "将军", "对手", "诸侯", "近臣", "皇室", "关系"])
.range(["#ff7f0e", "#d62728", "#1f77b4", "#2ca02c", "#9467bd", "#e377c2", "#8c564b", "#7f7f7f", "#bcbd22", "#17becf"]);
const linkColor = d3.scaleOrdinal()
.domain(["婚姻", "君臣", "敌对", "亲属", "联盟", "分封", "任用", "父子", "母子", "亲信", "友情"])
.range(["#ff69b4", "#1f77b4", "#d62728", "#8c564b", "#2ca02c", "#9467bd", "#7f7f7f", "#17becf", "#e377c2", "#bcbd22", "#ff7f0e"]);
const link = svg.append("g")
.selectAll("line")
.data(data.links)
.enter().append("line")
.attr("class", "link")
.attr("stroke", d => linkColor(d.type))
.attr("stroke-width", d => d.value / 2);
const node = svg.append("g")
.selectAll("g")
.data(data.nodes)
.enter().append("g")
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
node.append("circle")
.attr("r", d => 20 + d.rank)
.attr("fill", d => color(d.type))
.attr("class", "node")
.on("mouseover", function(event, d) {
d3.select(this).classed("highlight", true);
d3.select(".tooltip")
.style("left", (event.pageX + 10) + "px")
.style("top", (event.pageY - 10) + "px")
.style("opacity", 1)
.html(`<strong>${d.id}</strong><br>类型: ${d.type}<br>${d.desc}`);
link.style("stroke-opacity", l => (l.source === d || l.target === d) ? 1 : 0.2);
})
.on("mouseout", function() {
d3.select(this).classed("highlight", false);
d3.select(".tooltip").style("opacity", 0);
link.style("stroke-opacity", 0.6);
})
.on("click", function(event, d) {
updatePanel(d);
});
node.append("text")
.attr("dy", ".35em")
.text(d => d.id);
simulation
.nodes(data.nodes)
.on("tick", ticked);
simulation.force("link")
.links(data.links);
function ticked() {
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);
node
.attr("transform", d => `translate(${d.x},${d.y})`);
}
function dragstarted(event, d) {
if (!event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(event, d) {
d.fx = event.x;
d.fy = event.y;
}
function dragended(event, d) {
if (!event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
function updatePanel(d) {
const panelContent = d3.select("#panel-content");
panelContent.html(`
<h3>${d.id}</h3>
<p><strong>类型:</strong> ${d.type}</p>
<p><strong>描述:</strong> ${d.desc}</p>
<p><strong>重要性:</strong> ${d.rank}/10</p>
<h4>关系</h4>
<div id="relationships">
${getRelationships(d.id)}
</div>
`);
}
function getRelationships(id) {
const relatedLinks = data.links.filter(l => l.source === id || l.target === id);
return relatedLinks.map(link => {
const otherNode = link.source === id ? link.target : link.source;
const direction = link.source === id ? "→" : "←";
return `
<div class="relationship-item">
<p><strong>${direction} ${otherNode}</strong></p>
<p>关系类型: ${link.type}</p>
<p>关系强度: ${link.value}/10</p>
</div>
`;
}).join("");
}
// Layout controls
d3.select("#force").on("click", () => {
simulation.force("charge", d3.forceManyBody().strength(-500));
simulation.force("center", d3.forceCenter(width / 2, height / 2));
simulation.alpha(1).restart();
});
d3.select("#radial").on("click", () => {
simulation.force("center", null);
simulation.force("radial", d3.forceRadial()
.radius(d => {
if (d.id === "刘邦") return 0;
return 200;
})
.x(width / 2)
.y(height / 2)
.strength(0.1));
simulation.alpha(1).restart();
});
d3.select("#circular").on("click", () => {
simulation.force("center", null);
simulation.force("radial", d3.forceRadial()
.radius(d => {
const rank = d.rank;
if (rank >= 8) return 100;
if (rank >= 6) return 200;
return 300;
})
.x(width / 2)
.y(height / 2)
.strength(0.1));
simulation.alpha(1).restart();
});
d3.select("#grid").on("click", () => {
simulation.force("center", null);
simulation.force("position", d3.forceX(d => {
const index = data.nodes.findIndex(n => n.id === d.id);
return (index % 5) * (width / 5) + (width / 10);
}).strength(1));
simulation.force("positionY", d3.forceY(d => {
const index = data.nodes.findIndex(n => n.id === d.id);
return Math.floor(index / 5) * (height / 4) + (height / 8);
}).strength(1));
simulation.alpha(1).restart();
});
// Initial selection
updatePanel(data.nodes[0]);
// Responsive resize
window.addEventListener('resize', function() {
const newWidth = document.getElementById('graph').clientWidth;
const newHeight = document.getElementById('graph').clientHeight;
svg.attr("width", newWidth).attr("height", newHeight);
if (simulation.force("center")) {
simulation.force("center", d3.forceCenter(newWidth / 2, newHeight / 2));
}
simulation.alpha(1).restart();
});
</script>
</body>
</html>
Loading…
Cancel
Save