-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabeledgraph.html
More file actions
128 lines (105 loc) · 2.72 KB
/
labeledgraph.html
File metadata and controls
128 lines (105 loc) · 2.72 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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.link {
stroke: #ccc;
}
.node circle {
fill: #ccc;
stroke: #fff;
stroke-width: 1px;
}
.node text {
pointer-events: none;
font: 10px sans-serif;
color: black;
}
</style>
<body>
<div style="position: absolute;">
Name:<br>
<input id="name" type="text" name="name"><br>
Connections:<br>
<input id="connections" type="text" name="connections">
<button onClick="addNode()">Add Node</button>
</div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var graph = {
"nodes": [
{"name": "Data Structures", "group": 1},
{"name": "Algorithms"},
{"name": "Lists"},
{"name": "Sorting"},
{"name": "ArrayList"},
{"name": "LinkedList"},
{"name": "Array"}
],
"links": [
{"source":0,"target":1,"value":1},
{"source":2,"target":0,"value":1},
{"source":3,"target":1,"value":1},
{"source":4,"target":2,"value":1},
{"source":5,"target":2,"value":1},
{"source":6,"target":2,"value":1}
]
};
var width = 960,
height = 500
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force()
.gravity(0.05)
.distance(100)
.charge(-100)
.size([width, height]);
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link");
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("circle")
.attr('r', width/100);
node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { 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 + ")"; });
});
function addNode() {
var newNode = document.getElementById("name").value;
console.log(newNode);
//var connections = document.getElementById("connections").split(",");
graph.nodes[graph.nodes.length] = {"name": newNode};
console.log(graph.nodes);
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("circle")
.attr('r', width/100);
node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.name });
}
</script>