本範例實現一個互動式人物關係網絡圖,支援:
var relationData = [
{
name: "成龍",
friends: [
{ name: "房祖名", relationship: "父子", type: "family" },
{ name: "林鳳嬌", relationship: "夫妻", type: "family" },
{ name: "吳綺莉", relationship: "緋聞", type: "romance" },
{ name: "徐靜蕾", relationship: "激吻", type: "romance" }
]
},
{
name: "房祖名",
friends: [
{ name: "成龍", relationship: "父子", type: "family" },
{ name: "林鳳嬌", relationship: "母子", type: "family" },
{ name: "方大同", relationship: "情敵", type: "conflict" },
{ name: "薛凱琪", relationship: "女友", type: "romance" },
{ name: "陳坤", relationship: "朋友", type: "friend" },
{ name: "趙薇", relationship: "朋友", type: "friend" }
]
}
// ... 更多節點
];
為避免引入 D3.js 等重型庫,採用簡化的力導向演算法:
var Graph = {
nodes: [], // { id, name, x, y, vx, vy, isCenter }
links: [], // { source, target, relationship, type }
centerNode: null,
width: 800,
height: 600,
// 物理參數
k: 150, // 理想彈簧長度
repulsion: 8000, // 排斥力常數
damping: 0.85, // 阻尼
init: function(data, centerName) {
// 1. 建立節點映射
var nameToNode = {};
data.forEach(function(d) {
var node = { id: d.name, name: d.name, x: 0, y: 0, vx: 0, vy: 0 };
this.nodes.push(node);
nameToNode[d.name] = node;
}, this);
// 2. 建立連結
data.forEach(function(d) {
var source = nameToNode[d.name];
d.friends.forEach(function(f) {
var target = nameToNode[f.name];
if (target) {
this.links.push({ source: source, target: target,
relationship: f.relationship, type: f.type });
}
}, this);
}, this);
// 3. 設定中心節點
this.centerNode = nameToNode[centerName] || this.nodes[0];
this.centerNode.isCenter = true;
this.layout();
},
layout: function() {
// 初始佈局:中心節點置中,其他節點圓周分佈
var angleStep = (Math.PI * 2) / (this.nodes.length - 1);
var radius = Math.min(this.width, this.height) * 0.35;
this.nodes.forEach(function(node, i) {
if (node === this.centerNode) {
node.x = this.width / 2;
node.y = this.height / 2;
} else {
var angle = (i - 1) * angleStep;
node.x = this.width / 2 + Math.cos(angle) * radius;
node.y = this.height / 2 + Math.sin(angle) * radius;
}
}, this);
// 迭代求解(簡化版:固定 50 幀)
for (var iter = 0; iter < 50; iter++) this.tick();
this.render();
},
tick: function() {
var cx = this.width / 2, cy = this.height / 2;
// 1. 排斥力:所有節點兩兩互斥
this.nodes.forEach(function(a) {
this.nodes.forEach(function(b) {
if (a === b) return;
var dx = a.x - b.x, dy = a.y - b.y;
var dist = Math.sqrt(dx*dx + dy*dy) || 1;
var force = this.repulsion / (dist * dist);
a.vx += (dx / dist) * force;
a.vy += (dy / dist) * force;
}, this);
}, this);
// 2. 吸引力:連結節點拉近
this.links.forEach(function(link) {
var dx = link.target.x - link.source.x;
var dy = link.target.y - link.source.y;
var dist = Math.sqrt(dx*dx + dy*dy) || 1;
var force = (dist - this.k) / dist * 0.5;
link.source.vx += dx * force;
link.source.vy += dy * force;
link.target.vx -= dx * force;
link.target.vy -= dy * force;
}, this);
// 3. 中心約束:中心節點強制歸位
if (this.centerNode) {
var dx = cx - this.centerNode.x;
var dy = cy - this.centerNode.y;
this.centerNode.vx += dx * 0.1;
this.centerNode.vy += dy * 0.1;
}
// 4. 更新位置 + 阻尼 + 邊界限制
this.nodes.forEach(function(node) {
if (node === this.centerNode) return; // 中心節點不移動
node.vx *= this.damping;
node.vy *= this.damping;
node.x += node.vx;
node.y += node.vy;
// 邊界約束
node.x = Math.max(50, Math.min(this.width - 50, node.x));
node.y = Math.max(50, Math.min(this.height - 50, node.y));
}, this);
},
// 點擊節點切換中心
setCenter: function(name) {
var newCenter = this.nodes.find(function(n) { return n.id === name; });
if (newCenter && newCenter !== this.centerNode) {
this.centerNode.isCenter = false;
this.centerNode = newCenter;
this.centerNode.isCenter = true;
this.animateToNewLayout();
}
},
animateToNewLayout: function() {
// 使用 jQuery animate 過渡到新佈局位置
// 實際專案建議使用 requestAnimationFrame 逐幀插值
},
render: function() {
// 使用 jQuery 操作 DOM 渲染節點與連線
// 連線使用 SVG 或 Canvas 繪製
}
};
| 類型 | 關係範例 | 線條顏色 | 線條樣式 | 節點邊框 |
|---|---|---|---|---|
| family | 父子、夫妻、母女 | #2ecc71 (綠) | 實線 3px | 雙層邊框 |
| romance | 緋聞、女友、激吻 | #e74c3c (紅) | 虛線 2px | 紅色發光 |
| friend | 朋友、歌手 | #3498db (藍) | 實線 1.5px | 藍色邊框 |
| conflict | 情敵、否認拍拖 | #e67e22 (橙) | 點線 2px | 橙色虛線邊框 |
| work | 電影、娛樂圈 | #9b59b6 (紫) | 實線 1px | 紫色邊框 |
function showTooltip(node, e) {
var html = '<strong>' + node.name + '</strong><br>';
var relations = Graph.links.filter(function(l) {
return l.source === node || l.target === node;
});
relations.forEach(function(l) {
var other = l.source === node ? l.target : l.source;
html += '<span class="rel-' + l.type + '">' + l.relationship + ':' + other.name + '</span><br>';
});
$("#tooltip").html(html).css({
left: e.pageX + 15,
top: e.pageY + 15,
display: "block"
});
}
var draggedNode = null;
$("#graph-container").on("mousedown", ".node", function(e) {
draggedNode = Graph.nodes.find(function(n) { return n.id === $(this).data("id"); });
draggedNode.fixed = true; // 拖拽時暫停物理模擬
e.preventDefault();
});
$(document).on("mousemove", function(e) {
if (draggedNode) {
draggedNode.x = e.pageX - $("#graph-container").offset().left;
draggedNode.y = e.pageY - $("#graph-container").offset().top;
Graph.render();
}
}).on("mouseup", function() {
if (draggedNode) {
draggedNode.fixed = false;
draggedNode = null;
}
});
此關係圖展示了資料驅動視覺化的核心流程:資料結構 → 佈局演算法 → 視覺編碼 → 互動綁定。雖然範例使用簡化物理模擬,但架構可平滑升級至 D3.js Force Layout、Cytoscape.js 或 Graphology 等專業圖繪製庫。關鍵在於將「關係資料」與「視覺表達」解耦,使業務邏輯變更不影響渲染層。