-
Notifications
You must be signed in to change notification settings - Fork 0
/
force.html
261 lines (237 loc) · 7.88 KB
/
force.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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg {
display: block;
margin: 0 auto;
}
.svg-tooltip {
pointer-events: none;
}
.tooltip {
padding: 10px;
color: #4A22FF;
}
.lead {
color:#000;
font-weight: bold;
}
.favicon{
height:30px;
width:30px;
margin:0 10px 0 0;
padding:0 0 0 0;
float:left;
}
p {
margin: 5px 0px;
}
polygon {
pointer-events: none;
}
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
.node circle {
stroke: #fff;
stroke-width: 1.5px;
}
.node circle text {
display: inline;
color:black;
font: 11px sans-serif;
}
.node circle:hover {
stroke: #000;
stroke-width:3px;
}
.top-bar{
width:100%;
height:100px;
border:2px solid blue;
background-color:#ddd;
overflow:hidden;
}
.top-bar a{
float:left;
margin:3px;
padding:3px;
}
</style>
<!-- <div class="top-bar">
</div> -->
<svg width="1100" height="670">
<defs id="mdef">
<pattern id="image" x="0" y="0" height="40" width="40">
<image x="0" y="0" width="20" height="20" xlink:href="data/fav/apple.png"></image>
</pattern>
</defs>
</svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
// var fileList = [{"id":"www.google.com"},{"id":"www.apple.com"},{"id":"www.google.com"},{"id":"www.apple.com"},{"id":"www.google.com"},{"id":"www.apple.com"},{"id":"www.google.com"},{"id":"www.apple.com"},{"id":"www.google.com"},{"id":"www.apple.com"},{"id":"www.google.com"},{"id":"www.apple.com"},{"id":"www.google.com"},{"id":"www.apple.com"},{"id":"www.google.com"},{"id":"www.apple.com"},{"id":"www.google.com"},{"id":"www.apple.com"},{"id":"www.google.com"},{"id":"www.apple.com"}]
// let fileNodes = d3.select(".top-bar").selectAll("a").data(fileList).enter().append('a').attr('href',function(d){return d.id}).text(function(d){return d.id});
let url = new URL(location.href);
let sp = new URLSearchParams(url.search);
var filename = sp.get('file');
var favicon = "data/fav/"+filename.split('.')[0]+".png";
var jsonFile = "data/"+filename;
var margin = {top: 20, right: 10, bottom: 20, left: 10};
// var width = 1100 - margin.left - margin.right;
// var height = 670 - margin.top - margin.bottom;
var foWidth = 300;
var foHeight = 200;
var anchor = {'w': width/3, 'h': height/3};
var t = 50, k = 15;
var tip = {'w': (3/4 * t), 'h': k};
var width = Math.max(document.documentElement.clientWidth- margin.left - margin.right, window.innerWidth- margin.left - margin.right || 0);
var height = Math.max(document.documentElement.clientHeight- margin.top - margin.bottom, window.innerHeight- margin.top - margin.bottom || 0);
var svg = d3.select("svg")
.attr("height",height)
.attr("width",width);
//
// width = +svg.attr("width"),
// height = +svg.attr("height");
var radius = 10;
var color = d3.scaleOrdinal(d3.schemeCategory20);
var gravFunctionX = function(node){return (1/(node.index+.11));};
var gravFunctionY = function(node){return (Math.log10(node.group+.1));};
var gravFunctionXRoot = function(node){if(node.index==0){return 1}else{return 0}};
var gravFunctionYRoot = function(node){if(node.index==0){return 1}else{return 0}};
var gravFunctionYPages = function(node){if(node.group==1){return 2+node.index*.01}else{return 0}};
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.id; }))
// .force("collide", d3.forceCollide())
.force("charge", d3.forceManyBody().strength(function strength(){return -30;}))
.force("forceX", d3.forceX().strength(gravFunctionX)
.x(function(node){return 0})
)
.force("forceY", d3.forceY()
.strength(gravFunctionY)
.y(function(node){return 0})
)
.force("forceYPages", d3.forceY()
.strength(gravFunctionYPages)
.y(function(node){return height})
)
// .force("gravFunctionXRoot", d3.forceX().strength(gravFunctionXRoot)
// .x(function(node){return width})
// )
// .force("gravFunctionYRoot", d3.forceY().strength(gravFunctionYRoot)
// .y(function(node){return height/2})
// )
.force("center", d3.forceCenter(width / 2, height / 2));
d3.json(jsonFile, function(error, graph) {
if (error) throw error;
var link = svg.append("g")
.attr("class","links")
.selectAll("line")
.data(graph.links)
.enter().append("line")
.attr("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.append("g")
.attr("class", "node").selectAll("g")
.data(graph.nodes).enter().append("g").append("circle")
// .attr("r", 5)
.attr("r", function(d){return 25/(Math.sqrt(d.group)+.5)})
.style("fill", function(d) { return color(d.group); })
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
var rootNode = svg.selectAll('g.node g circle')
.filter(function (d, i) { return i === 0;})
.style("fill","red")
.attr("cx",0)
.attr("cy",0)
.attr("fixed",true);
var rootNodeId = rootNode.data()[0].id;
// node.append("title")
// .text(function(d) { return d.id+"\n"+d.mt; });
node.on("mouseover", function(d){
if(d.x+foWidth>=width){
var tx = d.x-37-foWidth;
}else{
var tx = d.x-37;
}
if(d.y+foHeight>=height){
var ty = d.y+15-foHeight;
}else{
var ty = d.y+15;
}
var fo = svg.append("foreignObject")
.attr('x', tx)
.attr('y', ty)
.attr('width', 300)
.attr('height', 200)
.attr('class', 'svg-tooltip');
var div = fo.append('xhtml:div')
.append('div')
.attr('class', 'tooltip')
div.append('p')
.html(function(f){return "<img class='favicon' src='"+favicon+"'/>";});
div.append('p')
.attr('class','lead')
.html("content-type: <b>"+d.mt+"</b>")
.append('p')
.attr('class', 'lead')
.html("URL: <b>"+d.id+"</b>");
div.append('p')
.html(function(f){if(d.mt.indexOf('image')>=0){return "<img src='"+d.id+"'/>"}else{return "";}});
fo.attr({
'height': foHeight
});
svg.insert('polygon', '.svg-tooltip')
.attr('points',"0,0 0," + foHeight + " " + foWidth + "," + foHeight + " " + foWidth + ",0 " + (t) + ",0 " + tip.w + "," + (-tip.h) + " " + (t/2) + ",0")
.attr('height', foHeight + tip.h)
.attr('width', foWidth)
.attr('fill','#D8D8D8')
.attr('opacity', 0.75)
.attr('transform', "translate("+tx+","+ty+")")
})
.on('mouseout', function() {
svg.selectAll('.svg-tooltip').remove();
svg.selectAll('polygon').remove();
});
simulation.nodes(graph.nodes)
.on("tick", ticked);
simulation.force("link")
.links(graph.links)
.distance(function(link){if(link.source.id == rootNodeId){return 100}else{return .15/link.target.group;}})
.strength(function strength(link) {
// return 1 / link.target.group;
if(link.source.id == rootNodeId){return .8}else{return .2;}
// return .07;
// return 2;
}
);
simulation.alphaTarget(0).alphaDecay(0.25).velocityDecay(.7);
function ticked() {
link
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node
// .attr("cx", function(d) { return d.x; })
// .attr("cy", function(d) { return d.y; });
.attr("cx", function(d) { return d.x = Math.max(radius, Math.min(width - radius, d.x)); })
.attr("cy", function(d) { return d.y = Math.max(radius, Math.min(height - radius, d.y)); });
}
});
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.1).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
</script>