-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClusterMapping_ForceDirectedLayout_ONCLICK.html
119 lines (93 loc) · 2.98 KB
/
ClusterMapping_ForceDirectedLayout_ONCLICK.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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #999;
stroke-opacity: .6;
}
</style>
<body>
<div id="top10values"></div>
<!--<script src="https://gist.github.com/grantmichaels/5237322.js"></script>-->
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://raw.githubusercontent.com/d3/d3-plugins/master/fisheye/fisheye.js"></script>
<script>
var flag = 0;
var width = 1000;
height = 1000;
var color = d3.scale.category20();
var force = d3.layout.force()
.charge(-120)
.linkDistance(30)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var fisheye = d3.fisheye.circular()
.radius(200)
.distortion(2);
d3.json("clustermapping.json", function(error, graph) {
if (error) throw error;
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
.on("click", function(d){
if(flag == 0){
flag = 1;
tooltip.select("#top10").html(d.top10values.map(function(d){return d[0];}).join("<br>"))
tooltip.style("top", (d3.event.pageY - 130) + "px").style("left", (d3.event.pageX - 130) + "px")
return tooltip.style("visibility", "visible");
}
else{
flag = 0;
return tooltip.style("visibility", "hidden"); }
})
.style("fill", function(d) { return color(d.group); })
.call(force.drag);
node.append("title")
.text(function(d) { return d.name; });
tooltip = d3.select("#top10values")
.append("div")
.attr("class", "my-tooltip")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden");
tooltip.append("div")
.attr("id", "top10")
.text("simple");
svg.on("mousemove", function() {
fisheye.focus(d3.mouse(this));
node.each(function(d) { d.fisheye = fisheye(d); })
.attr("cx", function(d) { return d.fisheye.x; })
.attr("cy", function(d) { return d.fisheye.y; })
.attr("r", function(d) { return d.fisheye.z * 4.5; });
link.attr("x1", function(d) { return d.source.fisheye.x; })
.attr("y1", function(d) { return d.source.fisheye.y; })
.attr("x2", function(d) { return d.target.fisheye.x; })
.attr("y2", function(d) { return d.target.fisheye.y; });
});
force.on("tick", function() {
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; });
});
});
</script>