-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd3.behavior.js
144 lines (125 loc) · 3.24 KB
/
d3.behavior.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
(function(){d3.behavior = {};
// TODO unbind zoom behavior?
// TODO unbind listener?
d3.behavior.zoom = function() {
var x = 0,
y = 0,
z = 0,
listeners = [],
pan,
zoom;
function zoom() {
var container = this
.on("mousedown", mousedown)
.on("mousewheel", mousewheel)
.on("DOMMouseScroll", mousewheel)
.on("dblclick", mousewheel);
d3.select(window)
.on("mousemove", mousemove)
.on("mouseup", mouseup);
}
function mousedown(d, i) {
pan = {
x0: x - d3.event.clientX,
y0: y - d3.event.clientY,
target: this,
data: d,
index: i
};
d3.event.preventDefault();
window.focus(); // TODO focusableParent
}
function mousemove() {
zoom = null;
if (pan) {
x = d3.event.clientX + pan.x0;
y = d3.event.clientY + pan.y0;
dispatch.call(pan.target, pan.data, pan.index);
}
}
function mouseup() {
if (pan) {
mousemove();
pan = null;
}
}
// mousewheel events are totally broken!
// https://bugs.webkit.org/show_bug.cgi?id=40441
// not only that, but Chrome and Safari differ in re. to acceleration!
var outer = d3.select("body").append("div")
.style("visibility", "hidden")
.style("position", "absolute")
.style("top", "-3000px")
.style("height", 0)
.style("overflow-y", "scroll")
.append("div")
.style("height", "2000px")
.node().parentNode;
function mousewheel(d, i) {
var e = d3.event;
// initialize the mouse location for zooming (to avoid drift)
if (!zoom) {
var p = d3.svg.mouse(this.nearestViewportElement || this);
zoom = {
x0: x,
y0: y,
z0: z,
x1: x - p[0],
y1: y - p[1]
};
}
// adjust zoom level
if (e.type === "dblclick") {
z = e.shiftKey ? Math.ceil(z - 1) : Math.floor(z + 1);
} else {
var delta = e.wheelDelta || -e.detail;
if (delta) {
try {
outer.scrollTop = 1000;
outer.dispatchEvent(e);
delta = 1000 - outer.scrollTop;
} catch (error) {
// Derp! Hope for the best?
}
delta *= .005;
}
z += delta;
}
// adjust x and y to center around mouse location
var k = Math.pow(2, z - zoom.z0) - 1;
x = zoom.x0 + zoom.x1 * k;
y = zoom.y0 + zoom.y1 * k;
// dispatch redraw
dispatch.call(this, d, i);
}
function dispatch(d, i) {
var o = d3.event, // Events can be reentrant (e.g., focus).
k = Math.pow(2, z);
d3.event = {
scale: k,
translate: [x, y],
transform: function(sx, sy) {
if (sx) transform(sx, x);
if (sy) transform(sy, y);
}
};
function transform(scale, o) {
var domain = scale.__domain || (scale.__domain = scale.domain()),
range = scale.range().map(function(v) { return (v - o) / k; });
scale.domain(domain).domain(range.map(scale.invert));
}
try {
for (var j = 0, m = listeners.length; j < m; j++) {
listeners[j].call(this, d, i);
}
} finally {
d3.event = o;
}
}
zoom.on = function(type, listener) {
if (type == "zoom") listeners.push(listener);
return zoom;
};
return zoom;
};
})();