Skip to content

Commit 4a9243e

Browse files
committed
Merge pull request #490 from plotly/plot-clip-bug
Plot Area: Plot clip relayout bug
2 parents 7a94fec + fe43f21 commit 4a9243e

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

src/plot_api/plot_api.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2887,23 +2887,28 @@ function lsInner(gd) {
28872887
.call(Color.fill, fullLayout.plot_bgcolor);
28882888
}
28892889

2890+
28902891
// Clip so that data only shows up on the plot area.
28912892
var clips = fullLayout._defs.selectAll('g.clips'),
28922893
clipId = 'clip' + fullLayout._uid + subplot + 'plot';
28932894

2894-
clips.selectAll('#' + clipId)
2895-
.data([0])
2896-
.enter().append('clipPath')
2895+
var plotClip = clips.selectAll('#' + clipId)
2896+
.data([0]);
2897+
2898+
plotClip.enter().append('clipPath')
28972899
.attr({
28982900
'class': 'plotclip',
28992901
'id': clipId
29002902
})
2901-
.append('rect')
2903+
.append('rect');
2904+
2905+
plotClip.selectAll('rect')
29022906
.attr({
29032907
'width': xa._length,
29042908
'height': ya._length
29052909
});
29062910

2911+
29072912
plotinfo.plot.attr({
29082913
'transform': 'translate(' + xa._offset + ', ' + ya._offset + ')',
29092914
'clip-path': 'url(#' + clipId + ')'

test/jasmine/tests/plot_api_test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,36 @@ describe('Test plot api', function() {
2020
});
2121
});
2222

23+
describe('Plotly.relayout', function() {
24+
var gd;
25+
26+
beforeEach(function() {
27+
gd = createGraphDiv();
28+
});
29+
30+
afterEach(destroyGraphDiv);
31+
32+
it('should update the plot clipPath if the plot is resized', function(done) {
33+
34+
Plotly.plot(gd, [{ x: [1,2,3], y: [1,2,3] }], { width: 500, height: 500 })
35+
.then(function() {
36+
return Plotly.relayout(gd, { width: 400, height: 400 });
37+
})
38+
.then(function() {
39+
var uid = gd._fullLayout._uid;
40+
41+
var plotClip = document.getElementById('clip' + uid + 'xyplot'),
42+
clipRect = plotClip.children[0],
43+
clipWidth = +clipRect.getAttribute('width'),
44+
clipHeight = +clipRect.getAttribute('height');
45+
46+
expect(clipWidth).toBe(240);
47+
expect(clipHeight).toBe(220);
48+
})
49+
.then(done);
50+
});
51+
});
52+
2353
describe('Plotly.restyle', function() {
2454
beforeEach(function() {
2555
spyOn(Plotly, 'plot');

test/jasmine/tests/plots_test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,50 @@ describe('Test Plots', function() {
320320

321321
});
322322

323+
describe('Plots.resize', function() {
324+
var gd;
325+
326+
beforeEach(function(done) {
327+
gd = createGraphDiv();
328+
329+
Plotly.plot(gd, [{ x: [1,2,3], y: [2,3,4] }], {})
330+
.then(function() {
331+
gd.style.width = '400px';
332+
gd.style.height = '400px';
333+
334+
return Plotly.Plots.resize(gd);
335+
})
336+
.then(done);
337+
});
338+
339+
afterEach(destroyGraphDiv);
340+
341+
it('should resize the plot clip', function() {
342+
var uid = gd._fullLayout._uid;
343+
344+
var plotClip = document.getElementById('clip' + uid + 'xyplot'),
345+
clipRect = plotClip.children[0],
346+
clipWidth = +clipRect.getAttribute('width'),
347+
clipHeight = +clipRect.getAttribute('height');
348+
349+
expect(clipWidth).toBe(240);
350+
expect(clipHeight).toBe(220);
351+
});
352+
353+
it('should resize the main svgs', function() {
354+
var mainSvgs = document.getElementsByClassName('main-svg');
355+
356+
for(var i = 0; i < mainSvgs.length; i++) {
357+
var svg = mainSvgs[i],
358+
svgWidth = +svg.getAttribute('width'),
359+
svgHeight = +svg.getAttribute('height');
360+
361+
expect(svgWidth).toBe(400);
362+
expect(svgHeight).toBe(400);
363+
}
364+
});
365+
});
366+
323367
describe('Plots.purge', function() {
324368
var gd;
325369

0 commit comments

Comments
 (0)