Skip to content

Commit e8b9ca2

Browse files
authored
Merge pull request #672 from plotly/images-add-remove
Fix layout images add / remove
2 parents f69fc5f + badc4e1 commit e8b9ca2

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

src/components/images/draw.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ module.exports = function draw(gd) {
145145

146146

147147
// Required for updating images
148-
function keyFunction(d) {
149-
return d.source;
148+
function keyFunction(d, i) {
149+
return d.source + i;
150150
}
151151

152152

test/jasmine/tests/layout_images_test.js

+58
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var Plotly = require('@lib/index');
22
var Plots = require('@src/plots/plots');
33
var Images = require('@src/components/images');
4+
5+
var d3 = require('d3');
46
var createGraphDiv = require('../assets/create_graph_div');
57
var destroyGraphDiv = require('../assets/destroy_graph_div');
68
var mouseEvent = require('../assets/mouse_event');
@@ -290,4 +292,60 @@ describe('Layout images', function() {
290292
});
291293
});
292294

295+
describe('when adding/removing images', function() {
296+
297+
afterEach(destroyGraphDiv);
298+
299+
it('should properly add and removing image', function(done) {
300+
var gd = createGraphDiv(),
301+
data = [{ x: [1, 2, 3], y: [1, 2, 3] }],
302+
layout = { width: 500, height: 400 };
303+
304+
function makeImage(source, x, y) {
305+
return {
306+
source: source,
307+
x: x,
308+
y: y,
309+
sizex: 1,
310+
sizey: 1
311+
};
312+
}
313+
314+
function assertImages(cnt) {
315+
expect(d3.selectAll('image').size()).toEqual(cnt);
316+
}
317+
318+
Plotly.plot(gd, data, layout).then(function() {
319+
assertImages(0);
320+
321+
return Plotly.relayout(gd, 'images[0]', makeImage(jsLogo, 0.1, 0.1));
322+
}).then(function() {
323+
assertImages(1);
324+
325+
return Plotly.relayout(gd, 'images[1]', makeImage(pythonLogo, 0.9, 0.9));
326+
}).then(function() {
327+
assertImages(2);
328+
329+
return Plotly.relayout(gd, 'images[2]', makeImage(pythonLogo, 0.2, 0.5));
330+
}).then(function() {
331+
assertImages(3);
332+
333+
return Plotly.relayout(gd, 'images[2]', 'remove');
334+
}).then(function() {
335+
assertImages(2);
336+
337+
return Plotly.relayout(gd, 'images[1]', 'remove');
338+
}).then(function() {
339+
assertImages(1);
340+
341+
return Plotly.relayout(gd, 'images[0]', 'remove');
342+
}).then(function() {
343+
assertImages(0);
344+
345+
done();
346+
});
347+
});
348+
349+
});
350+
293351
});

0 commit comments

Comments
 (0)