-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd3scatter.html
433 lines (370 loc) · 15.3 KB
/
d3scatter.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<!DOCTYPE html>
<!-- saved from url=(0073)http://examples.oreilly.com/0636920026938/chapter_09/20_axes_dynamic.html -->
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>D3: Transitioning points to randomized values, plus rescaled axes!</title>
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script>
<script type="text/javascript" src="WeaveExternalHelper.js"></script>
<style type="text/css">
circle {
fill: white;
fill-opacity: 0.0;
stroke-width: 1.25;
stroke: black;
}
circle.selected {
stroke-width: 2;
}
circle.unselected {
filter:url(#blur-effect-1);
opacity: 0.5;
}
circle.probed {
stroke: black;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
</style>
<style type="text/css"></style></head>
<body onload="weave_setup();">
<script type="text/javascript">
//Width and height
var dataset = [];
var w = 500;
var h = 300;
var padding = 30;
var toolPath;
//Create scale functions
var xScale = d3.scale.linear()
.domain([0, 1])
.range([padding, w - padding * 2]);
var yScale = d3.scale.linear()
.domain([0, 1])
.range([h - padding, padding]);
var rScale = d3.scale.linear()
.domain([0, 1])
.range([2, 10]);
var quadtree = null;
//var brush = d3.svg.brush().x(xScale).y(yScale).on("brush", brushed);
//Define X axis
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(5);
//Define Y axis
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(5);
var localKeys = [];
var localProbes = {};
var localSelection = {};
var localSubset = {};
//Create SVG element
var svg = d3.select("body").append("svg");
//Create element groups for the axes.
var yAxisElement = svg.append("g").attr("class", "y axis").call(yAxis);
var xAxisElement = svg.append("g").attr("class", "x axis").call(xAxis);
update_layout();
function update_layout()
{
//Modify SVG
svg.attr("width", w).attr("height", h);
//Create X axis
xAxisElement.attr("transform", "translate(0," + (h - padding) + ")");
//Create Y axis
yAxisElement.attr("transform", "translate(" + padding + ",0)");
// Update xScale and yScale according to changes in padding
xScale.range([padding, w - padding * 2]);
yScale.range([h - padding, padding]);
plot();
}
function get_windowsize()
{
/* Ganked from http://stackoverflow.com/questions/3437786/how-to-get-web-page-size-browser-window-size-screen-size-in-a-cross-browser-wa */
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
x = w.innerWidth || e.clientWidth || g.clientWidth,
y = w.innerHeight|| e.clientHeight|| g.clientHeight;
return {x: x, y: y};
}
function keyfunc(datum)
{
return datum.key;
}
function weave_to_d3(raw_data, column_names)
{
raw_data_len = raw_data[0].length;
var new_dataset = new Array(raw_data_len);
for (var raw_idx = 0; raw_idx < raw_data_len; raw_idx++)
{
var new_datum = {};
new_datum.key = raw_data[0][raw_idx];
for (var col_idx = 0; col_idx < column_names.length; col_idx++)
{
new_datum[column_names[col_idx]] = raw_data[col_idx+1][raw_idx];
}
new_dataset[raw_idx] = new_datum;
}
return new_dataset;
}
function data_updated()
{
var column_names = ['x', 'y', 'size', 'color'];
var raw_data = toolPath.retrieveColumns(column_names);
localKeys = raw_data[0];
dataset = weave_to_d3(raw_data, column_names);
plot();
return;
}
function in_set(d,s)
{
if (d == undefined) return null;
if (s[d.key] == undefined) return false;
return true;
}
function set_isempty(s) {
return Object.keys(s).length == 0;
}
function set_negate(a,b) {
var result = {};
for (var k in a)
{
if (b[k] == undefined)
result[k] = a[k];
}
return result;
}
function set_from_array(a)
{
var set = {};
for (var k in a)
{
set[a[k]] = true;
}
return set;
}
function in_probeset(d,i,a) {return in_set(d, localProbes);}
function in_selectionset(d,i,a) {return in_set(d, localSelection);}
function in_subset(d,i,a) { return in_set(d, localSubset);}
var debug_tmp;
function plot()
{
//dataset.forEach(function (d) {d.hidden = !in_subset() })
dataset.forEach(function(d) {
d.hidden = !in_subset(d);
d.selected = in_selectionset(d);
d.unselected = !set_isempty(localSelection) && !d.selected;
});
if (!set_isempty(localSubset))
var working_dataset = dataset.filter(function(d) {return !d.hidden;});
else
var working_dataset = dataset;
//Update scale domains.
if (xScaleFunc == d3.scale.log) min = 1; else min = 0;
xScale.domain([min, d3.max(working_dataset, function(d) { return d.x; })]);
if (yScaleFunc == d3.scale.log) min = 1; else min = 0;
yScale.domain([min, d3.max(working_dataset, function(d) { return d.y; })]);
rScale.domain([min, d3.max(working_dataset, function(d) { return d.size; })]);
// Add any new records.
svg.selectAll("circle")
.data(dataset, keyfunc)
.enter()
.append("circle")
.classed("selected", function(d) { return d.selected; })
.classed("unselected", function(d) { return d.unselected; })
.attr("cx", function(d) {
if (d.x != NaN && d.x != null)
return xScale(d.x);
else
return xScale(d.x);
})
.attr("cy", function(d) {
if (d.y != NaN && d.y != null)
return yScale(d.y);
else
return yScale(d.y);
})
//.attr("opacity", function (d) { return d.hidden ? 0 : 1.0; })
.attr("r", function(d) {
if (d.size != NaN && d.size != null)
return rScale(d.size);
else
return rScale(0);
})
.on("mouseover", probed)
.on("mouseout", unprobed);
//Update all circles
svg.selectAll("circle")
.data(dataset, keyfunc)
.classed("selected", function(d) { return d.selected; })
.classed("unselected", function(d) { return d.unselected; })
.transition()
.duration(1000)
.attr("cx", function(d) {
if (d.x != NaN && d.x != null)
return xScale(d.x);
else
return xScale(d.x);
})
.attr("cy", function(d) {
if (d.y != NaN && d.y != null)
return yScale(d.y);
else
return yScale(d.y);
})
//.attr("opacity", function (d) { return d.hidden ? 0 : 1.0; })
.attr("r", function(d) {
if (d.size != NaN && d.size != null)
return rScale(d.size);
else
return rScale(0);
});
// Remove missing records.
svg.selectAll("circle")
.data(dataset, keyfunc)
.exit()
.transition()
.duration(250)
.attr("opacity", 0)
.remove();
//Update X axis
svg.select(".x.axis")
.transition()
.duration(250)
.call(xAxis);
//Update Y axis
svg.select(".y.axis")
.transition()
.duration(250)
.call(yAxis);
};
var toolPath;
var SCALE_TYPES = {
"Linear": d3.scale.linear,
"Logarithmic": d3.scale.log
};
var xScaleFunc = d3.scale.linear;
var yScaleFunc = d3.scale.linear;
function scale_string_to_func(str)
{
SCALE_TYPES[str] || d3.scale.linear;
}
function scale_changed()
{
var xScaleStr = toolPath.getState('xscale_type');
var yScaleStr = toolPath.getState('yscale_type');
xScaleFunc = SCALE_TYPES[xScaleStr] || d3.scale.linear;
yScaleFunc = SCALE_TYPES[yScaleStr] || d3.scale.linear;
if (!SCALE_TYPES[xScaleStr]) toolPath.state('xscale_type', 'Linear');
if (!SCALE_TYPES[yScaleStr]) toolPath.state('yscale_type', 'Linear');
update_layout();
}
function size_changed( )
{
var tmp_size = toolPath.getState('base_size');
var tmp_max_size = toolPath.getState('max_size');
if (isNaN(tmp_size))
{
console.log("base_size invalid, setting to default");
toolPath.state('base_size', 5);
tmp_size = 5;
}
if (isNaN(tmp_max_size))
{
console.log("max_size invalid, setting to default");
toolPath.state('max_size', 5);
tmp_max_size = 5;
}
rScale.range([tmp_size, tmp_max_size]);
plot();
}
function selection_changed()
{
}
function probe_changed()
{
var remoteProbes = toolPath.probe_keyset.getKeys();
console.log(remoteProbes);
localProbes = set_from_array(remoteProbes);
console.log(localProbes);
plot();
}
function update_probes()
{
toolPath.probe_keyset.setKeys(Object.keys(localProbes));
}
function probed(d)
{
if (d.hidden) return;
localProbes = {};
localProbes[d.key] = true;
update_probes();
}
function unprobed(d)
{
if (d.hidden) return;
delete localProbes[d.key];
//localProbes = {};
update_probes();
}
function selection_changed()
{
localSelection = set_from_array(toolPath.selection_keyset.getKeys());
plot();
}
function subset_changed()
{
localSubset = set_from_array(toolPath.subset_filter.filterKeys(localKeys));
plot();
}
function weave_setup()
{
toolPath = weaveExternalInit();
toolPath.newProperty('x', 'DynamicColumn', data_updated)
.newProperty('y', 'DynamicColumn', data_updated)
.newProperty('color', 'DynamicColumn', data_updated)
.newProperty('size', 'DynamicColumn', data_updated)
.newProperty('xscale_type', 'LinkableString', scale_changed)
.newProperty('yscale_type', 'LinkableString', scale_changed)
.newProperty('base_size', 'LinkableNumber', size_changed)
.newProperty('max_size', 'LinkableNumber', size_changed);
toolPath.probe_keyset.addCallback(probe_changed, true);
toolPath.selection_keyset.addCallback(selection_changed, true);
toolPath.subset_filter.addCallback(subset_changed, true);
window.onunload = function () {
toolPath.removeCallback(probe_changed, true);
toolPath.removeCallback(selection_changed, true);
toolPath.removeCallback(subset_changed, true);
toolPath.removeCallback(data_updated, true);
toolPath.removeCallback(scale_changed, true);
toolPath.removeCallback(size_changed, true);
};
window.onresize();
};
window.onresize = function () {
var obj = get_windowsize();
w = obj.x;
h = obj.y;
update_layout();
}
</script>
<svg id="svg-effects">
<filter id="blur-effect-1">
<feGaussianBlur stdDeviation="1" />
</filter>
<filter id="blur-effect-2">
<feGaussianBlur stdDeviation="2" />
</filter>
</svg>
</body></html>