-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
158 lines (129 loc) · 4.44 KB
/
test.html
File metadata and controls
158 lines (129 loc) · 4.44 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
<!DOCTYPE html>
<head>
<title>Moving Bubble Tutorial</title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" />
<meta charset="utf-8">
</head>
<body>
<div id="main-wrapper">
<div id="chart"></div>
</div>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script>
var margin = {top: 16, right: 0, bottom: 0, left: 0},
width = 950 - margin.left - margin.right,
height = 700 - margin.top - margin.bottom;
var node_radius = 5,
padding = 1,
cluster_padding = 10,
num_nodes = 200;
var svg = d3.select("#chart").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Clusterings
var foci = {
"toppos": { x: 475, y: 150, color: "#cc5efa" },
"leftpos": { x: 225, y: 300, color: "#29bf10" },
"rightpos": { x: 725, y: 300, color: "#23cdc7" },
"bottompos": { x: 475, y: 450, color: "#eb494f" },
};
// Create node objects
var nodes = d3.range(0, num_nodes).map(function(o, i) {
return {
id: "node" + i,
x: foci.toppos.x + Math.random(),
y: foci.toppos.y + Math.random(),
radius: node_radius,
choice: "toppos",
}
});
// Force-directed layout
var simulation = d3.forceSimulation(nodes)
.force("charge", d3.forceManyBody().strength(0))
.force("center", d3.forceCenter(width / 2, height / 2))
.force("collide", d3.forceCollide().radius(node_radius + padding))
.alpha(0.1)
.on("tick", tick);
// Draw circle for each node.
var circle = svg.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("id", function(d) { return d.id; })
.attr("class", "node")
.style("fill", function(d) { return foci[d.choice].color; });
// For smoother initial transition to settling spots.
// To be more precise, this will make the bubles grow larger and larger at the beginning (attrTween take function as parameter, and it automatically put values from 0 to 1)
circle.transition()
.duration(900)
.delay(function(d,i) { return i * 5; })
.attrTween("r", function(d) {
var i = d3.interpolate(0, d.radius);
return function(t) { return d.radius = i(t); };
});
// Run function periodically to make things move.
var timeout;
function timer() {
// Random place for a node to go
var choices = Object.keys(foci);
var foci_index = Math.floor(Math.random() * choices.length);
var choice = choices[foci_index];
// Update random node
var random_index = Math.floor(Math.random() * nodes.length);
nodes[random_index].fx = foci[choice].x;
nodes[random_index].fy = foci[choice].y;
nodes[random_index].choice = choice;
simulation.alpha(0.1).restart();
// Run it again in a few seconds.
timeout = setTimeout(timer, 400);
}
timeout = setTimeout(timer, 400);
//
// Force-directed boilerplate functions
//
function tick() {
circle
.each(gravity(0.051 * simulation.alpha()))
.each(collide(0.5))
.style("fill", function(d) { return foci[d.choice].color; })
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
}
// Move nodes toward cluster focus.
function gravity(alpha) {
return function(d) {
d.y += (foci[d.choice].y - d.y) * alpha;
d.x += (foci[d.choice].x - d.x) * alpha;
};
}
// Resolve collisions between nodes.
function collide(alpha) {
var quadtree = d3.quadtree(nodes);
return function(d) {
var r = d.radius + node_radius + Math.max(padding, cluster_padding),
nx1 = d.x - r,
nx2 = d.x + r,
ny1 = d.y - r,
ny2 = d.y + r;
quadtree.visit(function(quad, x1, y1, x2, y2) {
if (quad.point && (quad.point !== d)) {
var x = d.x - quad.point.x,
y = d.y - quad.point.y,
l = Math.sqrt(x * x + y * y),
r = d.radius + quad.point.radius + (d.choice === quad.point.choice ? padding : cluster_padding);
if (l < r) {
l = (l - r) / l * alpha;
d.x -= x *= l;
d.y -= y *= l;
quad.point.x += x;
quad.point.y += y;
}
}
return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
});
};
}
</script>
</body>
</html>