-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.html
More file actions
160 lines (123 loc) · 3.79 KB
/
graph.html
File metadata and controls
160 lines (123 loc) · 3.79 KB
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
<html xmlns:xlink="http://www.w3.org/1999/xlink">
<head>
<style>
.node {
opacity: 0.3;
}
.node:hover {
opacity: 0.3;
}
.link {
stroke: #999;
stroke-opacity: 0.3;
}
</style>
<script src="http://d3js.org/d3.v3.min.js"></script>
</head>
<body>
<div id="viz"></div>
<script>
function name(d) { return d.name; }
function group(d) { return d.group; }
var color = d3.scale.category10();
function colorByGroup(d) { return color(group(d)); }
var width = 960,
height = 900;
var svg = d3.select('#viz')
.append('svg')
.attr('width', width)
.attr('height', height);
var node, link;
var voronoi = d3.geom.voronoi()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; })
.clipExtent([[-10, -10], [width+10, height+10]]);
function recenterVoronoi(nodes) {
var shapes = [];
voronoi(nodes).forEach(function(d) {
if ( !d.length ) return;
var n = [];
d.forEach(function(c){
n.push([ c[0] - d.point.x, c[1] - d.point.y ]);
});
n.point = d.point;
shapes.push(n);
});
return shapes;
}
var force = d3.layout.force()
.charge(-2000)
.friction(0.3)
.linkDistance(100)//here to change link's length
.size([width, height]);
force.on('tick', function() {
node.attr('transform', function(d) { return 'translate('+d.x+','+d.y+')'; })
.attr('clip-path', function(d) { return 'url(#clip-'+d.index+')'; });
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; });
/* var clip = svg.selectAll('.clip')
.data( recenterVoronoi(node.data()), function(d) { return d.point.index; } );
clip.enter().append('clipPath')
.attr('id', function(d) { return 'clip-'+d.point.index; })
.attr('class', 'clip');
clip.exit().remove()
clip.selectAll('path').remove();
clip.append('path')
.attr('d', function(d) { return 'M'+d.join(',')+'Z'; });*/
});
d3.json('miserables1.json', function(err, data) {
data.nodes.forEach(function(d, i) {
d.id = i;
});
link = svg.selectAll('.link')
.data( data.links )
.enter().append('line')
.attr('class', 'link')
.style("stroke-width", function(d) { return Math.sqrt(d.value); });
node = svg.selectAll('.node')
.data( data.nodes )
.enter().append('a') //important change append('g') into ('a')
.attr("xlink:target","_blank")
.attr("xlink:href", function(d){return d.url;})
// .attr("xlink:target","_blank")
.attr('title', name)
.attr('class', 'node')
// .on("click", click)
.on("mouseover",mouseover)
.on("mouseout",mouseout)
.call( force.drag );
node.append('circle')
.attr('r', 20) //the r of each circle
.attr('fill', colorByGroup)
.attr('fill-opacity', 0.5);
node.append('circle')
.attr("xlink:href", function(d){return d.url;})
.attr('r', 4)
.attr('stroke', 'black');
node.append("text")
.attr("xlink:href", function(d){return d.url;})
.attr("dx", function(d) { return d.children ? -10 : 10; }) //the offset of x coordinate of text
.attr("dy", ".35em")
.style("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
.text(function(d) { return d.name;});
force
.nodes( data.nodes )
.links( data.links )
.start();
}
);
function click(d) {
d3.select(this)
.style('fill', d3.rgb(180, 120, 180));// the function of click event
}
function mouseover(d) {
d3.select(this).style({opacity:'1.0'});
}
function mouseout(d) {
d3.select(this).style({opacity:'0.5'});
}
</script>
</body>
</html>