forked from GiovineItalia/Gadfly.jl
/
gadfly.js
664 lines (528 loc) · 20.6 KB
/
gadfly.js
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
// Convert an offset in screen units (pixels) to client units (millimeters)
var client_offset = function(fig, x, y) {
var client_box = fig.node.getBoundingClientRect();
x = x * fig.node.viewBox.baseVal.width / client_box.width;
y = y * fig.node.viewBox.baseVal.height / client_box.height;
return [x, y]
};
// Get an x/y coordinate value in pixels
var xPX = function(fig, x) {
var client_box = fig.node.getBoundingClientRect();
return x * fig.node.viewBox.baseVal.width / client_box.width;
};
var yPX = function(fig, y) {
var client_box = fig.node.getBoundingClientRect();
return y * fig.node.viewBox.baseVal.height / client_box.height;
};
Snap.plugin(function (Snap, Element, Paper, global) {
// Traverse upwards from a snap element to find and return the first
// note with the "plotroot" class.
Element.prototype.plotroot = function () {
var element = this;
while (!element.hasClass("plotroot") && element.parent() != null) {
element = element.parent();
}
return element;
};
Element.prototype.plotbounds = function () {
var root = this.plotroot()
var bbox = root.select(".guide.background").node.getBBox();
return {
x0: bbox.x,
x1: bbox.x + bbox.width,
y0: bbox.y,
y1: bbox.y + bbox.height
};
};
Element.prototype.plotcenter = function () {
var root = this.plotroot()
var bbox = root.select(".guide.background").node.getBBox();
return {
x: bbox.x + bbox.width / 2,
y: bbox.y + bbox.height / 2
};
};
// Emulate IE style mouseenter/mouseleave events, since Microsoft always
// does everything right.
// See: http://www.dynamic-tools.net/toolbox/isMouseLeaveOrEnter/
var events = ["mouseenter", "mouseleave"];
for (i in events) {
(function (event_name) {
var event_name = events[i];
Element.prototype[event_name] = function (fn, scope) {
if (Snap.is(fn, "function")) {
var fn2 = function (event) {
if (event.type != "mouseover" && event.type != "mouseout") {
return;
}
var reltg = event.relatedTarget ? event.relatedTarget :
event.type == "mouseout" ? event.toElement : event.fromElement;
while (reltg && reltg != this.node) reltg = reltg.parentNode;
if (reltg != this.node) {
return fn.apply(this, event);
}
};
if (event_name == "mouseenter") {
this.mouseover(fn2, scope);
} else {
this.mouseout(fn2, scope);
}
}
return this;
};
})(events[i]);
}
// Snap's attr function can be too slow for things like panning/zooming.
// This is a function to directly update element attributes without going
// through eve.
Element.prototype.attribute = function(key, val) {
if (val === undefined) {
return this.node.getAttribute(key, val);
} else {
return this.node.setAttribute(key, val);
}
};
});
// When the plot is moused over, emphasize the grid lines.
var plot_mouseover = function(event) {
var root = this.plotroot();
var xgridlines = root.select(".xgridlines"),
ygridlines = root.select(".ygridlines");
xgridlines.data("unfocused_strokedash",
xgridlines.attr("stroke-dasharray"))
ygridlines.data("unfocused_strokedash",
ygridlines.attr("stroke-dasharray"))
// emphasize grid lines
var destcolor = root.data("focused_xgrid_color");
xgridlines.attr("stroke-dasharray", "none")
.selectAll("path")
.animate({stroke: destcolor}, 250);
destcolor = root.data("focused_ygrid_color");
ygridlines.attr("stroke-dasharray", "none")
.selectAll("path")
.animate({stroke: destcolor}, 250);
// reveal zoom slider
root.select(".zoomslider")
.animate({opacity: 1.0}, 250);
};
// Unemphasize grid lines on mouse out.
var plot_mouseout = function(event) {
var root = this.plotroot();
var xgridlines = root.select(".xgridlines"),
ygridlines = root.select(".ygridlines");
var destcolor = root.data("unfocused_xgrid_color");
xgridlines.attr("stroke-dasharray", xgridlines.data("unfocused_strokedash"))
.selectAll("path")
.animate({stroke: destcolor}, 250);
destcolor = root.data("unfocused_ygrid_color");
ygridlines.attr("stroke-dasharray", ygridlines.data("unfocused_strokedash"))
.selectAll("path")
.animate({stroke: destcolor}, 250);
// hide zoom slider
root.select(".zoomslider")
.animate({opacity: 0.0}, 250);
};
var set_geometry_transform = function(root, tx, ty, scale) {
var xscalable = root.hasClass("xscalable"),
yscalable = root.hasClass("yscalable");
var old_scale = root.data("scale");
var xscale = xscalable ? scale : 1.0,
yscale = yscalable ? scale : 1.0;
tx = xscalable ? tx : 0.0;
ty = yscalable ? ty : 0.0;
var t = new Snap.Matrix().translate(tx, ty).scale(xscale, yscale);
root.selectAll(".geometry")
.forEach(function (element, i) {
element.transform(t);
});
bounds = root.plotbounds();
if (yscalable) {
var xfixed_t = new Snap.Matrix().translate(0, ty).scale(1.0, yscale);
root.selectAll(".xfixed")
.forEach(function (element, i) {
element.transform(xfixed_t);
});
root.select(".ylabels")
.transform(xfixed_t)
.selectAll("text")
.forEach(function (element, i) {
if (element.attribute("gadfly:inscale") == "true") {
var cx = element.asPX("x"),
cy = element.asPX("y");
var st = element.data("static_transform");
unscale_t = new Snap.Matrix();
unscale_t.scale(1, 1/scale, cx, cy).add(st);
element.transform(unscale_t);
var y = cy * scale + ty;
element.attr("visibility",
bounds.y0 <= y && y <= bounds.y1 ? "visible" : "hidden");
}
});
}
if (xscalable) {
var yfixed_t = new Snap.Matrix().translate(tx, 0).scale(xscale, 1.0);
var xtrans = new Snap.Matrix().translate(tx, 0);
root.selectAll(".yfixed")
.forEach(function (element, i) {
element.transform(yfixed_t);
});
root.select(".xlabels")
.transform(yfixed_t)
.selectAll("text")
.forEach(function (element, i) {
if (element.attribute("gadfly:inscale") == "true") {
var cx = element.asPX("x"),
cy = element.asPX("y");
var st = element.data("static_transform");
unscale_t = new Snap.Matrix();
unscale_t.scale(1/scale, 1, cx, cy).add(st);
element.transform(unscale_t);
var x = cx * scale + tx;
element.attr("visibility",
bounds.x0 <= x && x <= bounds.x1 ? "visible" : "hidden");
}
});
}
// we must unscale anything that is scale invariance: widths, raiduses, etc.
var size_attribs = ["font-size"];
var unscaled_selection = ".geometry, .geometry > *";
if (xscalable) {
size_attribs.push("rx");
unscaled_selection += ", .xgridlines";
}
if (yscalable) {
size_attribs.push("ry");
unscaled_selection += ", .ygridlines";
}
root.selectAll(unscaled_selection)
.forEach(function (element, i) {
// circle need special help
if (element.node.nodeName == "circle") {
var cx = element.asPX("cx"),
cy = element.asPX("cy");
unscale_t = new Snap.Matrix().scale(1/xscale, 1/yscale,
cx, cy);
element.transform(unscale_t);
return;
}
for (i in size_attribs) {
var key = size_attribs[i];
var val = parseFloat(element.attribute(key));
if (val !== undefined && val != 0 && !isNaN(val)) {
element.attribute(key, val * old_scale / scale);
}
}
});
};
// Find the most appropriate tick scale and update label visibility.
var update_tickscale = function(root, scale, axis) {
if (!root.hasClass(axis + "scalable")) return;
var tickscales = root.data(axis + "tickscales");
var best_tickscale = 1.0;
var best_tickscale_dist = Infinity;
for (tickscale in tickscales) {
var dist = Math.abs(Math.log(tickscale) - Math.log(scale));
if (dist < best_tickscale_dist) {
best_tickscale_dist = dist;
best_tickscale = tickscale;
}
}
if (best_tickscale != root.data(axis + "tickscale")) {
root.data(axis + "tickscale", best_tickscale);
var mark_inscale_gridlines = function (element, i) {
var inscale = element.attr("gadfly:scale") == best_tickscale;
element.attr("gadfly:inscale", inscale);
element.attr("visibility", inscale ? "visible" : "hidden");
};
var mark_inscale_labels = function (element, i) {
var inscale = element.attr("gadfly:scale") == best_tickscale;
element.attr("gadfly:inscale", inscale);
element.attr("visibility", inscale ? "visible" : "hidden");
};
root.select("." + axis + "gridlines").selectAll("path").forEach(mark_inscale_gridlines);
root.select("." + axis + "labels").selectAll("text").forEach(mark_inscale_labels);
}
};
var set_plot_pan_zoom = function(root, tx, ty, scale) {
var old_scale = root.data("scale");
var bounds = root.plotbounds();
var width = bounds.x1 - bounds.x0,
height = bounds.y1 - bounds.y0;
// compute the viewport derived from tx, ty, and scale
var x_min = -width * scale - (scale * width - width),
x_max = width * scale,
y_min = -height * scale - (scale * height - height),
y_max = height * scale;
var x0 = bounds.x0 - scale * bounds.x0,
y0 = bounds.y0 - scale * bounds.y0;
var tx = Math.max(Math.min(tx - x0, x_max), x_min),
ty = Math.max(Math.min(ty - y0, y_max), y_min);
tx += x0;
ty += y0;
// when the scale change, we may need to alter which set of
// ticks is being displayed
if (scale != old_scale) {
update_tickscale(root, scale, "x");
update_tickscale(root, scale, "y");
}
set_geometry_transform(root, tx, ty, scale);
root.data("scale", scale);
root.data("tx", tx);
root.data("ty", ty);
};
var scale_centered_translation = function(root, scale) {
var bounds = root.plotbounds();
var width = bounds.x1 - bounds.x0,
height = bounds.y1 - bounds.y0;
var tx0 = root.data("tx"),
ty0 = root.data("ty");
var scale0 = root.data("scale");
// how off from center the current view is
var xoff = tx0 - (bounds.x0 * (1 - scale0) + (width * (1 - scale0)) / 2),
yoff = ty0 - (bounds.y0 * (1 - scale0) + (height * (1 - scale0)) / 2);
// rescale offsets
xoff = xoff * scale / scale0;
yoff = yoff * scale / scale0;
// adjust for the panel position being scaled
var x_edge_adjust = bounds.x0 * (1 - scale),
y_edge_adjust = bounds.y0 * (1 - scale);
return {
x: xoff + x_edge_adjust + (width - width * scale) / 2,
y: yoff + y_edge_adjust + (height - height * scale) / 2
};
};
// Initialize data for panning zooming if it isn't already.
var init_pan_zoom = function(root) {
if (root.data("zoompan-ready")) {
return;
}
// The non-scaling-stroke trick. Rather than try to correct for the
// stroke-width when zooming, we force it to a fixed value.
px_per_mm = root.parent().node.getCTM().a;
root.selectAll("path")
.forEach(function (element, i) {
sw = element.asPX("stroke-width") * px_per_mm;
if (sw > 0) {
element.attr("stroke-width", sw);
element.attr("vector-effect", "non-scaling-stroke");
}
});
// Store ticks labels original tranformation
root.selectAll(".xlabels > text, .ylabels > text")
.forEach(function (element, i) {
var lm = element.transform().localMatrix;
element.data("static_transform",
new Snap.Matrix(lm.a, lm.b, lm.c, lm.d, lm.e, lm.f));
});
if (root.data("tx") === undefined) root.data("tx", 0);
if (root.data("ty") === undefined) root.data("ty", 0);
if (root.data("scale") === undefined) root.data("scale", 1.0);
if (root.data("xtickscales") === undefined) {
// index all the tick scales that are listed
var xtickscales = {};
var ytickscales = {};
var add_x_tick_scales = function (element, i) {
xtickscales[element.attr("gadfly:scale")] = true;
};
var add_y_tick_scales = function (element, i) {
ytickscales[element.attr("gadfly:scale")] = true;
};
root.select(".xgridlines").selectAll("path").forEach(add_x_tick_scales);
root.select(".ygridlines").selectAll("path").forEach(add_y_tick_scales);
root.select(".xlabels").selectAll("text").forEach(add_x_tick_scales);
root.select(".ylabels").selectAll("text").forEach(add_y_tick_scales);
root.data("xtickscales", xtickscales);
root.data("ytickscales", ytickscales);
root.data("xtickscale", 1.0);
}
var min_scale = 1.0, max_scale = 1.0;
for (scale in xtickscales) {
min_scale = Math.min(min_scale, scale);
max_scale = Math.max(max_scale, scale);
}
for (scale in ytickscales) {
min_scale = Math.min(min_scale, scale);
max_scale = Math.max(max_scale, scale);
}
root.data("min_scale", min_scale);
root.data("max_scale", max_scale);
// store the original positions of labels
root.select(".xlabels")
.selectAll("text")
.forEach(function (element, i) {
element.data("x", element.asPX("x"));
});
root.select(".ylabels")
.selectAll("text")
.forEach(function (element, i) {
element.data("y", element.asPX("y"));
});
// mark grid lines and ticks as in or out of scale.
var mark_inscale = function (element, i) {
element.attr("gadfly:inscale", element.attr("gadfly:scale") == 1.0);
};
root.select(".xgridlines").selectAll("path").forEach(mark_inscale);
root.select(".ygridlines").selectAll("path").forEach(mark_inscale);
root.select(".xlabels").selectAll("text").forEach(mark_inscale);
root.select(".ylabels").selectAll("text").forEach(mark_inscale);
// figure out the upper ond lower bounds on panning using the maximum
// and minum grid lines
var bounds = root.plotbounds();
var pan_bounds = {
x0: 0.0,
y0: 0.0,
x1: 0.0,
y1: 0.0
};
root.select(".xgridlines")
.selectAll("path")
.forEach(function (element, i) {
if (element.attr("gadfly:inscale") == "true") {
var bbox = element.node.getBBox();
if (bounds.x1 - bbox.x < pan_bounds.x0) {
pan_bounds.x0 = bounds.x1 - bbox.x;
}
if (bounds.x0 - bbox.x > pan_bounds.x1) {
pan_bounds.x1 = bounds.x0 - bbox.x;
}
}
});
root.select(".ygridlines")
.selectAll("path")
.forEach(function (element, i) {
if (element.attr("gadfly:inscale") == "true") {
var bbox = element.node.getBBox();
if (bounds.y1 - bbox.y < pan_bounds.y0) {
pan_bounds.y0 = bounds.y1 - bbox.y;
}
if (bounds.y0 - bbox.y > pan_bounds.y1) {
pan_bounds.y1 = bounds.y0 - bbox.y;
}
}
});
// nudge these values a little
pan_bounds.x0 -= 5;
pan_bounds.x1 += 5;
pan_bounds.y0 -= 5;
pan_bounds.y1 += 5;
root.data("pan_bounds", pan_bounds);
// Set all grid lines at scale 1.0 to visible. Out of bounds lines
// will be clipped.
root.select(".xgridlines")
.selectAll("path")
.forEach(function (element, i) {
if (element.attr("gadfly:inscale") == "true") {
element.attr("visibility", "visible");
}
});
root.select(".ygridlines")
.selectAll("path")
.forEach(function (element, i) {
if (element.attr("gadfly:inscale") == "true") {
element.attr("visibility", "visible");
}
});
root.data("zoompan-ready", true)
};
// Panning
var guide_background_drag_onmove = function(dx, dy, x, y, event) {
var root = this.plotroot();
// TODO:
// This has problems on Firefox. Here's what I think is happening: firefox
// computes a bounding box for everything, including the invisible shit,
// which throws off the 'client_offset' calculation.'
var dxdy = client_offset(fig, dx, dy);
dx = dxdy[0];
dy = dxdy[1];
var tx0 = root.data("tx"),
ty0 = root.data("ty");
var dx0 = root.data("dx"),
dy0 = root.data("dy");
root.data("dx", dx);
root.data("dy", dy);
dx = dx - dx0;
dy = dy - dy0;
var tx = tx0 + dx,
ty = ty0 + dy;
set_plot_pan_zoom(root, tx, ty, root.data("scale"));
};
var guide_background_drag_onstart = function(x, y, event) {
var root = this.plotroot();
root.data("dx", 0);
root.data("dy", 0);
init_pan_zoom(root);
};
var guide_background_drag_onend = function(event) {
var root = this.plotroot();
};
var zoomslider_button_mouseover = function(event) {
this.select(".button_logo")
.animate({fill: this.data("mouseover_color")}, 100);
};
var zoomslider_button_mouseout = function(event) {
this.select(".button_logo")
.animate({fill: this.data("mouseout_color")}, 100);
};
var zoomslider_zoomout_click = function(event) {
// TODO
};
var zoomslider_zoomin_click = function(event) {
// TODO
};
var zoomslider_track_click = function(event) {
// TODO
};
var zoomslider_thumb_mousedown = function(event) {
this.animate({fill: this.data("mouseover_color")}, 100);
};
var zoomslider_thumb_mouseup = function(event) {
this.animate({fill: this.data("mouseout_color")}, 100);
};
// compute the position in [0, 1] of the zoom slider thumb from the current scale
var slider_position_from_scale = function(scale, min_scale, max_scale) {
if (scale >= 1.0) {
return 0.5 + 0.5 * (Math.log(scale) / Math.log(max_scale));
}
else {
return 0.5 * (Math.log(scale) - Math.log(min_scale)) / (0 - Math.log(min_scale));
}
}
var zoomslider_thumb_dragmove = function(dx, dy, x, y) {
var root = this.plotroot();
var min_pos = this.data("min_pos"),
max_pos = this.data("max_pos"),
min_scale = root.data("min_scale"),
max_scale = root.data("max_scale"),
old_scale = root.data("old_scale");
var dxdy = client_offset(fig, dx, dy);
dx = dxdy[0];
dy = dxdy[1];
var xmid = (min_pos + max_pos) / 2;
var xpos = slider_position_from_scale(old_scale, min_scale, max_scale) +
dx / (max_pos - min_pos);
// compute the new scale
var new_scale;
if (xpos >= 0.5) {
new_scale = Math.exp(2.0 * (xpos - 0.5) * Math.log(max_scale));
}
else {
new_scale = Math.exp(2.0 * xpos * (0 - Math.log(min_scale)) +
Math.log(min_scale));
}
new_scale = Math.min(max_scale, Math.max(min_scale, new_scale));
var trans = scale_centered_translation(root, new_scale);
set_plot_pan_zoom(root, trans.x, trans.y, new_scale);
// TODO: this will have to be move to its own function
this.transform(new Snap.Matrix().translate(
Math.max(min_pos, Math.min(
max_pos, min_pos + (max_pos - min_pos) * xpos)) - xmid, 0));
};
var zoomslider_thumb_dragstart = function(event) {
var root = this.plotroot();
init_pan_zoom(root);
// keep track of what the scale was when we started dragging
root.data("old_scale", root.data("scale"));
};
var zoomslider_thumb_dragend = function(event) {
};
//@ sourceURL=gadfly.js