| 1 |
import * as widgets from '@jupyter-widgets/base'; |
| 2 |
import { expect } from 'chai'; |
| 3 |
import { DummyManager } from './dummy-manager'; |
| 4 |
import bqplot = require('..'); |
| 5 |
import { |
| 6 |
create_model_bqplot, |
| 7 |
create_figure_scatter, |
| 8 |
create_figure_bars, |
| 9 |
} from './widget-utils'; |
| 10 |
import * as d3Timer from 'd3-timer'; |
| 11 |
|
| 12 |
// text pixel coordinate |
| 13 |
const test_x = 200; |
| 14 |
const test_y = 250; |
| 15 |
const pixel_red = [255, 0, 0, 255]; |
| 16 |
const pixel_green = [0, 255, 0, 255]; |
| 17 |
const pixel_background = [255, 255, 255, 255]; |
| 18 |
|
| 19 |
describe('interacts >', () => { |
| 20 |
beforeEach(async function () { |
| 21 |
this.manager = new DummyManager({ bqplot: bqplot }); |
| 22 |
}); |
| 23 |
|
| 24 |
it('brush selector basics', async function () { |
| 25 |
const x = { |
| 26 |
dtype: 'float32', |
| 27 |
value: new DataView(new Float32Array([0, 1]).buffer), |
| 28 |
}; |
| 29 |
const y = { |
| 30 |
dtype: 'float32', |
| 31 |
value: new DataView(new Float32Array([2, 3]).buffer), |
| 32 |
}; |
| 33 |
const { figure, scatter } = await create_figure_scatter(this.manager, x, y); |
| 34 |
|
| 35 |
const brush_selector = await create_model_bqplot( |
| 36 |
this.manager, |
| 37 |
'BrushSelector', |
| 38 |
'brush_selector_1', |
| 39 |
{ |
| 40 |
x_scale: figure.model.get('scale_x').toJSON(), |
| 41 |
y_scale: figure.model.get('scale_y').toJSON(), |
| 42 |
marks: [scatter.model.toJSON()], |
| 43 |
color: '#0f0', // green |
| 44 |
} |
| 45 |
); |
| 46 |
brush_selector.set('selected_x', [0.5, 1.5]); |
| 47 |
brush_selector.set('selected_y', [2.5, 3.5]); |
| 48 |
let brush_selector_view = await figure.set_interaction(brush_selector); |
| 49 |
const indices = scatter.model.get('selected'); |
| 50 |
expect([...indices]).to.deep.equal([1]); |
| 51 |
expect(indices).to.be.an.instanceof(Uint32Array); |
| 52 |
|
| 53 |
brush_selector_view.d3el.select('.selection').attr('fill-opacity', 1.0); // make sure it's full green |
| 54 |
let brushColorData = await figure.getPixel(390, 10); // upper right corner |
| 55 |
expect([...brushColorData]).to.deep.equals(pixel_green); |
| 56 |
|
| 57 |
// removing the selector should not deselect |
| 58 |
figure.set_interaction(null); |
| 59 |
expect([...scatter.model.get('selected')]).to.deep.equal([1]); |
| 60 |
// changing a disconnected brush, should not change the selection |
| 61 |
brush_selector.set('selected_x', [-0.5, 0.5]); |
| 62 |
brush_selector.set('selected_y', [1.5, 2.5]); |
| 63 |
expect([...scatter.model.get('selected')]).to.deep.equal([1]); |
| 64 |
|
| 65 |
// we restore the brush |
| 66 |
brush_selector_view = await figure.set_interaction(brush_selector); |
| 67 |
brush_selector_view.d3el.select('.selection').attr('fill-opacity', 1.0); // make sure it's full green |
| 68 |
// and the selection should be updated |
| 69 |
expect([...scatter.model.get('selected')]).to.deep.equal([0]); |
| 70 |
brushColorData = await figure.getPixel(10, 490); // lower left corner |
| 71 |
expect([...brushColorData]).to.deep.equals(pixel_green); |
| 72 |
|
| 73 |
// we set the initial selection, and it should update the selection AND the DOM |
| 74 |
brush_selector.set('selected_x', [0.5, 1.5]); |
| 75 |
brush_selector.set('selected_y', [2.5, 3.5]); |
| 76 |
expect([...scatter.model.get('selected')]).to.deep.equal([1]); |
| 77 |
brushColorData = await figure.getPixel(390, 10); // upper right corner |
| 78 |
expect([...brushColorData]).to.deep.equals(pixel_green); |
| 79 |
}); |
| 80 |
|
| 81 |
it('brush interval selector basics', async function () { |
| 82 |
const x = { |
| 83 |
dtype: 'float32', |
| 84 |
value: new DataView(new Float32Array([0, 1]).buffer), |
| 85 |
}; |
| 86 |
const y = { |
| 87 |
dtype: 'float32', |
| 88 |
value: new DataView(new Float32Array([2, 3]).buffer), |
| 89 |
}; |
| 90 |
const { figure, scatter } = await create_figure_scatter(this.manager, x, y); |
| 91 |
|
| 92 |
const brush_interval_selector = await create_model_bqplot( |
| 93 |
this.manager, |
| 94 |
'BrushIntervalSelector', |
| 95 |
'brush_interval_selector_1', |
| 96 |
{ |
| 97 |
scale: figure.model.get('scale_y').toJSON(), |
| 98 |
orientation: 'vertical', |
| 99 |
marks: [scatter.model.toJSON()], |
| 100 |
color: '#0f0', // green |
| 101 |
} |
| 102 |
); |
| 103 |
let brush_interval_selector_view = await figure.set_interaction( |
| 104 |
brush_interval_selector |
| 105 |
); |
| 106 |
brush_interval_selector.set('selected', [2.5, 3.5]); |
| 107 |
expect([...scatter.model.get('selected')]).to.deep.equal([1]); |
| 108 |
brush_interval_selector.set('selected', [1.5, 2.5]); |
| 109 |
expect([...scatter.model.get('selected')]).to.deep.equal([0]); |
| 110 |
brush_interval_selector_view.relayout(); |
| 111 |
|
| 112 |
brush_interval_selector_view.d3el |
| 113 |
.select('.selection') |
| 114 |
.attr('fill-opacity', 1.0); // make sure it's full green |
| 115 |
let brushColorData = await figure.getPixel(test_x, 490); // bottom edge |
| 116 |
expect([...brushColorData]).to.deep.equals(pixel_green); |
| 117 |
|
| 118 |
figure.set_interaction(null); |
| 119 |
expect([...scatter.model.get('selected')]).to.deep.equal([0]); |
| 120 |
// changing a disconnected brush, should not change the selection |
| 121 |
brush_interval_selector.set('selected', [2.5, 3.5]); |
| 122 |
expect([...scatter.model.get('selected')]).to.deep.equal([0]); |
| 123 |
|
| 124 |
// we restore the brush |
| 125 |
brush_interval_selector_view = await figure.set_interaction( |
| 126 |
brush_interval_selector |
| 127 |
); |
| 128 |
brush_interval_selector_view.d3el |
| 129 |
.select('.selection') |
| 130 |
.attr('fill-opacity', 1.0); // make sure it's full green |
| 131 |
// and the selection should be updated |
| 132 |
brush_interval_selector_view.relayout(); |
| 133 |
expect([...scatter.model.get('selected')]).to.deep.equal([1]); |
| 134 |
brushColorData = await figure.getPixel(test_x, 10); // top edge |
| 135 |
expect([...brushColorData]).to.deep.equals(pixel_green); |
| 136 |
|
| 137 |
// we set the initial selection, and it should update the selection AND the DOM |
| 138 |
brush_interval_selector.set('selected', [1.5, 2.5]); |
| 139 |
expect([...scatter.model.get('selected')]).to.deep.equal([0]); |
| 140 |
brushColorData = await figure.getPixel(test_x, 490); // bottom edge |
| 141 |
expect([...brushColorData]).to.deep.equals(pixel_green); |
| 142 |
}); |
| 143 |
|
| 144 |
it('brush interval selector on bars', async function () { |
| 145 |
const x = { |
| 146 |
dtype: 'float32', |
| 147 |
value: new DataView(new Float32Array([0, 1]).buffer), |
| 148 |
}; |
| 149 |
const y = { |
| 150 |
dtype: 'float32', |
| 151 |
value: new DataView(new Float32Array([2, 3]).buffer), |
| 152 |
}; |
| 153 |
const { figure, bars } = await create_figure_bars(this.manager, x, y); |
| 154 |
|
| 155 |
const brush_interval_selector = await create_model_bqplot( |
| 156 |
this.manager, |
| 157 |
'BrushIntervalSelector', |
| 158 |
'brush_interval_selector_1', |
| 159 |
{ |
| 160 |
scale: figure.model.get('scale_x').toJSON(), |
| 161 |
orientation: 'horizontal', |
| 162 |
marks: [bars.model.toJSON()], |
| 163 |
} |
| 164 |
); |
| 165 |
const brush_interval_selector_view = await figure.set_interaction( |
| 166 |
brush_interval_selector |
| 167 |
); |
| 168 |
await brush_interval_selector_view.displayed; |
| 169 |
// all the events in figure are async, and we don't have access |
| 170 |
// to the promises, so we manually call these methods |
| 171 |
await brush_interval_selector_view.create_scales(); |
| 172 |
await brush_interval_selector_view.mark_views_promise; |
| 173 |
brush_interval_selector_view.relayout(); |
| 174 |
d3Timer.timerFlush(); // this makes sure the animations are all executed |
| 175 |
brush_interval_selector.set('selected', [0.9, 1.1]); |
| 176 |
expect([...bars.model.get('selected')]).to.deep.equal([1]); |
| 177 |
brush_interval_selector.set('selected', [-0.1, 1.1]); |
| 178 |
expect([...bars.model.get('selected')]).to.deep.equal([0, 1]); |
| 179 |
expect(bars.model.get('selected')).to.be.an.instanceof(Uint32Array); |
| 180 |
}); |
| 181 |
|
| 182 |
it('pan/zoom', async function () { |
| 183 |
const x = { |
| 184 |
dtype: 'float32', |
| 185 |
value: new DataView(new Float32Array([0.5]).buffer), |
| 186 |
}; |
| 187 |
const y = { |
| 188 |
dtype: 'float32', |
| 189 |
value: new DataView(new Float32Array([2.5]).buffer), |
| 190 |
}; |
| 191 |
const { figure, scatter } = await create_figure_scatter(this.manager, x, y); |
| 192 |
// scatter.model.set("default_size", 100*100); |
| 193 |
scatter.model.set('default_size', 100 * 100); |
| 194 |
scatter.model.set('colors', ['red']); |
| 195 |
scatter.d3el.selectAll('.object_grp').data(); |
| 196 |
|
| 197 |
const panzoom = await create_model_bqplot( |
| 198 |
this.manager, |
| 199 |
'PanZoom', |
| 200 |
'panzoom', |
| 201 |
{ |
| 202 |
scales: { |
| 203 |
x: [figure.model.get('scale_x').toJSON()], |
| 204 |
y: [figure.model.get('scale_y').toJSON()], |
| 205 |
}, |
| 206 |
} |
| 207 |
); |
| 208 |
const panzoom_view = await figure.set_interaction(panzoom); |
| 209 |
await panzoom_view.displayed; |
| 210 |
await widgets.resolvePromisesDict(panzoom_view.scale_promises); |
| 211 |
|
| 212 |
await figure.relayout(); |
| 213 |
// we want two cycles to make sure relayout is done (it is using requestAnimationFrame) |
| 214 |
await new Promise((resolve) => window.requestAnimationFrame(resolve)); |
| 215 |
await new Promise((resolve) => window.requestAnimationFrame(resolve)); |
| 216 |
|
| 217 |
// first check if we have a red pixel in the center |
| 218 |
let canvas = await figure.get_rendered_canvas(); |
| 219 |
let context = canvas.getContext('2d'); |
| 220 |
let pixel = context.getImageData( |
| 221 |
test_x * window.devicePixelRatio, |
| 222 |
test_y * window.devicePixelRatio, |
| 223 |
1, |
| 224 |
1 |
| 225 |
); |
| 226 |
expect(Array.prototype.slice.call(pixel.data)).to.deep.equals(pixel_red); |
| 227 |
|
| 228 |
// we pan to the right |
| 229 |
panzoom_view._mousedown([0, 0]); |
| 230 |
await panzoom_view._mousemove([150, 0]); |
| 231 |
d3Timer.timerFlush(); |
| 232 |
|
| 233 |
// and check if we find a red pixel there as well |
| 234 |
canvas = await figure.get_rendered_canvas(); |
| 235 |
context = canvas.getContext('2d'); |
| 236 |
pixel = context.getImageData( |
| 237 |
test_x * window.devicePixelRatio, |
| 238 |
test_y * window.devicePixelRatio, |
| 239 |
1, |
| 240 |
1 |
| 241 |
); |
| 242 |
expect(Array.prototype.slice.call(pixel.data)).to.deep.equals( |
| 243 |
pixel_background |
| 244 |
); |
| 245 |
|
| 246 |
pixel = context.getImageData( |
| 247 |
(test_x + 150) * window.devicePixelRatio, |
| 248 |
test_y * window.devicePixelRatio, |
| 249 |
1, |
| 250 |
1 |
| 251 |
); |
| 252 |
expect(Array.prototype.slice.call(pixel.data)).to.deep.equals(pixel_red); |
| 253 |
|
| 254 |
// check zooming |
| 255 |
const scale_x = figure.model.get('scale_x'); |
| 256 |
const xmin = scale_x.get('min'); |
| 257 |
const xmax = scale_x.get('max'); |
| 258 |
await panzoom_view._zoom([test_x + 150, test_y], 10); |
| 259 |
const xmin2 = scale_x.get('min'); |
| 260 |
const xmax2 = scale_x.get('max'); |
| 261 |
expect(xmin2).to.be.greaterThan(xmin); |
| 262 |
expect(xmax2).to.be.lessThan(xmax); |
| 263 |
}); |
| 264 |
|
| 265 |
it('pan/zoom log', async function () { |
| 266 |
const x = { |
| 267 |
dtype: 'float32', |
| 268 |
value: new DataView(new Float32Array([1]).buffer), |
| 269 |
}; |
| 270 |
const y = { |
| 271 |
dtype: 'float32', |
| 272 |
value: new DataView(new Float32Array([1]).buffer), |
| 273 |
}; |
| 274 |
const { figure, scatter } = await create_figure_scatter( |
| 275 |
this.manager, |
| 276 |
x, |
| 277 |
y, |
| 278 |
false, |
| 279 |
true |
| 280 |
); |
| 281 |
// scatter.model.set("default_size", 100*100); |
| 282 |
scatter.model.set('default_size', 100 * 100); |
| 283 |
scatter.model.set('colors', ['red']); |
| 284 |
scatter.d3el.selectAll('.object_grp').data(); |
| 285 |
|
| 286 |
const panzoom = await create_model_bqplot( |
| 287 |
this.manager, |
| 288 |
'PanZoom', |
| 289 |
'panzoom', |
| 290 |
{ |
| 291 |
scales: { |
| 292 |
x: [figure.model.get('scale_x').toJSON()], |
| 293 |
y: [figure.model.get('scale_y').toJSON()], |
| 294 |
}, |
| 295 |
} |
| 296 |
); |
| 297 |
const panzoom_view = await figure.set_interaction(panzoom); |
| 298 |
await panzoom_view.displayed; |
| 299 |
await widgets.resolvePromisesDict(panzoom_view.scale_promises); |
| 300 |
|
| 301 |
await figure.relayout(); |
| 302 |
// we want two cycles to make sure relayout is done (it is using requestAnimationFrame) |
| 303 |
await new Promise((resolve) => window.requestAnimationFrame(resolve)); |
| 304 |
await new Promise((resolve) => window.requestAnimationFrame(resolve)); |
| 305 |
let canvas = await figure.get_rendered_canvas(); |
| 306 |
let context = canvas.getContext('2d'); |
| 307 |
let pixel = context.getImageData( |
| 308 |
test_x * window.devicePixelRatio, |
| 309 |
test_y * window.devicePixelRatio, |
| 310 |
1, |
| 311 |
1 |
| 312 |
); |
| 313 |
expect(Array.prototype.slice.call(pixel.data)).to.deep.equals(pixel_red); |
| 314 |
|
| 315 |
// we pan to the right |
| 316 |
panzoom_view._mousedown([0, 0]); |
| 317 |
await panzoom_view._mousemove([150, 0]); |
| 318 |
d3Timer.timerFlush(); |
| 319 |
|
| 320 |
// and check if we find a red pixel there as well |
| 321 |
canvas = await figure.get_rendered_canvas(); |
| 322 |
context = canvas.getContext('2d'); |
| 323 |
pixel = context.getImageData( |
| 324 |
test_x * window.devicePixelRatio, |
| 325 |
test_y * window.devicePixelRatio, |
| 326 |
1, |
| 327 |
1 |
| 328 |
); |
| 329 |
expect(Array.prototype.slice.call(pixel.data)).to.deep.equals( |
| 330 |
pixel_background |
| 331 |
); |
| 332 |
|
| 333 |
pixel = context.getImageData( |
| 334 |
(test_x + 150) * window.devicePixelRatio, |
| 335 |
test_y * window.devicePixelRatio, |
| 336 |
1, |
| 337 |
1 |
| 338 |
); |
| 339 |
expect(Array.prototype.slice.call(pixel.data)).to.deep.equals(pixel_red); |
| 340 |
}); |
| 341 |
}); |