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.

499 lines
19 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;
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, #f5d742, #f5a742);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
text-shadow: 0 0 10px rgba(245,215,66,0.3);
}
#container {
display: flex;
height: calc(100vh - 100px);
}
#graph {
flex: 3;
position: relative;
border-right: 1px solid #2a4a8a;
}
#panel {
flex: 1;
padding: 20px;
overflow-y: auto;
background: rgba(10,26,58,0.7);
}
.node {
stroke-width: 2px;
filter: url(#drop-shadow);
transition: all 0.3s ease;
}
.node:hover {
stroke: #f5d742;
stroke-width: 3px;
filter: url(#glow-effect);
cursor: pointer;
}
.link {
stroke-opacity: 0.6;
}
.legend {
position: absolute;
bottom: 20px;
left: 20px;
background: rgba(10,26,58,0.8);
padding: 10px;
border-radius: 5px;
border: 1px solid #2a4a8a;
}
.legend-item {
display: flex;
align-items: center;
margin: 5px 0;
}
.legend-color {
width: 20px;
height: 20px;
margin-right: 10px;
border-radius: 3px;
}
.tooltip {
position: absolute;
padding: 10px;
background: rgba(10,26,58,0.9);
border: 1px solid #2a4a8a;
border-radius: 5px;
pointer-events: none;
max-width: 300px;
font-size: 14px;
box-shadow: 0 0 10px rgba(0,0,0,0.5);
opacity: 0;
transition: opacity 0.3s;
}
.panel-title {
font-size: 20px;
font-weight: bold;
margin-bottom: 15px;
color: #f5d742;
border-bottom: 1px solid #2a4a8a;
padding-bottom: 5px;
}
.panel-content {
margin-bottom: 20px;
}
.relation-list {
list-style-type: none;
padding: 0;
}
.relation-item {
padding: 8px;
margin-bottom: 5px;
background: rgba(26,58,106,0.5);
border-radius: 3px;
border-left: 3px solid #f5a742;
}
.controls {
position: absolute;
top: 20px;
right: 20px;
background: rgba(10,26,58,0.8);
padding: 10px;
border-radius: 5px;
border: 1px solid #2a4a8a;
z-index: 10;
}
.control-btn {
display: block;
margin: 5px 0;
padding: 5px 10px;
background: #2a4a8a;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
transition: background 0.3s;
}
.control-btn:hover {
background: #3a5a9a;
}
#filters {
position: absolute;
top: 20px;
left: 20px;
background: rgba(10,26,58,0.8);
padding: 10px;
border-radius: 5px;
border: 1px solid #2a4a8a;
z-index: 10;
}
.filter-item {
margin: 5px 0;
}
</style>
</head>
<body>
<div id="header">
<h1 id="title">汉匈战争人物关系图谱</h1>
</div>
<div id="container">
<div id="graph"></div>
<div id="panel">
<div class="panel-title">人物详情</div>
<div class="panel-content">
<p>点击节点查看详细信息</p>
</div>
<div class="panel-title">关系列表</div>
<div>
<ul class="relation-list">
<li class="relation-item">暂无选择</li>
</ul>
</div>
</div>
</div>
<div class="legend">
<div class="legend-item">
<div class="legend-color" style="background-color: #f5a742;"></div>
<span>军事关系</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #42f5a7;"></div>
<span>家族关系</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #a742f5;"></div>
<span>政治关系</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #f542a7;"></div>
<span>冲突关系</span>
</div>
</div>
<div class="controls">
<button class="control-btn" id="force-layout">力导向布局</button>
<button class="control-btn" id="radial-layout">辐射状布局</button>
<button class="control-btn" id="circular-layout">环形布局</button>
<button class="control-btn" id="grid-layout">网格布局</button>
</div>
<div id="filters">
<div class="filter-item">
<label><input type="checkbox" class="relation-filter" value="military" checked> 军事关系</label>
</div>
<div class="filter-item">
<label><input type="checkbox" class="relation-filter" value="family" checked> 家族关系</label>
</div>
<div class="filter-item">
<label><input type="checkbox" class="relation-filter" value="political" checked> 政治关系</label>
</div>
<div class="filter-item">
<label><input type="checkbox" class="relation-filter" value="conflict" checked> 冲突关系</label>
</div>
</div>
<div class="tooltip"></div>
<svg style="display:none;">
<defs>
<filter id="drop-shadow" x="-20%" y="-20%" width="140%" height="140%">
<feGaussianBlur in="SourceAlpha" stdDeviation="3" result="blur"/>
<feOffset in="blur" dx="0" dy="0" result="offsetBlur"/>
<feComposite in="SourceGraphic" in2="offsetBlur" operator="over"/>
</filter>
<filter id="glow-effect" x="-20%" y="-20%" width="140%" height="140%">
<feGaussianBlur stdDeviation="5" result="glow"/>
<feComposite in="SourceGraphic" in2="glow" operator="over" result="composite"/>
<feMerge>
<feMergeNode in="glow"/>
<feMergeNode in="composite"/>
</feMerge>
</filter>
</defs>
</svg>
<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(-300))
.force("center", d3.forceCenter(width / 2, height / 2))
.force("collision", d3.forceCollide().radius(d => d.size * 1.2));
const tooltip = d3.select(".tooltip");
// 数据定义
const nodes = [
{ id: "1", name: "卫青", type: "person", description: "汉朝大将军,多次率军抗击匈奴", size: 25, group: 1 },
{ id: "2", name: "李广", type: "person", description: "汉朝名将,飞将军,七十余战未能封侯", size: 20, group: 1 },
{ id: "3", name: "匈奴", type: "geo", description: "汉朝北方主要敌人", size: 30, group: 2 },
{ id: "4", name: "武帝", type: "person", description: "汉武帝,汉朝第七位皇帝", size: 28, group: 3 },
{ id: "5", name: "张骞", type: "person", description: "汉朝外交家,丝绸之路开拓者", size: 18, group: 1 },
{ id: "6", name: "霍去病", type: "person", description: "卫青外甥,骠骑将军,抗击匈奴名将", size: 22, group: 1 },
{ id: "7", name: "卫子夫", type: "person", description: "卫青姐姐,汉武帝皇后", size: 15, group: 3 },
{ id: "8", name: "李敢", type: "person", description: "李广之子,因怨恨卫青而刺杀未遂", size: 12, group: 1 },
{ id: "9", name: "漠北之战", type: "event", description: "公元前119年决定性战役", size: 20, group: 4 },
{ id: "10", name: "河西之战", type: "event", description: "霍去病率军攻打匈奴的战役", size: 18, group: 4 }
];
const links = [
{ source: "1", target: "3", value: 5, type: "military", description: "卫青多次率军抗击匈奴" },
{ source: "2", target: "3", value: 5, type: "military", description: "李广七十余战抗击匈奴" },
{ source: "1", target: "2", value: 3, type: "conflict", description: "卫青与李广存在军事矛盾" },
{ source: "1", target: "4", value: 4, type: "political", description: "卫青受汉武帝重用" },
{ source: "1", target: "6", value: 4, type: "family", description: "卫青是霍去病的舅舅" },
{ source: "1", target: "7", value: 3, type: "family", description: "卫青与卫子夫是姐弟" },
{ source: "2", target: "8", value: 2, type: "family", description: "李广与李敢是父子" },
{ source: "1", target: "8", value: 2, type: "conflict", description: "李敢因父仇怨恨卫青" },
{ source: "1", target: "9", value: 4, type: "military", description: "卫青指挥漠北之战" },
{ source: "6", target: "9", value: 4, type: "military", description: "霍去病参与漠北之战" },
{ source: "6", target: "10", value: 5, type: "military", description: "霍去病主导河西之战" },
{ source: "4", target: "3", value: 5, type: "military", description: "汉武帝发动对匈奴战争" },
{ source: "5", target: "1", value: 2, type: "military", description: "张骞跟随卫青作战" },
{ source: "5", target: "2", value: 2, type: "military", description: "张骞与李广共同征战" },
{ source: "7", target: "4", value: 3, type: "political", description: "卫子夫是汉武帝皇后" }
];
// 颜色映射
const color = d3.scaleOrdinal()
.domain(["military", "family", "political", "conflict"])
.range(["#f5a742", "#42f5a7", "#a742f5", "#f542a7"]);
// 创建力导向图
let link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(links)
.enter().append("line")
.attr("class", "link")
.attr("stroke", d => color(d.type))
.attr("stroke-width", d => Math.sqrt(d.value));
let node = svg.append("g")
.attr("class", "nodes")
.selectAll("g")
.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.size)
.attr("fill", d => {
if (d.type === "person") return "#4a90e2";
if (d.type === "geo") return "#e24a4a";
if (d.type === "event") return "#4ae2a3";
return "#a34ae2";
})
.attr("stroke", "#fff")
.on("mouseover", function(event, d) {
tooltip.style("opacity", 1)
.html(`<strong>${d.name}</strong><br>${d.description}`)
.style("left", (event.pageX + 10) + "px")
.style("top", (event.pageY - 28) + "px");
d3.select(this).attr("stroke-width", 3).attr("stroke", "#f5d742");
})
.on("mouseout", function() {
tooltip.style("opacity", 0);
d3.select(this).attr("stroke-width", 2).attr("stroke", "#fff");
})
.on("click", function(event, d) {
updatePanel(d);
});
node.append("text")
.attr("dy", 4)
.attr("text-anchor", "middle")
.attr("dominant-baseline", "central")
.text(d => d.name)
.attr("fill", "#fff")
.attr("font-size", d => d.size / 2 + 8);
simulation
.nodes(nodes)
.on("tick", ticked);
simulation.force("link")
.links(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(node) {
d3.select(".panel-title").text(node.name);
d3.select(".panel-content").html(`
<p><strong>类型:</strong> ${node.type}</p>
<p><strong>描述:</strong> ${node.description}</p>
`);
const relatedLinks = links.filter(l => l.source.id === node.id || l.target.id === node.id);
const relationList = d3.select(".relation-list").selectAll("li")
.data(relatedLinks);
relationList.exit().remove();
relationList.enter()
.append("li")
.attr("class", "relation-item")
.merge(relationList)
.html(d => {
const otherNode = d.source.id === node.id ?
nodes.find(n => n.id === d.target.id) :
nodes.find(n => n.id === d.source.id);
return `<strong>${otherNode.name}</strong>: ${d.description}`;
});
}
// 布局切换
d3.select("#force-layout").on("click", function() {
simulation.force("charge", d3.forceManyBody().strength(-300));
simulation.alpha(1).restart();
});
d3.select("#radial-layout").on("click", function() {
const centerX = width / 2;
const centerY = height / 2;
nodes.forEach(d => {
const angle = Math.random() * Math.PI * 2;
const radius = Math.random() * Math.min(width, height) / 3;
d.fx = centerX + Math.cos(angle) * radius;
d.fy = centerY + Math.sin(angle) * radius;
});
simulation.alpha(1).restart();
});
d3.select("#circular-layout").on("click", function() {
const centerX = width / 2;
const centerY = height / 2;
const radius = Math.min(width, height) / 3;
const angleStep = (2 * Math.PI) / nodes.length;
nodes.forEach((d, i) => {
const angle = i * angleStep;
d.fx = centerX + Math.cos(angle) * radius;
d.fy = centerY + Math.sin(angle) * radius;
});
simulation.alpha(1).restart();
});
d3.select("#grid-layout").on("click", function() {
const cols = Math.ceil(Math.sqrt(nodes.length));
const cellWidth = width / cols;
const cellHeight = height / cols;
nodes.forEach((d, i) => {
const row = Math.floor(i / cols);
const col = i % cols;
d.fx = col * cellWidth + cellWidth / 2;
d.fy = row * cellHeight + cellHeight / 2;
});
simulation.alpha(1).restart();
});
// 响应式布局
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.alpha(1).restart();
});
// 关系过滤
d3.selectAll(".relation-filter").on("change", function() {
const selectedTypes = [];
d3.selectAll(".relation-filter:checked").each(function() {
selectedTypes.push(this.value);
});
link.style("display", d => selectedTypes.includes(d.type) ? "inline" : "none");
});
</script>
</body>
</html>