forked from tinker10/D3-Labeler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
274 lines (218 loc) · 7.21 KB
/
index.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>D3-Labeler</title>
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.circ {
stroke: #000;
}
.rect {
stroke: #000;
}
.label {
font: 14px sans-serif;
}
.link {
stroke:gray;
stroke-width:0.5
}
h2 {
font-size: 22px;
}
div {
margin-top:35px;
margin-bottom:0px;
margin-left:150px;
}
p {
font-family: sans-serif;
font-size: 14px;
}
</style>
</head>
<body>
<div>
<h2>D3-Labeler</h2>
<p>A D3 plug-in for automatic label placement using simulated annealing that easily incorporates into existing D3 code, with <br> syntax mirroring other D3 layouts.</p>
<form>
Number of labels: <input type="text" id="n_labels" value=50 size="6"> |
<input type="radio" name="dist" id="uniform" value="Uniform" checked="checked">Uniform <input type="radio" name="dist" id="gaussian" value="Gaussian">Gaussian |
Number of MC sweeps: <input type="text" id="n_sweeps" value=1000 size="6"> |
<button type="button" id="start">Label</button>
</form>
</div>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="labeler.js"></script>
<script>
// Global variables
var anchor_array = [],
label_array = [],
margin = {top: 20, right: 300, bottom: 60, left: 150},
width = 1240 - margin.left - margin.right,
height = 530 - margin.top - margin.bottom,
x_mean = width/2,
y_mean = height/2,
offset = 4,
radius = 7;
var anchor_data, labels, circ, links, bounds;
// Setup canvas
var svg = d3.select("body").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 + ")");
// Draw boundaries of figure
var plot_boundary = svg.append("rect")
.attr("x", 0.0)
.attr("y", 0.0)
.attr("width", width)
.attr("height", height)
.style("fill-opacity", '0.0')
.style("stroke", "black")
.style("stroke-opacity", "0.4");
// Functions
function gaussian() {
// Box-Muller transformation
do
{
u = 2 * Math.random() - 1;
v = 2 * Math.random() - 1;
r = u * u + v * v;
}
while (r >= 1.0);
var c = Math.sqrt(-2 * Math.log(r) / r );
return u * c;
}
function redrawLabels() {
// Redraw labels and leader lines
labels
.transition()
.duration(800)
.attr("x", function(d) { return (d.x); })
.attr("y", function(d) { return (d.y); });
links
.transition()
.duration(800)
.attr("x2",function(d) { return (d.x); })
.attr("y2",function(d) { return (d.y); });
}
function randomize(count, is_uniform) {
anchor_array = [];
label_array = [];
for (var i = 0; i < count; i++) {
var xval, yval;
if (is_uniform) {
// Uniformly distributed of x, y
do {
xval=Math.random() * width;
} while (xval < 10 || xval > width - 60);
do {
yval=Math.random() * width;
} while (yval < 28 || yval > height - 15);
} else {
// Gaussian distributed x, y
do {
xval = x_mean + gaussian() * width / 6;
} while (xval < 10 || xval > width - 60);
do {
yval = y_mean + gaussian() * height / 6;
} while (yval < 28 || yval > height - 15);
}
// Put label and anchor positions into arrays
anchor_array.push({x: xval, y: yval, r: radius});
label_array.push({x: xval, y: yval, name: "Node "+String(i), width: 0.0, height: 0.0});
}
// Delete old stuff
svg.selectAll(".dot").data([]).exit().remove();
svg.selectAll(".label").data([]).exit().remove();
svg.selectAll(".circ").data([]).exit().remove();
svg.selectAll(".link").data([]).exit().remove();
svg.selectAll(".rect").data([]).exit().remove();
// Draw anchors
anchors = svg.selectAll(".dot")
.data(anchor_array)
.enter().append("circle")
.attr("class", "dot")
.attr("r", function(d) { return (d.r); })
.attr("cx", function(d) { return (d.x); })
.attr("cy", function(d) { return (d.y); })
.style("fill", 'green');
// Draw labels
labels = svg.selectAll(".label")
.data(label_array)
.enter()
.append("text")
.attr("class", "label")
.attr('text-anchor', 'start')
.text(function(d) { return d.name; })
.attr("x", function(d) { return (d.x); })
.attr("y", function(d) { return (d.y); })
.attr("fill", "black");
// Size of each label
var index = 0;
labels.each(function() {
label_array[index].width = this.getBBox().width;
label_array[index].height = this.getBBox().height;
index += 1;
});
// Draw data points
circ = svg.selectAll(".circ")
.data(label_array)
.enter().append("circle")
.attr("class", ".circ")
.attr("r", 20.0)
.attr("cx", function(d) { return (d.x); })
.attr("cy", function(d) { return (d.y - offset); })
.style("fill", 'red')
.attr('opacity',0.0);
// Draw links
links = svg.selectAll(".link")
.data(label_array)
.enter()
.append("line")
.attr("class", "link")
.attr("x1", function(d) { return (d.x); })
.attr("y1", function(d) { return (d.y); })
.attr("x2", function(d) { return (d.x); })
.attr("y2", function(d) { return (d.y); })
.attr("stroke-width", 0.6)
.attr("stroke", "gray");
};
// Start button function
d3.select("#start").on("click",function() {
// Get user-defined parameters
var nlabels = document.getElementById('n_labels').value;
var is_uniform = document.getElementById('uniform').checked;
var nsweeps = document.getElementById('n_sweeps').value;
// Randomly distribute data
randomize(nlabels, is_uniform);
// Setup labels
var sim_ann = d3.labeler()
.label(label_array)
.anchor(anchor_array)
.width(width)
.height(height)
.start(nsweeps);
// Redraw
redrawLabels();
});
// Default starting parameters
// 50 labels, uniformly distributed in x, y
randomize(50, true);
var sim_ann = d3.labeler()
.label(label_array)
.anchor(anchor_array)
.width(width)
.height(height)
sim_ann.start(1000);
redrawLabels();
</script>