-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.html
126 lines (103 loc) · 2.78 KB
/
builder.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Force Layouts</title>
<script type="text/javascript" src="../DS/d3/d3.js"></script>
<script type="text/javascript" src="../DS/d3/d3.geom.js"></script>
<script type="text/javascript" src="../DS/d3/d3.layout.js"></script>
<link type="text/css" rel="stylesheet" href="../DS/style.css"/>
<link href="https://fonts.googleapis.com/css?family=Amatic+SC" rel="stylesheet">
<style type="text/css">
rect {
fill: #fff;
}
.cursor {
fill: none;
stroke: brown;
pointer-events: none;
}
.link {
stroke: #999;
}
#header{
font-family: 'Amatic SC', cursive;
font-size:70px;
text-align:center;
}
</style>
</head>
<body>
<div id="body">
<div id="chart"></div>
<div id="header">
Build your own graph
</div>
</div>
<script type="text/javascript">
var w = 1800,
h = 1000;
var force = d3.layout.force()
.charge(-100)
.linkDistance(100)
.size([w, h]);
var nodes = force.nodes(),
links = force.links();
var svg = d3.select("#chart").append("svg:svg")
.attr("width", w)
.attr("height", h);
svg.append("svg:rect")
.attr("width", w)
.attr("height", h);
var cursor = svg.append("svg:circle")
.attr("r", 40)
.attr("transform", "translate(-100,-100)")
.attr("class", "cursor");
force.on("tick", function() {
svg.selectAll("line.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; });
svg.selectAll("circle.node")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
svg.on("mousemove", function() {
cursor.attr("transform", "translate(" + d3.svg.mouse(this) + ")");
});
svg.on("mousedown", function() {
d3.event.preventDefault();
});
svg.on("click", function() {
var point = d3.svg.mouse(this),
node = {x: point[0], y: point[1], index: Math.floor(Math.random() * 10)},
n = nodes.push(node);
// add links to any nearby nodes
nodes.forEach(function(target) {
var x = target.x - node.x,
y = target.y - node.y;
if (Math.sqrt(x * x + y * y) < 30) {
links.push({source: node, target: target});
}
});
restart();
});
restart();
function restart() {
svg.selectAll("line.link")
.data(links)
.enter().insert("svg:line", "circle.node")
.attr("class", "link");
svg.selectAll("circle.node")
.data(nodes)
.enter().insert("svg:circle", "circle.cursor")
.attr("class", "node")
.attr("r", function(d) {return d.index;})
.attr("fill","hotpink")
.call(force.drag);
force.start();
}
</script>
</body>
</html>