forked from IanMulvany/ic2s2_graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_graph.html
91 lines (76 loc) · 2.03 KB
/
simple_graph.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
<!DOCTYPE html>
<meta charset="utf-8">
<!-- <script src="http://d3js.org/d3.v2.min.js?2.9.3"></script> -->
<script src="js/d3.v2.min.js"></script>
<style>
.link {
stroke: #aaa;
}
.node text {
stroke:#333;
cursos:pointer;
}
.node circle{
stroke:#fff;
stroke-width:3px;
fill:#555;
}
</style>
<body>
<script>
var width = 1200,
height = 1000
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force()
.gravity(.05)
.distance(100)
.charge(-100)
.size([width, height]);
d3.json("d3_graph.json", function(json) {
force
.nodes(json.nodes)
.links(json.links)
.start();
var link = svg.selectAll(".link")
.data(json.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.weight); });
var node = svg.selectAll(".node")
.data(json.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
d3.selectAll('g.node') //here's how you get all the nodes
.each(function(d) {
var d2=d3.select(this) // Transform to d3 Object
if (d.type == "Keyword"){
console.log(d);
d2.append("circle")
.attr("r","12");
console.log(d2.keys());
} else {
d2.append("circle")
.attr("r","10").attr("fill","blue");
}
// your update code here as it was in your example
});
node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) {
if (d.type == "Keyword"){
return d.name;
};
});
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("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
});
});
</script>