Debian Codegraph @

js/src/PanZoom.ts

raw
1 /* Copyright 2015 Bloomberg Finance L.P.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 import * as widgets from '@jupyter-widgets/base';
17 import * as d3 from 'd3';
18 import { d3GetEvent } from './utils';
19 import * as interaction from './Interaction';
20 import * as _ from 'underscore';
21
22 const nop = () => {};
23
24 export class PanZoom extends interaction.Interaction {
25 render() {
26 super.render();
27 const that = this;
28 // chrome bug that requires a listener on the parent svg node
29 // https://github.com/d3/d3-zoom/issues/231#issuecomment-802713799
30 this.parent.svg.node().addEventListener(`wheel`, nop, { passive: false });
31 this.d3el
32 .style('cursor', 'move')
33 .call(
34 d3
35 .drag()
36 .on('start', () => {
37 that.mousedown();
38 })
39 .on('drag', () => {
40 that.mousemove();
41 })
42 .on('end', () => {
43 that.mouseup();
44 })
45 )
46 .on('wheel', () => {
47 that.mousewheel();
48 });
49 this.active = false;
50
51 this.update_scales();
52 this.listenTo(this.model, 'change:scales', this.update_scales);
53
54 this.set_ranges();
55 this.listenTo(this.parent, 'margin_updated', this.set_ranges);
56 }
57
58 remove() {
59 this.parent.svg.node().removeEventListener(`wheel`, nop);
60 super.remove();
61 }
62
63 update_scales() {
64 const scales = this.model.get('scales');
65 const that = this;
66 this.scale_promises = widgets.resolvePromisesDict({
67 x: Promise.all(
68 (scales.x || []).map((model: widgets.WidgetModel) => {
69 return that.create_child_view(model);
70 })
71 ),
72 y: Promise.all(
73 (scales.y || []).map((model: widgets.WidgetModel) => {
74 return that.create_child_view(model);
75 })
76 ),
77 });
78 widgets
79 .resolvePromisesDict(this.scale_promises)
80 .then(_.bind(this.set_ranges, this));
81 }
82
83 set_ranges() {
84 const that = this;
85 let i;
86 this.scale_promises.then((scale_views) => {
87 const xscale_views = scale_views.x;
88 for (i = 0; i < xscale_views.length; i++) {
89 xscale_views[i].set_range(
90 that.parent.padded_range('x', xscale_views[i].model)
91 );
92 }
93 const yscale_views = scale_views.y;
94 for (i = 0; i < yscale_views.length; i++) {
95 yscale_views[i].set_range(
96 that.parent.padded_range('y', yscale_views[i].model)
97 );
98 }
99 });
100 }
101
102 mousedown() {
103 this._mousedown(d3.mouse(this.el));
104 }
105
106 _mousedown(mouse_pos) {
107 const scales = this.model.get('scales');
108 this.active = true;
109 this.d3el.style('cursor', 'move');
110 this.previous_pos = mouse_pos.slice();
111 // A copy of the original domains is required to avoid additional
112 // drift when Paning.
113 this.domains_in_order = {
114 x: (scales.x || []).map((s) => {
115 return s.get_domain_slice_in_order();
116 }),
117 y: (scales.y || []).map((s) => {
118 return s.get_domain_slice_in_order();
119 }),
120 };
121 }
122
123 mouseup() {
124 this.active = false;
125 }
126
127 mousemove() {
128 this._mousemove(d3.mouse(this.el));
129 }
130
131 _mousemove(mouse_pos) {
132 if (this.active && this.model.get('allow_pan')) {
133 // If memory is set to true, intermediate positions between the
134 // last position of the mouse and the current one will be
135 // interpolated.
136 if (this.previous_pos === undefined) {
137 this.previous_pos = mouse_pos;
138 }
139 const mouse_delta = {
140 x: mouse_pos[0] - this.previous_pos[0],
141 y: mouse_pos[1] - this.previous_pos[1],
142 };
143 return this.scale_promises.then((scale_views) => {
144 ['x', 'y'].forEach((dimension) => {
145 scale_views[dimension].forEach((view, index) => {
146 if (view.scale.invert) {
147 // Categorical scales don't have an inversion.
148 const scale = view.scale
149 .copy()
150 .domain(this.domains_in_order[dimension][index]);
151 // convert the initial domain to pixel coordinates
152 const pixel_min = scale(
153 this.domains_in_order[dimension][index][0]
154 );
155 const pixel_max = scale(
156 this.domains_in_order[dimension][index][1]
157 );
158 // shift pixels, and convert to new domain
159 const domain_min = scale.invert(
160 pixel_min - mouse_delta[dimension]
161 );
162 const domain_max = scale.invert(
163 pixel_max - mouse_delta[dimension]
164 );
165 view.model.set('min', domain_min);
166 view.model.set('max', domain_max);
167 view.touch();
168 }
169 });
170 });
171 });
172 }
173 }
174
175 mousewheel() {
176 if (this.model.get('allow_zoom')) {
177 const event = d3GetEvent();
178 event.preventDefault();
179 const delta = event.deltaY * -1;
180 const mouse_pos = d3.mouse(this.el);
181 this._zoom(mouse_pos, delta);
182 }
183 }
184
185 _zoom(mouse_pos, delta) {
186 if (delta) {
187 if (delta > 0) {
188 this.d3el.style('cursor', 'zoom-in');
189 } else {
190 this.d3el.style('cursor', 'zoom-out');
191 }
192 const mouse = { x: mouse_pos[0], y: mouse_pos[1] };
193 const factor = Math.exp(-delta * 0.001);
194 return this.scale_promises.then((scale_views) => {
195 ['x', 'y'].forEach((dimension) => {
196 scale_views[dimension].forEach((view, index) => {
197 if (view.scale.invert) {
198 // Categorical scales don't have an inversion.
199 const scale = view.scale; //.copy().domain(this.domains_in_order[dimension][index]);
200 // convert the initial domain to pixel coordinates
201 let [domain_min, domain_max] =
202 view.model.get_domain_slice_in_order();
203 const pixel_min = scale(domain_min);
204 const pixel_max = scale(domain_max);
205 // take a weighted average between the mouse pos and the original pixel coordinate
206 // and translate that back to the domain
207 domain_min = scale.invert(
208 (1 - factor) * mouse[dimension] + factor * pixel_min
209 );
210 domain_max = scale.invert(
211 (1 - factor) * mouse[dimension] + factor * pixel_max
212 );
213 view.model.set('min', domain_min);
214 view.model.set('max', domain_max);
215 view.touch();
216 }
217 });
218 });
219 });
220 }
221 }
222
223 active: boolean;
224 scale_promises: any;
225 previous_pos: [number, number];
226 domains_in_order: { x: any[]; y: any[] };
227 }