| 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 |
const IS_CHROMIUM = navigator.userAgent.includes(' Chrome/'); |
| 25 |
|
| 26 |
export class PanZoom extends interaction.Interaction { |
| 27 |
render() { |
| 28 |
super.render(); |
| 29 |
const that = this; |
| 30 |
// chrome bug that requires a listener on the parent svg node |
| 31 |
// https://github.com/d3/d3-zoom/issues/231#issuecomment-802713799 |
| 32 |
this.parent.svg.node().addEventListener(`wheel`, nop, { passive: false }); |
| 33 |
this.d3el |
| 34 |
.style('cursor', 'move') |
| 35 |
.call( |
| 36 |
d3 |
| 37 |
.drag() |
| 38 |
.on('start', () => { |
| 39 |
that.mousedown(); |
| 40 |
}) |
| 41 |
.on('drag', () => { |
| 42 |
that.mousemove(); |
| 43 |
}) |
| 44 |
.on('end', () => { |
| 45 |
that.mouseup(); |
| 46 |
}) |
| 47 |
) |
| 48 |
.on('wheel', () => { |
| 49 |
that.mousewheel(); |
| 50 |
}); |
| 51 |
this.active = false; |
| 52 |
|
| 53 |
this.update_scales(); |
| 54 |
this.listenTo(this.model, 'change:scales', this.update_scales); |
| 55 |
|
| 56 |
this.set_ranges(); |
| 57 |
this.listenTo(this.parent, 'margin_updated', this.set_ranges); |
| 58 |
} |
| 59 |
|
| 60 |
remove() { |
| 61 |
this.parent.svg.node().removeEventListener(`wheel`, nop); |
| 62 |
super.remove(); |
| 63 |
} |
| 64 |
|
| 65 |
update_scales() { |
| 66 |
const scales = this.model.get('scales'); |
| 67 |
const that = this; |
| 68 |
this.scale_promises = widgets.resolvePromisesDict({ |
| 69 |
x: Promise.all( |
| 70 |
(scales.x || []).map((model: widgets.WidgetModel) => { |
| 71 |
return that.create_child_view(model); |
| 72 |
}) |
| 73 |
), |
| 74 |
y: Promise.all( |
| 75 |
(scales.y || []).map((model: widgets.WidgetModel) => { |
| 76 |
return that.create_child_view(model); |
| 77 |
}) |
| 78 |
), |
| 79 |
}); |
| 80 |
widgets |
| 81 |
.resolvePromisesDict(this.scale_promises) |
| 82 |
.then(_.bind(this.set_ranges, this)); |
| 83 |
} |
| 84 |
|
| 85 |
set_ranges() { |
| 86 |
const that = this; |
| 87 |
let i; |
| 88 |
this.scale_promises.then((scale_views) => { |
| 89 |
const xscale_views = scale_views.x; |
| 90 |
for (i = 0; i < xscale_views.length; i++) { |
| 91 |
xscale_views[i].set_range( |
| 92 |
that.parent.padded_range('x', xscale_views[i].model) |
| 93 |
); |
| 94 |
} |
| 95 |
const yscale_views = scale_views.y; |
| 96 |
for (i = 0; i < yscale_views.length; i++) { |
| 97 |
yscale_views[i].set_range( |
| 98 |
that.parent.padded_range('y', yscale_views[i].model) |
| 99 |
); |
| 100 |
} |
| 101 |
}); |
| 102 |
} |
| 103 |
|
| 104 |
hasRotatedParent(element) { |
| 105 |
const parent = element.parentNode; |
| 106 |
/* Base case: the element has no parent or the parent is the <html> element */ |
| 107 |
if (!parent || parent.tagName === 'HTML') { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
/* Check if the parent is rotated */ |
| 112 |
const computedStyle = window.getComputedStyle(parent); |
| 113 |
const transform = computedStyle.getPropertyValue('transform'); |
| 114 |
|
| 115 |
if (transform !== 'none' && !transform.startsWith('matrix(1, 0, 0, 1')) { |
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
/* Recursively check the parent's parent */ |
| 120 |
return this.hasRotatedParent(parent); |
| 121 |
} |
| 122 |
|
| 123 |
mousePos() { |
| 124 |
if (this.use_css_rotation_workaround) { |
| 125 |
const mouseEvent = window.event as MouseEvent; |
| 126 |
|
| 127 |
/* the mouse position is only correct within this.el */ |
| 128 |
if (mouseEvent.target === this.el) { |
| 129 |
this.last_mouse_pos = [mouseEvent.offsetX, mouseEvent.offsetY]; |
| 130 |
} |
| 131 |
return this.last_mouse_pos; |
| 132 |
} else { |
| 133 |
return d3.mouse(this.el); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
mousedown() { |
| 138 |
this.use_css_rotation_workaround = |
| 139 |
!IS_CHROMIUM && this.hasRotatedParent(this.el); |
| 140 |
this._mousedown(this.mousePos()); |
| 141 |
} |
| 142 |
|
| 143 |
_mousedown(mouse_pos) { |
| 144 |
const scales = this.model.get('scales'); |
| 145 |
this.active = true; |
| 146 |
this.d3el.style('cursor', 'move'); |
| 147 |
this.previous_pos = mouse_pos.slice(); |
| 148 |
// A copy of the original domains is required to avoid additional |
| 149 |
// drift when Paning. |
| 150 |
this.domains_in_order = { |
| 151 |
x: (scales.x || []).map((s) => { |
| 152 |
return s.get_domain_slice_in_order(); |
| 153 |
}), |
| 154 |
y: (scales.y || []).map((s) => { |
| 155 |
return s.get_domain_slice_in_order(); |
| 156 |
}), |
| 157 |
}; |
| 158 |
} |
| 159 |
|
| 160 |
mouseup() { |
| 161 |
this.active = false; |
| 162 |
} |
| 163 |
|
| 164 |
mousemove() { |
| 165 |
this._mousemove(this.mousePos()); |
| 166 |
} |
| 167 |
|
| 168 |
_mousemove(mouse_pos) { |
| 169 |
if (this.active && this.model.get('allow_pan')) { |
| 170 |
// If memory is set to true, intermediate positions between the |
| 171 |
// last position of the mouse and the current one will be |
| 172 |
// interpolated. |
| 173 |
if (this.previous_pos === undefined) { |
| 174 |
this.previous_pos = mouse_pos; |
| 175 |
} |
| 176 |
const mouse_delta = { |
| 177 |
x: mouse_pos[0] - this.previous_pos[0], |
| 178 |
y: mouse_pos[1] - this.previous_pos[1], |
| 179 |
}; |
| 180 |
return this.scale_promises.then((scale_views) => { |
| 181 |
['x', 'y'].forEach((dimension) => { |
| 182 |
scale_views[dimension].forEach((view, index) => { |
| 183 |
if (view.scale.invert) { |
| 184 |
// Categorical scales don't have an inversion. |
| 185 |
const scale = view.scale |
| 186 |
.copy() |
| 187 |
.domain(this.domains_in_order[dimension][index]); |
| 188 |
// convert the initial domain to pixel coordinates |
| 189 |
const pixel_min = scale( |
| 190 |
this.domains_in_order[dimension][index][0] |
| 191 |
); |
| 192 |
const pixel_max = scale( |
| 193 |
this.domains_in_order[dimension][index][1] |
| 194 |
); |
| 195 |
// shift pixels, and convert to new domain |
| 196 |
const domain_min = scale.invert( |
| 197 |
pixel_min - mouse_delta[dimension] |
| 198 |
); |
| 199 |
const domain_max = scale.invert( |
| 200 |
pixel_max - mouse_delta[dimension] |
| 201 |
); |
| 202 |
view.model.set('min', domain_min); |
| 203 |
view.model.set('max', domain_max); |
| 204 |
view.touch(); |
| 205 |
} |
| 206 |
}); |
| 207 |
}); |
| 208 |
}); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
mousewheel() { |
| 213 |
if (this.model.get('allow_zoom')) { |
| 214 |
const event = d3GetEvent(); |
| 215 |
event.preventDefault(); |
| 216 |
const delta = event.deltaY * -1; |
| 217 |
const mouse_pos = this.mousePos(); |
| 218 |
this._zoom(mouse_pos, delta); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
_zoom(mouse_pos, delta) { |
| 223 |
if (delta) { |
| 224 |
if (delta > 0) { |
| 225 |
this.d3el.style('cursor', 'zoom-in'); |
| 226 |
} else { |
| 227 |
this.d3el.style('cursor', 'zoom-out'); |
| 228 |
} |
| 229 |
const mouse = { x: mouse_pos[0], y: mouse_pos[1] }; |
| 230 |
const factor = Math.exp(-delta * 0.001); |
| 231 |
return this.scale_promises.then((scale_views) => { |
| 232 |
['x', 'y'].forEach((dimension) => { |
| 233 |
scale_views[dimension].forEach((view, index) => { |
| 234 |
if (view.scale.invert) { |
| 235 |
// Categorical scales don't have an inversion. |
| 236 |
const scale = view.scale; //.copy().domain(this.domains_in_order[dimension][index]); |
| 237 |
// convert the initial domain to pixel coordinates |
| 238 |
let [domain_min, domain_max] = |
| 239 |
view.model.get_domain_slice_in_order(); |
| 240 |
const pixel_min = scale(domain_min); |
| 241 |
const pixel_max = scale(domain_max); |
| 242 |
// take a weighted average between the mouse pos and the original pixel coordinate |
| 243 |
// and translate that back to the domain |
| 244 |
domain_min = scale.invert( |
| 245 |
(1 - factor) * mouse[dimension] + factor * pixel_min |
| 246 |
); |
| 247 |
domain_max = scale.invert( |
| 248 |
(1 - factor) * mouse[dimension] + factor * pixel_max |
| 249 |
); |
| 250 |
view.model.set('min', domain_min); |
| 251 |
view.model.set('max', domain_max); |
| 252 |
view.touch(); |
| 253 |
} |
| 254 |
}); |
| 255 |
}); |
| 256 |
}); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
active: boolean; |
| 261 |
scale_promises: any; |
| 262 |
previous_pos: [number, number]; |
| 263 |
domains_in_order: { x: any[]; y: any[] }; |
| 264 |
last_mouse_pos = null; |
| 265 |
use_css_rotation_workaround: boolean; |
| 266 |
} |