-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
294 lines (265 loc) · 9.93 KB
/
index.html
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
<!DOCTYPE html>
<html lang="ja">
<head>
<title>Network Visualizer</title>
<meta charset="utf-8" />
<script src="https://d3js.org/d3.v7.min.js"></script>
<style>
body,
html {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
#wrapper {
display: flex;
height: 100%;
}
#network {
flex-grow: 1;
}
#controls {
flex-basis: 250px;
padding: 10px;
}
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #999;
stroke-opacity: 0.6;
}
.texts {
font-family: Arial, sans-serif;
font-size: 12px;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="network"></div>
<div id="controls">
<input type="file" id="fileInput" /><br />
<hr />
<input type="checkbox" id="colorCheckbox" onchange="toggleNodeColor()" checked="true" />
<label for="colorCheckbox">ノードに色付けする</label><br />
<input type="checkbox" id="nameCheckbox" onchange="toggleNodeName()" checked="true" />
<label for="nameCheckbox">ノード名を表示する</label><br />
<input type="checkbox" id="nodeFixCheckbox" onchange="toggleNodeFix()" />
<label for="nodeFixCheckbox">ノードを固定する</label><br />
<input type="checkbox" id="directionCheckbox" onchange="toggleLinkDirection()" checked="true" />
<label for="directionCheckbox">有向グラフ</label><br />
</div>
</div>
<script>
nodes = [
{ id: "ノード1", name: "Node 1", group: 1, explain: "This is node 1" },
{ id: "ノード2", name: "Node 2", group: 2, explain: "This is node 2" },
];
links = [
{ source: "ノード1", target: "ノード2", value: 2 },
];
function drawNetwork(event) {
d3.select("#network").html(""); // Clear
// ノードの色設定
if (nodes.some(node => node.hasOwnProperty('group'))) {
const label = document.querySelector("label[for='colorCheckbox']");
label.style.color = "black";
colorCheckbox.disabled = false;
} else {
const label = document.querySelector("label[for='colorCheckbox']");
label.style.color = "gray";
colorCheckbox.checked = false;
colorCheckbox.disabled = true;
}
// ウィンドウのサイズに基づいてサイズを設定
const width = window.innerWidth - (250 + 10 * 2);
const height = window.innerHeight;
const svg = d3.select("#network").append("svg")
.attr("width", "100%")
.attr("height", "100%");
// Zoom動作の定義
const zoom = d3.zoom()
.scaleExtent([1 / 4, 4]) // 最小縮小率と最大拡大率
.on("zoom", (event) => {
g.attr("transform", event.transform);
});
svg.call(zoom); // SVGにZoom動作を適用
// グラフ要素のためのコンテナを追加
const g = svg.append("g");
svg.append("defs").selectAll("marker")
.data(["end"])
.enter().append("marker")
.attr("id", d => d)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", 0)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("path")
.attr("fill", "#999")
.attr("d", "M0,-5L10,0L0,5");
color = d3.scaleOrdinal(d3.schemeCategory10);
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links)
.id(d => d.id)
.distance(100) // リンクの距離
.strength(0.5) // リンクの強度
)
.force("charge", d3.forceManyBody().strength(-100)) // 斥力
.force("center", d3.forceCenter(width / 2, height / 2));
link = g.append("g")
.attr("class", "links")
.selectAll("line")
.data(links)
.enter().append("line")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.attr("stroke-width", 1.5);
toggleLinkDirection();
node = g.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("r", 5);
toggleNodeColor();
// ノード名をテキストとして追加
text = g.append("g")
.attr("class", "texts")
.selectAll("text")
.data(nodes)
.enter().append("text")
.attr("x", 8)
.attr("y", ".31em")
.text(d => d.name);
toggleNodeName();
// ツールチップの追加
node.append("title")
.text(d => d.explain);
link.append("title")
.text(d => d.value);
simulation.on("tick", () => {
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("cx", d => d.x)
.attr("cy", d => d.y);
text.attr("x", d => d.x + 8)
.attr("y", d => d.y + 3);
});
// ノードにドラッグ機能を適用
node.call(d3.drag()
.on("start", (event, d) => {
if (!event.active) {
simulation.alphaTarget(0.3).restart();
}
d.fx = d.x;
d.fy = d.y;
})
.on("drag", (event, d) => {
d.fx = event.x;
d.fy = event.y;
})
.on("end", (event, d) => {
if (!event.active) {
simulation.alphaTarget(0);
}
if (nodeFixCheckbox.checked) {
node.each(d => {
d.fx = d.x;
d.fy = d.y;
});
} else {
node.each(d => {
d.fx = null;
d.fy = null;
});
}
}));
}
/**
* チェックボックスの状態に従ってノードの色を変更
*/
function toggleNodeColor() {
if (colorCheckbox.checked) {
node.style("fill", (d) => color(d.group));
} else {
node.style("fill", "black");
}
}
/**
* チェックボックスの状態に従ってノード名の表示を変更
*/
function toggleNodeName() {
if (nameCheckbox.checked) {
text.style("display", "block");
} else {
text.style("display", "none");
}
}
/**
* チェックボックスの状態に従って有向辺に変更
*/
function toggleLinkDirection() {
if (directionCheckbox.checked) {
link.attr("marker-end", "url(#end)");
} else {
link.attr("marker-end", "");
}
}
/**
* チェックボックスの状態に従ってノードの固定を変更
*/
function toggleNodeFix() {
if (nodeFixCheckbox.checked) {
node.each(d => {
d.fx = d.x;
d.fy = d.y;
});
} else {
node.each(d => {
d.fx = null;
d.fy = null;
});
}
}
document.addEventListener("DOMContentLoaded", drawNetwork);
document.getElementById("fileInput").addEventListener("change", function (event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
const content = e.target.result;
if (file.name.endsWith(".json")) {
const data = JSON.parse(content);
nodes = data.nodes;
nodes.forEach(node => {
if (!node.hasOwnProperty('name')) {
node.name = node.id;
}
});
links = data.links;
nodeFixCheckbox.checked = false;
drawNetwork(data);
} else if (file.name.endsWith(".csv")) {
const data = d3.csvParse(content);
links = data.map(d => ({ source: d.source, target: d.target, value: d.value }));
nodes = Array.from(new Set(links.flatMap(link => [link.source, link.target]))).map(id => ({ id, name: id }));
nodeFixCheckbox.checked = false;
drawNetwork(data);
} else {
alert("ファイル形式が不正です");
}
};
reader.readAsText(file);
}
});
</script>
</body>
</html>