-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathlayout_images_test.js
355 lines (271 loc) · 10.5 KB
/
layout_images_test.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
var Plotly = require('@lib/index');
var Plots = require('@src/plots/plots');
var Images = require('@src/components/images');
var d3 = require('d3');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var mouseEvent = require('../assets/mouse_event');
var jsLogo = 'https://images.plot.ly/language-icons/api-home/js-logo.png';
var pythonLogo = 'https://images.plot.ly/language-icons/api-home/python-logo.png';
describe('Layout images', function() {
describe('supplyLayoutDefaults', function() {
var layoutIn,
layoutOut;
beforeEach(function() {
layoutIn = { images: [] };
layoutOut = { _has: Plots._hasPlotType };
});
it('should reject when there is no `source`', function() {
layoutIn.images[0] = { opacity: 0.5, sizex: 0.2, sizey: 0.2 };
Images.supplyLayoutDefaults(layoutIn, layoutOut);
expect(layoutOut.images.length).toEqual(0);
});
it('should reject when not an array', function() {
layoutIn.images = {
source: jsLogo,
opacity: 0.5,
sizex: 0.2,
sizey: 0.2
};
Images.supplyLayoutDefaults(layoutIn, layoutOut);
expect(layoutOut.images).not.toBeDefined();
});
it('should coerce the correct defaults', function() {
layoutIn.images[0] = { source: jsLogo };
var expected = {
source: jsLogo,
layer: 'above',
x: 0,
y: 0,
xanchor: 'left',
yanchor: 'top',
sizex: 0,
sizey: 0,
sizing: 'contain',
opacity: 1,
xref: 'paper',
yref: 'paper'
};
Images.supplyLayoutDefaults(layoutIn, layoutOut);
expect(layoutOut.images[0]).toEqual(expected);
});
});
describe('drawing', function() {
var gd,
data = [{ x: [1, 2, 3], y: [1, 2, 3] }];
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);
it('should draw images on the right layers', function() {
var layer;
Plotly.plot(gd, data, { images: [{
source: 'imageabove',
layer: 'above'
}]});
layer = gd._fullLayout._imageUpperLayer;
expect(layer.length).toBe(1);
destroyGraphDiv();
gd = createGraphDiv();
Plotly.plot(gd, data, { images: [{
source: 'imagebelow',
layer: 'below'
}]});
layer = gd._fullLayout._imageLowerLayer;
expect(layer.length).toBe(1);
destroyGraphDiv();
gd = createGraphDiv();
Plotly.plot(gd, data, { images: [{
source: 'imagesubplot',
layer: 'below',
xref: 'x',
yref: 'y'
}]});
layer = gd._fullLayout._imageSubplotLayer;
expect(layer.length).toBe(1);
});
describe('with anchors and sizing', function() {
function testAspectRatio(xAnchor, yAnchor, sizing, expected) {
var anchorName = xAnchor + yAnchor;
Plotly.plot(gd, data, { images: [{
source: anchorName,
xanchor: xAnchor,
yanchor: yAnchor,
sizing: sizing
}]});
var image = Plotly.d3.select('image'),
parValue = image.attr('preserveAspectRatio');
expect(parValue).toBe(expected);
}
it('should work for center middle', function() {
testAspectRatio('center', 'middle', undefined, 'xMidYMid');
});
it('should work for left top', function() {
testAspectRatio('left', 'top', undefined, 'xMinYMin');
});
it('should work for right bottom', function() {
testAspectRatio('right', 'bottom', undefined, 'xMaxYMax');
});
it('should work for stretch sizing', function() {
testAspectRatio('middle', 'center', 'stretch', 'none');
});
it('should work for fill sizing', function() {
testAspectRatio('invalid', 'invalid', 'fill', 'xMinYMin slice');
});
});
});
describe('when the plot is dragged', function() {
var gd,
data = [{ x: [1, 2, 3], y: [1, 2, 3] }];
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);
it('should not move when referencing the paper', function(done) {
var image = {
source: jsLogo,
xref: 'paper',
yref: 'paper',
x: 0,
y: 0,
sizex: 0.1,
sizey: 0.1
};
Plotly.plot(gd, data, {
images: [image],
dragmode: 'pan',
width: 600,
height: 400
}).then(function() {
var img = Plotly.d3.select('image').node(),
oldPos = img.getBoundingClientRect();
mouseEvent('mousedown', 250, 200);
mouseEvent('mousemove', 300, 250);
var newPos = img.getBoundingClientRect();
expect(newPos.left).toBe(oldPos.left);
expect(newPos.top).toBe(oldPos.top);
mouseEvent('mouseup', 300, 250);
}).then(done);
});
it('should move when referencing axes', function(done) {
var image = {
source: jsLogo,
xref: 'x',
yref: 'y',
x: 2,
y: 2,
sizex: 1,
sizey: 1
};
Plotly.plot(gd, data, {
images: [image],
dragmode: 'pan',
width: 600,
height: 400
}).then(function() {
var img = Plotly.d3.select('image').node(),
oldPos = img.getBoundingClientRect();
mouseEvent('mousedown', 250, 200);
mouseEvent('mousemove', 300, 250);
var newPos = img.getBoundingClientRect();
expect(newPos.left).toBe(oldPos.left + 50);
expect(newPos.top).toBe(oldPos.top + 50);
mouseEvent('mouseup', 300, 250);
}).then(done);
});
});
describe('when relayout', function() {
var gd,
data = [{ x: [1, 2, 3], y: [1, 2, 3] }];
beforeEach(function(done) {
gd = createGraphDiv();
Plotly.plot(gd, data, {
images: [{
source: jsLogo,
x: 2,
y: 2,
sizex: 1,
sizey: 1
}],
width: 500,
height: 400
}).then(done);
});
afterEach(destroyGraphDiv);
it('should update the image if changed', function(done) {
var img = Plotly.d3.select('image'),
url = img.attr('xlink:href');
Plotly.relayout(gd, 'images[0].source', pythonLogo).then(function() {
var newImg = Plotly.d3.select('image'),
newUrl = newImg.attr('xlink:href');
expect(url).not.toBe(newUrl);
}).then(done);
});
it('should update the image position if changed', function(done) {
var update = {
'images[0].x': 0,
'images[0].y': 1
};
var img = Plotly.d3.select('image');
expect([+img.attr('x'), +img.attr('y')]).toEqual([760, -120]);
Plotly.relayout(gd, update).then(function() {
var newImg = Plotly.d3.select('image');
expect([+newImg.attr('x'), +newImg.attr('y')]).toEqual([80, 100]);
}).then(done);
});
it('should remove the image tag if an invalid source', function(done) {
var selection = Plotly.d3.select('image');
expect(selection.size()).toBe(1);
Plotly.relayout(gd, 'images[0].source', 'invalidUrl').then(function() {
var newSelection = Plotly.d3.select('image');
expect(newSelection.size()).toBe(0);
}).then(done);
});
});
describe('when adding/removing images', function() {
afterEach(destroyGraphDiv);
it('should properly add and removing image', function(done) {
var gd = createGraphDiv(),
data = [{ x: [1, 2, 3], y: [1, 2, 3] }],
layout = { width: 500, height: 400 };
function makeImage(source, x, y) {
return {
source: source,
x: x,
y: y,
sizex: 1,
sizey: 1
};
}
function assertImages(cnt) {
expect(d3.selectAll('image').size()).toEqual(cnt);
}
Plotly.plot(gd, data, layout).then(function() {
assertImages(0);
return Plotly.relayout(gd, 'images[0]', makeImage(jsLogo, 0.1, 0.1));
}).then(function() {
assertImages(1);
return Plotly.relayout(gd, 'images[1]', makeImage(pythonLogo, 0.9, 0.9));
}).then(function() {
assertImages(2);
return Plotly.relayout(gd, 'images[2]', makeImage(pythonLogo, 0.2, 0.5));
}).then(function() {
assertImages(3);
expect(gd.layout.images.length).toEqual(3);
return Plotly.relayout(gd, 'images[2]', null);
}).then(function() {
assertImages(2);
expect(gd.layout.images.length).toEqual(2);
return Plotly.relayout(gd, 'images[1]', null);
}).then(function() {
assertImages(1);
expect(gd.layout.images.length).toEqual(1);
return Plotly.relayout(gd, 'images[0]', null);
}).then(function() {
assertImages(0);
expect(gd.layout.images).toEqual([]);
done();
});
});
});
});