You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

540 lines
18 KiB

<!DOCTYPE html>
<html lang="zh">
<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;
background: linear-gradient(135deg, #0a1a3a, #1a3a5a);
color: white;
font-family: 'Arial', sans-serif;
overflow: hidden;
}
#header {
text-align: center;
padding: 20px;
background: linear-gradient(90deg, rgba(10,26,58,0.8), rgba(26,58,90,0.8));
margin-bottom: 20px;
}
#title {
font-size: 32px;
font-weight: bold;
background: linear-gradient(90deg, #ff8a00, #e52e71);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
text-shadow: 0 0 10px rgba(255,138,0,0.3);
}
#container {
display: flex;
height: calc(100vh - 100px);
}
#graph {
flex: 1;
border-right: 1px solid #2a4a6a;
}
#sidebar {
width: 300px;
padding: 20px;
background: rgba(10, 26, 58, 0.7);
overflow-y: auto;
}
.node {
stroke: #fff;
stroke-width: 1.5px;
filter: drop-shadow(0 0 5px rgba(255,255,255,0.3));
}
.link {
stroke-opacity: 0.6;
}
.link.military {
stroke: #ff5252;
}
.link.political {
stroke: #4fc3f7;
}
.link.personal {
stroke: #69f0ae;
}
.legend {
margin-top: 20px;
}
.legend-item {
display: flex;
align-items: center;
margin-bottom: 8px;
}
.legend-color {
width: 20px;
height: 20px;
margin-right: 10px;
border-radius: 3px;
}
#layout-controls {
margin-top: 20px;
}
button {
background: #1a3a5a;
color: white;
border: 1px solid #2a4a6a;
padding: 8px 12px;
margin-right: 5px;
cursor: pointer;
border-radius: 4px;
transition: all 0.3s;
}
button:hover {
background: #2a4a6a;
}
#node-info {
margin-top: 20px;
}
.node-title {
font-size: 20px;
font-weight: bold;
margin-bottom: 10px;
color: #4fc3f7;
}
.node-description {
margin-bottom: 15px;
line-height: 1.5;
}
.relations-list {
margin-top: 15px;
}
.relation-item {
margin-bottom: 10px;
padding: 8px;
background: rgba(26, 58, 90, 0.5);
border-radius: 4px;
}
.relation-type {
font-weight: bold;
color: #69f0ae;
}
.tooltip {
position: absolute;
padding: 10px;
background: rgba(10, 26, 58, 0.9);
border: 1px solid #2a4a6a;
border-radius: 4px;
pointer-events: none;
max-width: 300px;
font-size: 14px;
z-index: 1000;
}
</style>
</head>
<body>
<div id="header">
<div id="title">秦汉时期人物关系图谱</div>
</div>
<div id="container">
<div id="graph"></div>
<div id="sidebar">
<div class="legend">
<h3>关系类型</h3>
<div class="legend-item">
<div class="legend-color" style="background-color: #ff5252;"></div>
<span>军事关系</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #4fc3f7;"></div>
<span>政治关系</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #69f0ae;"></div>
<span>个人关系</span>
</div>
</div>
<div id="layout-controls">
<h3>布局切换</h3>
<button id="force">力导向布局</button>
<button id="radial">辐射布局</button>
<button id="circular">环形布局</button>
<button id="grid">网格布局</button>
</div>
<div id="node-info">
<div class="node-title">点击节点查看详情</div>
<div class="node-description">请点击左侧图谱中的节点查看详细信息</div>
<div class="relations-list">
<h3>关系列表</h3>
<div id="relations-container"></div>
</div>
</div>
</div>
</div>
<div class="tooltip"></div>
<script>
const data = {
"nodes": [
{
"id": "1",
"name": "刘邦",
"type": "emperor",
"desc": "汉高祖,汉朝开国皇帝,从平民起家,击败项羽统一天下",
"rank": 9
},
{
"id": "2",
"name": "秦始皇",
"type": "emperor",
"desc": "中国第一位皇帝,统一六国,建立秦朝,实行中央集权",
"rank": 10
},
{
"id": "3",
"name": "项羽",
"type": "general",
"desc": "西楚霸王,勇猛善战,与刘邦争夺天下,最终失败自刎",
"rank": 8
},
{
"id": "4",
"name": "张良",
"type": "strategist",
"desc": "汉初三杰之一,刘邦重要谋士,精通兵法,善于谋划",
"rank": 7
},
{
"id": "5",
"name": "萧何",
"type": "minister",
"desc": "汉初三杰之一,刘邦重要助手,擅长行政后勤管理",
"rank": 7
},
{
"id": "6",
"name": "韩信",
"type": "general",
"desc": "汉初三杰之一,杰出军事家,为刘邦立下赫赫战功",
"rank": 7
},
{
"id": "7",
"name": "李斯",
"type": "minister",
"desc": "秦朝丞相,辅佐秦始皇统一六国,制定一系列制度",
"rank": 6
},
{
"id": "8",
"name": "吕不韦",
"type": "minister",
"desc": "秦国丞相,商人出身,传说为秦始皇生父",
"rank": 5
},
{
"id": "9",
"name": "王翦",
"type": "general",
"desc": "秦国名将,为秦始皇统一六国立下大功",
"rank": 5
},
{
"id": "10",
"name": "蒙恬",
"type": "general",
"desc": "秦朝名将,负责修筑长城,防御匈奴",
"rank": 5
}
],
"links": [
{
"source": "1",
"target": "2",
"type": "political",
"desc": "刘邦的命运与秦始皇关于天子气的预言相关联"
},
{
"source": "1",
"target": "3",
"type": "military",
"desc": "楚汉相争,刘邦最终击败项羽"
},
{
"source": "1",
"target": "4",
"type": "political",
"desc": "张良是刘邦重要谋士,为汉朝建立立下大功"
},
{
"source": "1",
"target": "5",
"type": "political",
"desc": "萧何是刘邦重要助手,负责行政后勤"
},
{
"source": "1",
"target": "6",
"type": "military",
"desc": "韩信为刘邦立下赫赫战功,后被贬为淮阴侯"
},
{
"source": "2",
"target": "7",
"type": "political",
"desc": "李斯是秦始皇重要助手,制定秦朝制度"
},
{
"source": "2",
"target": "8",
"type": "personal",
"desc": "吕不韦传说为秦始皇生父,后被废黜"
},
{
"source": "2",
"target": "9",
"type": "military",
"desc": "王翦为秦始皇统一六国立下战功"
},
{
"source": "2",
"target": "10",
"type": "military",
"desc": "蒙恬负责修筑长城,防御匈奴"
},
{
"source": "4",
"target": "2",
"type": "personal",
"desc": "张良曾试图刺杀秦始皇为韩国复仇"
},
{
"source": "6",
"target": "3",
"type": "military",
"desc": "韩信在楚汉战争中多次击败项羽军队"
},
{
"source": "7",
"target": "8",
"type": "political",
"desc": "李斯与吕不韦同为秦国重要大臣"
}
]
};
const width = document.getElementById('graph').offsetWidth;
const height = document.getElementById('graph').offsetHeight;
const svg = d3.select("#graph")
.append("svg")
.attr("width", width)
.attr("height", height);
const simulation = d3.forceSimulation(data.nodes)
.force("link", d3.forceLink(data.links).id(d => d.id).distance(100))
.force("charge", d3.forceManyBody().strength(-300))
.force("center", d3.forceCenter(width / 2, height / 2))
.force("collision", d3.forceCollide().radius(d => d.rank * 5 + 20));
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")
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
node.append("circle")
.attr("class", "node")
.attr("r", d => d.rank * 5 + 20)
.attr("fill", d => {
if (d.type === "emperor") return "#ff8a00";
if (d.type === "general") return "#e52e71";
if (d.type === "minister") return "#4fc3f7";
if (d.type === "strategist") return "#69f0ae";
return "#9e9e9e";
})
.on("mouseover", function(event, d) {
d3.select(this)
.attr("stroke", "#fff")
.attr("stroke-width", 2)
.attr("filter", "drop-shadow(0 0 10px rgba(255,255,255,0.5))");
tooltip.style("opacity", 1)
.html(`<strong>${d.name}</strong><br/>${d.desc}`)
.style("left", (event.pageX + 10) + "px")
.style("top", (event.pageY - 10) + "px");
})
.on("mouseout", function() {
d3.select(this)
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.attr("filter", "drop-shadow(0 0 5px rgba(255,255,255,0.3))");
tooltip.style("opacity", 0);
})
.on("click", function(event, d) {
updateSidebar(d);
highlightNode(d.id);
});
node.append("text")
.attr("dy", 4)
.attr("text-anchor", "middle")
.attr("dominant-baseline", "central")
.style("pointer-events", "none")
.style("font-size", d => d.rank * 0.8 + 12 + "px")
.style("font-weight", "bold")
.text(d => d.name);
const tooltip = d3.select(".tooltip")
.style("opacity", 0);
simulation.on("tick", () => {
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 updateSidebar(d) {
d3.select(".node-title").text(d.name);
d3.select(".node-description").text(d.desc);
const relations = data.links.filter(link =>
link.source.id === d.id || link.target.id === d.id
);
const relationsContainer = d3.select("#relations-container")
.html("");
relations.forEach(rel => {
const targetId = rel.source.id === d.id ? rel.target.id : rel.source.id;
const targetNode = data.nodes.find(n => n.id === targetId);
const direction = rel.source.id === d.id ? "→ " : "← ";
relationsContainer.append("div")
.attr("class", "relation-item")
.html(`
<div class="relation-type">${getRelationTypeName(rel.type)}</div>
<div>${direction} ${targetNode.name}</div>
<div>${rel.desc}</div>
`);
});
}
function highlightNode(nodeId) {
node.select("circle")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5);
node.filter(d => d.id === nodeId)
.select("circle")
.attr("stroke", "#fff")
.attr("stroke-width", 3);
link
.style("stroke-opacity", 0.2);
data.links.forEach(link => {
if (link.source.id === nodeId || link.target.id === nodeId) {
svg.select(`line[source="${link.source.id}"][target="${link.target.id}"]`)
.style("stroke-opacity", 0.8);
}
});
}
function getRelationTypeName(type) {
switch(type) {
case "military": return "军事关系";
case "political": return "政治关系";
case "personal": return "个人关系";
default: return "其他关系";
}
}
// 布局切换
d3.select("#force").on("click", () => {
simulation.force("charge", d3.forceManyBody().strength(-300))
.force("center", d3.forceCenter(width / 2, height / 2))
.alpha(0.3).restart();
});
d3.select("#radial").on("click", () => {
simulation.force("center", null)
.force("radial", d3.forceRadial(width / 3, width / 2, height / 2).strength(0.1))
.alpha(0.3).restart();
});
d3.select("#circular").on("click", () => {
simulation.force("center", null)
.force("circular", d3.forceRadial(width / 3, width / 2, height / 2).strength(0.1))
.alpha(0.3).restart();
});
d3.select("#grid").on("click", () => {
simulation.force("center", null)
.force("grid", d3.forceX(width / 2).strength(0.1))
.force("grid", d3.forceY(height / 2).strength(0.1))
.alpha(0.3).restart();
});
</script>
</body>
</html>