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.
386 lines
16 KiB
386 lines
16 KiB
|
|
<!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;
|
|
overflow: hidden;
|
|
background: linear-gradient(135deg, #1a2a6c, #3a7bd5);
|
|
font-family: 'Arial', sans-serif;
|
|
color: white;
|
|
}
|
|
|
|
#header {
|
|
text-align: center;
|
|
padding: 20px;
|
|
background: linear-gradient(90deg, rgba(0,0,0,0), rgba(58,123,213,0.7), rgba(0,0,0,0));
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
#title {
|
|
font-size: 36px;
|
|
font-weight: bold;
|
|
text-shadow: 0 0 10px rgba(255,255,255,0.5);
|
|
background: linear-gradient(to right, #f7ff00, #ff6b6b, #00d2ff);
|
|
-webkit-background-clip: text;
|
|
-webkit-text-fill-color: transparent;
|
|
}
|
|
|
|
#container {
|
|
display: flex;
|
|
height: calc(100vh - 100px);
|
|
}
|
|
|
|
#graph {
|
|
flex: 3;
|
|
position: relative;
|
|
}
|
|
|
|
#infoPanel {
|
|
flex: 1;
|
|
background: rgba(0,0,0,0.7);
|
|
padding: 20px;
|
|
overflow-y: auto;
|
|
border-left: 1px solid rgba(255,255,255,0.2);
|
|
}
|
|
|
|
#legend {
|
|
position: absolute;
|
|
bottom: 20px;
|
|
right: 20px;
|
|
background: rgba(0,0,0,0.7);
|
|
padding: 10px;
|
|
border-radius: 5px;
|
|
}
|
|
|
|
.legend-item {
|
|
display: flex;
|
|
align-items: center;
|
|
margin-bottom: 5px;
|
|
}
|
|
|
|
.legend-color {
|
|
width: 20px;
|
|
height: 20px;
|
|
margin-right: 10px;
|
|
border-radius: 50%;
|
|
}
|
|
|
|
.node {
|
|
stroke: #fff;
|
|
stroke-width: 1.5px;
|
|
filter: drop-shadow(0 0 5px rgba(255,255,255,0.5));
|
|
}
|
|
|
|
.node:hover {
|
|
stroke: #ff0;
|
|
stroke-width: 3px;
|
|
filter: drop-shadow(0 0 10px rgba(255,255,0,0.8));
|
|
}
|
|
|
|
.link {
|
|
stroke-opacity: 0.6;
|
|
}
|
|
|
|
.tooltip {
|
|
position: absolute;
|
|
padding: 10px;
|
|
background: rgba(0,0,0,0.8);
|
|
color: white;
|
|
border-radius: 5px;
|
|
pointer-events: none;
|
|
font-size: 14px;
|
|
max-width: 200px;
|
|
}
|
|
|
|
.control {
|
|
position: absolute;
|
|
top: 80px;
|
|
left: 20px;
|
|
background: rgba(0,0,0,0.7);
|
|
padding: 10px;
|
|
border-radius: 5px;
|
|
}
|
|
|
|
button {
|
|
background: rgba(58,123,213,0.7);
|
|
color: white;
|
|
border: none;
|
|
padding: 5px 10px;
|
|
margin: 2px;
|
|
border-radius: 3px;
|
|
cursor: pointer;
|
|
}
|
|
|
|
button:hover {
|
|
background: rgba(58,123,213,1);
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div id="header">
|
|
<h1 id="title">秦汉时期主要人物关系图</h1>
|
|
</div>
|
|
|
|
<div id="container">
|
|
<div id="graph"></div>
|
|
<div id="infoPanel">
|
|
<h2>人物信息</h2>
|
|
<p>点击节点查看详细信息</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="legend">
|
|
<h3>关系类型</h3>
|
|
<div class="legend-item">
|
|
<div class="legend-color" style="background-color: #ff6b6b;"></div>
|
|
<span>政治关系</span>
|
|
</div>
|
|
<div class="legend-item">
|
|
<div class="legend-color" style="background-color: #00d2ff;"></div>
|
|
<span>军事关系</span>
|
|
</div>
|
|
<div class="legend-item">
|
|
<div class="legend-color" style="background-color: #f7ff00;"></div>
|
|
<span>家族关系</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="control">
|
|
<h3>布局切换</h3>
|
|
<button id="force">力导向</button>
|
|
<button id="radial">辐射状</button>
|
|
<button id="circular">环形</button>
|
|
<button id="grid">网格</button>
|
|
</div>
|
|
|
|
<div class="tooltip" style="opacity: 0;"></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)
|
|
.call(d3.zoom().on("zoom", function(event) {
|
|
svg.attr("transform", event.transform);
|
|
}))
|
|
.append("g");
|
|
|
|
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));
|
|
|
|
const colorScale = d3.scaleOrdinal()
|
|
.domain(["political", "military", "family"])
|
|
.range(["#ff6b6b", "#00d2ff", "#f7ff00"]);
|
|
|
|
const data = {
|
|
"nodes": [
|
|
{"id": "秦始皇", "name": "秦始皇", "type": "emperor", "size": 30, "desc": "中国历史上第一个完成全国统一的皇帝", "img": "", "importance": 10},
|
|
{"id": "李斯", "name": "李斯", "type": "politician", "size": 25, "desc": "秦朝丞相,协助秦始皇统一中国", "img": "", "importance": 8},
|
|
{"id": "蒙恬", "name": "蒙恬", "type": "general", "size": 25, "desc": "秦朝名将,负责修筑长城", "img": "", "importance": 7},
|
|
{"id": "赵高", "name": "赵高", "type": "politician", "size": 20, "desc": "秦二世时期权臣,指鹿为马", "img": "", "importance": 6},
|
|
{"id": "胡亥", "name": "胡亥", "type": "emperor", "size": 20, "desc": "秦二世,秦始皇幼子", "img": "", "importance": 5},
|
|
{"id": "扶苏", "name": "扶苏", "type": "prince", "size": 20, "desc": "秦始皇长子,被赵高陷害自杀", "img": "", "importance": 5},
|
|
{"id": "刘邦", "name": "刘邦", "type": "emperor", "size": 30, "desc": "汉朝开国皇帝,汉高祖", "img": "", "importance": 10},
|
|
{"id": "项羽", "name": "项羽", "type": "general", "size": 28, "desc": "西楚霸王,刘邦的主要对手", "img": "", "importance": 9},
|
|
{"id": "萧何", "name": "萧何", "type": "politician", "size": 25, "desc": "汉朝开国功臣,第一任丞相", "img": "", "importance": 8},
|
|
{"id": "韩信", "name": "韩信", "type": "general", "size": 25, "desc": "汉朝开国名将,军事天才", "img": "", "importance": 8},
|
|
{"id": "张良", "name": "张良", "type": "strategist", "size": 25, "desc": "汉朝开国谋士,运筹帷幄", "img": "", "importance": 8},
|
|
{"id": "吕雉", "name": "吕雉", "type": "empress", "size": 22, "desc": "刘邦皇后,中国历史上第一位皇后", "img": "", "importance": 7},
|
|
{"id": "汉武帝", "name": "汉武帝", "type": "emperor", "size": 30, "desc": "西汉第七位皇帝,开创汉武盛世", "img": "", "importance": 9},
|
|
{"id": "卫青", "name": "卫青", "type": "general", "size": 25, "desc": "汉武帝时期名将,抗击匈奴", "img": "", "importance": 8},
|
|
{"id": "霍去病", "name": "霍去病", "type": "general", "size": 25, "desc": "汉武帝时期名将,封狼居胥", "img": "", "importance": 8},
|
|
{"id": "司马迁", "name": "司马迁", "type": "historian", "size": 22, "desc": "西汉史学家,著有《史记》", "img": "", "importance": 7}
|
|
],
|
|
"links": [
|
|
{"source": "秦始皇", "target": "李斯", "relation": "political", "desc": "君臣关系"},
|
|
{"source": "秦始皇", "target": "蒙恬", "relation": "military", "desc": "君臣关系"},
|
|
{"source": "李斯", "target": "赵高", "relation": "political", "desc": "政敌关系"},
|
|
{"source": "秦始皇", "target": "胡亥", "relation": "family", "desc": "父子关系"},
|
|
{"source": "秦始皇", "target": "扶苏", "relation": "family", "desc": "父子关系"},
|
|
{"source": "赵高", "target": "胡亥", "relation": "political", "desc": "利用关系"},
|
|
{"source": "赵高", "target": "扶苏", "relation": "political", "desc": "敌对关系"},
|
|
{"source": "刘邦", "target": "项羽", "relation": "political", "desc": "敌对关系"},
|
|
{"source": "刘邦", "target": "萧何", "relation": "political", "desc": "君臣关系"},
|
|
{"source": "刘邦", "target": "韩信", "relation": "political", "desc": "君臣关系"},
|
|
{"source": "刘邦", "target": "张良", "relation": "political", "desc": "君臣关系"},
|
|
{"source": "刘邦", "target": "吕雉", "relation": "family", "desc": "夫妻关系"},
|
|
{"source": "项羽", "target": "韩信", "relation": "military", "desc": "曾经共事"},
|
|
{"source": "汉武帝", "target": "卫青", "relation": "military", "desc": "君臣关系"},
|
|
{"source": "汉武帝", "target": "霍去病", "relation": "military", "desc": "君臣关系"},
|
|
{"source": "卫青", "target": "霍去病", "relation": "family", "desc": "舅舅与外甥"},
|
|
{"source": "汉武帝", "target": "司马迁", "relation": "political", "desc": "君臣关系"}
|
|
]
|
|
};
|
|
|
|
const link = svg.append("g")
|
|
.attr("class", "links")
|
|
.selectAll("line")
|
|
.data(data.links)
|
|
.enter().append("line")
|
|
.attr("class", "link")
|
|
.attr("stroke", d => colorScale(d.relation))
|
|
.attr("stroke-width", 2);
|
|
|
|
const node = svg.append("g")
|
|
.attr("class", "nodes")
|
|
.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.size)
|
|
.attr("fill", d => {
|
|
if (d.type === "emperor" || d.type === "empress") return "#d4af37";
|
|
if (d.type === "general") return "#c62828";
|
|
if (d.type === "politician") return "#1565c0";
|
|
return "#2e7d32";
|
|
})
|
|
.attr("stroke-width", 1.5)
|
|
.on("mouseover", function(event, d) {
|
|
d3.select(".tooltip")
|
|
.style("opacity", 1)
|
|
.html(`<strong>${d.name}</strong><br>${d.desc}`)
|
|
.style("left", (event.pageX + 10) + "px")
|
|
.style("top", (event.pageY - 30) + "px");
|
|
})
|
|
.on("mouseout", function() {
|
|
d3.select(".tooltip").style("opacity", 0);
|
|
})
|
|
.on("click", function(event, d) {
|
|
updateInfoPanel(d);
|
|
});
|
|
|
|
node.append("text")
|
|
.attr("dy", 4)
|
|
.attr("text-anchor", "middle")
|
|
.attr("dominant-baseline", "central")
|
|
.text(d => d.name)
|
|
.attr("fill", "white")
|
|
.attr("font-size", d => Math.min(14, d.size * 0.5));
|
|
|
|
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 updateInfoPanel(d) {
|
|
const panel = d3.select("#infoPanel");
|
|
panel.html("");
|
|
|
|
panel.append("h2").text(d.name);
|
|
panel.append("p").text(d.desc);
|
|
|
|
panel.append("h3").text("人物关系");
|
|
|
|
const relations = data.links.filter(link =>
|
|
link.source.id === d.id || link.target.id === d.id
|
|
);
|
|
|
|
if (relations.length === 0) {
|
|
panel.append("p").text("暂无相关关系信息");
|
|
return;
|
|
}
|
|
|
|
const relationList = panel.append("ul");
|
|
|
|
relations.forEach(rel => {
|
|
const otherNode = rel.source.id === d.id ?
|
|
data.nodes.find(n => n.id === rel.target.id) :
|
|
data.nodes.find(n => n.id === rel.source.id);
|
|
|
|
const relationType = rel.relation === "political" ? "政治关系" :
|
|
rel.relation === "military" ? "军事关系" : "家族关系";
|
|
|
|
relationList.append("li")
|
|
.html(`<strong>${otherNode.name}</strong> - ${relationType}: ${rel.desc}`);
|
|
});
|
|
}
|
|
|
|
// 布局切换
|
|
d3.select("#force").on("click", function() {
|
|
simulation
|
|
.force("charge", d3.forceManyBody().strength(-300))
|
|
.force("center", d3.forceCenter(width / 2, height / 2))
|
|
.force("collision", d3.forceCollide().radius(d => d.size))
|
|
.alpha(1).restart();
|
|
});
|
|
|
|
d3.select("#radial").on("click", function() {
|
|
simulation
|
|
.force("center", null)
|
|
.force("radial", d3.forceRadial(width / 3, width / 2, height / 2).strength(0.1))
|
|
.alpha(1).restart();
|
|
});
|
|
|
|
d3.select("#circular").on("click", function() {
|
|
simulation
|
|
.force("center", null)
|
|
.force("radial", d3.forceRadial(width / 4, width / 2, height / 2).strength(0.2))
|
|
.alpha(1).restart();
|
|
});
|
|
|
|
d3.select("#grid").on("click", function() {
|
|
const cols = 4;
|
|
const padding = width / (cols + 1);
|
|
const radius = padding / 2;
|
|
|
|
data.nodes.forEach((d, i) => {
|
|
d.x = (i % cols + 1) * padding;
|
|
d.y = Math.floor(i / cols + 1) * padding;
|
|
});
|
|
|
|
simulation
|
|
.force("center", null)
|
|
.force("radial", null)
|
|
.alpha(1).restart();
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|