-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.js
304 lines (239 loc) · 9.02 KB
/
graph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
const width = 800;
const height = 600;
let nodes = [];
let links = [];
let nodeIdCounter = 0;
// 添加缩放和拖动功能
const zoom = d3.zoom()
.scaleExtent([0.1, 10]) // 设置缩放范围
.on("zoom", function(event) {
svg.attr("transform", event.transform);
});
const svg = d3.select("#graph-container")
.append("svg")
.attr("width", window.innerWidth)
.attr("height", window.innerHeight)
.call(zoom);
// 当窗口大小改变时,更新 SVG 尺寸
window.addEventListener('resize', () => {
svg.attr("width", window.innerWidth)
.attr("height", window.innerHeight);
});
// 将缩放功能应用于 SVG
svg.call(zoom);
// 添加一个容器用于包含所有图形元素
const g = svg.append("g");
// 更新 linkGroup 和 nodeGroup 为新的容器 g 的子元素
const linkGroup = g.append("g").attr("class", "links");
const nodeGroup = g.append("g").attr("class", "nodes");
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody().strength(-100)) // 减小排斥力
.force("center", d3.forceCenter(width / 2, height / 2))
.force("collide", d3.forceCollide().radius(d => Math.max(10, d.degree * 5) + 10)) // 避免重叠
.force("x", d3.forceX(width / 2).strength(0.1)) // 添加 x 方向的力
.force("y", d3.forceY(height / 2).strength(0.1)) // 添加 y 方向的力
.on("tick", ticked);
// 绑定 importGraph 按钮的事件
document.getElementById('importGraph').addEventListener('click', function() {
const jsonInput = prompt("Enter the graph JSON data:", "{}");
if (jsonInput) {
try {
const graphData = JSON.parse(jsonInput);
// Validate graphData structure
if (Array.isArray(graphData.nodes) && Array.isArray(graphData.links)) {
nodes = graphData.nodes;
links = graphData.links;
updateGraph();
} else {
alert("Invalid graph data format.");
}
} catch (e) {
alert("Failed to parse JSON data.");
}
}
});
// 添加边界约束,确保节点不会超出视图边界
function ticked() {
linkGroup.selectAll("line")
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);
nodeGroup.selectAll("g")
.attr("transform", d => {
// 边界约束
d.x = Math.max(50, Math.min(width - 50, d.x));
d.y = Math.max(50, Math.min(height - 50, d.y));
return `translate(${d.x},${d.y})`;
});
}
function editNode(event, d) {
// Prevent the default click event from firing
event.preventDefault();
// Check if the textarea is already open
const isOpen = d3.select(this).select(".node-textarea").node();
// If the textarea is open, close it and restore the node size
if (isOpen) {
// Remove the textarea
d3.select(this).select(".node-textarea").remove();
// Restore the size of the node
d3.select(this).select("circle")
.transition()
.duration(250)
.attr("r", d => Math.max(10, d.degree * 5)); // Adjust back to original size based on degree
// Restore the text in the node to the attribute value
d3.select(this).select("text")
.text(d => d.text || d.name);
// No further action needed
return;
}
// If the textarea is not open, proceed to open it
d3.selectAll(".node-textarea").remove();
// Increase the size of the node
d3.select(this).select("circle")
.transition()
.duration(250)
.attr("r", 40);
// Append a textarea inside the node group
const textarea = d3.select(this).append("foreignObject")
.attr("class", "node-textarea")
.attr("width", 80)
.attr("height", 60)
.attr("x", -40)
.attr("y", -30)
.append("xhtml:textarea")
.style("width", "80px")
.style("height", "60px")
.style("background-color", "#003300") // Dark green background
.style("color", "#00ff00") // Bright green text
.style("border", "none")
.style("resize", "none")
.text(d.text || "") // Use the existing text if any
// When the user finishes editing (on blur event)
.on("blur", function() {
const newText = this.value;
d.text = newText; // Save the text to the node data
updateGraph(); // Update the graph
})
.on("click", function(event) {
// Prevent click event from propagating to parent node
event.stopPropagation();
});
// Automatically focus on the textarea
textarea.node().focus();
}
function updateGraph() {
// Reset node degrees
nodes.forEach(node => node.degree = 0);
// Calculate degrees for each node
links.forEach(link => {
nodes.find(node => node.id === link.source.id).degree++;
nodes.find(node => node.id === link.target.id).degree++;
});
const linkElements = linkGroup.selectAll("line")
.data(links);
linkElements.exit().remove();
linkElements.enter().append("line")
.attr("class", "link")
.merge(linkElements);
const nodeElements = nodeGroup.selectAll("g")
.data(nodes, d => d.id);
const nodeEnter = nodeElements.enter()
.append("g")
.attr("class", "node")
.call(d3.drag()
.on("start", dragStarted)
.on("drag", dragged)
.on("end", dragEnded))
.on("click", selectNode)
.on("dblclick", editNode); // Add this line to handle double-click
nodeEnter.append("circle")
.attr("r", d => Math.max(10, d.degree * 5, d.text ? d.text.length * 3 : 0)) // Adjust the radius based on degree and text length
.attr("fill", "#69b3a2");
nodeEnter.append("text")
.attr("dx", 25)
.attr("dy", 5)
.text(d => d.text || d.name); // Use the text attribute if available
nodeElements.exit().remove();
nodeEnter.merge(nodeElements)
.select("circle")
.attr("r", d => Math.max(10, d.degree * 5)); // Adjust the radius for existing nodes
nodeEnter.merge(nodeElements)
.select("text")
.text(d => d.name);
simulation.nodes(nodes);
simulation.force("link").links(links);
simulation.alpha(0.3).restart(); // 降低 alpha 值,使布局变化更平缓
}
function selectNode(event, d) {
if (event.shiftKey) { // only when press shift button, allow multiple selection
d3.select(this).classed("selected", !d3.select(this).classed("selected"));
} else {
d3.selectAll(".node").classed("selected", false);
d3.select(this).classed("selected", true);
}
}
function addNode() {
const centerX = width / 2;
const centerY = height / 2;
const radius = 100; // 增加新节点的分布范围
let x = centerX + (Math.random() - 0.5) * radius * 2;
let y = centerY + (Math.random() - 0.5) * radius * 2;
// 边界约束
x = Math.max(50, Math.min(width - 50, x));
y = Math.max(50, Math.min(height - 50, y));
const newNode = { id: ++nodeIdCounter, name: `Node ${nodeIdCounter}`, x, y };
nodes.push(newNode);
updateGraph();
simulation.alpha(1).restart(); // 重启模拟以重新计算布局
}
function removeNode() {
const selected = d3.select(".node.selected");
if (!selected.empty()) {
const nodeId = selected.datum().id;
nodes = nodes.filter(n => n.id !== nodeId);
links = links.filter(l => l.source.id !== nodeId && l.target.id !== nodeId);
updateGraph();
}
}
function connectNodes() {
const selectedNodes = d3.selectAll(".node.selected").data();
if (selectedNodes.length === 2) {
links.push({
source: selectedNodes[0],
target: selectedNodes[1]
});
d3.selectAll(".node").classed("selected", false); // cancel the node selection state
updateGraph();
}
}
function renameNode() {
const selected = d3.select(".node.selected");
if (!selected.empty()) {
const newName = prompt("Enter new name:", selected.datum().name);
if (newName) {
selected.datum().name = newName;
updateGraph();
}
}
}
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;
}
d3.select("#addNode").on("click", addNode);
d3.select("#removeNode").on("click", removeNode);
d3.select("#connectNodes").on("click", connectNodes);
d3.select("#renameNode").on("click", renameNode);
updateGraph();