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.

393 lines
15 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;
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>