Skip to content

Plot Area: Plot clip bug #490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2887,23 +2887,28 @@ function lsInner(gd) {
.call(Color.fill, fullLayout.plot_bgcolor);
}


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

clips.selectAll('#' + clipId)
.data([0])
.enter().append('clipPath')
var plotClip = clips.selectAll('#' + clipId)
.data([0]);

plotClip.enter().append('clipPath')
.attr({
'class': 'plotclip',
'id': clipId
})
.append('rect')
.append('rect');

plotClip.selectAll('rect')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done. Man, I gotta got better at noticing these as a reviewer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gotta get better at noticing these as a programmer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But really, I think it's just a symptom of the "d3 pattern". It's not explicitly clear and pretty tough to reason about what exactly each selection/block is doing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like d3 v4 will make this easier.

Taken from https://medium.com/@mbostock/what-makes-software-good-943557f8a488#.rb39u8abk

image

.attr({
'width': xa._length,
'height': ya._length
});


plotinfo.plot.attr({
'transform': 'translate(' + xa._offset + ', ' + ya._offset + ')',
'clip-path': 'url(#' + clipId + ')'
Expand Down
30 changes: 30 additions & 0 deletions test/jasmine/tests/plot_api_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,36 @@ describe('Test plot api', function() {
});
});

describe('Plotly.relayout', function() {
var gd;

beforeEach(function() {
gd = createGraphDiv();
});

afterEach(destroyGraphDiv);

it('should update the plot clipPath if the plot is resized', function(done) {

Plotly.plot(gd, [{ x: [1,2,3], y: [1,2,3] }], { width: 500, height: 500 })
.then(function() {
return Plotly.relayout(gd, { width: 400, height: 400 });
})
.then(function() {
var uid = gd._fullLayout._uid;

var plotClip = document.getElementById('clip' + uid + 'xyplot'),
clipRect = plotClip.children[0],
clipWidth = +clipRect.getAttribute('width'),
clipHeight = +clipRect.getAttribute('height');

expect(clipWidth).toBe(240);
expect(clipHeight).toBe(220);
})
.then(done);
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also add one test for Plots.resize, e.g. like in this http://codepen.io/etpinard/pen/vGVZxe ?

(that's how I noticed that bug after all).


describe('Plotly.restyle', function() {
beforeEach(function() {
spyOn(Plotly, 'plot');
Expand Down
44 changes: 44 additions & 0 deletions test/jasmine/tests/plots_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,50 @@ describe('Test Plots', function() {

});

describe('Plots.resize', function() {
var gd;

beforeEach(function(done) {
gd = createGraphDiv();

Plotly.plot(gd, [{ x: [1,2,3], y: [2,3,4] }], {})
.then(function() {
gd.style.width = '400px';
gd.style.height = '400px';

return Plotly.Plots.resize(gd);
})
.then(done);
});

afterEach(destroyGraphDiv);

it('should resize the plot clip', function() {
var uid = gd._fullLayout._uid;

var plotClip = document.getElementById('clip' + uid + 'xyplot'),
clipRect = plotClip.children[0],
clipWidth = +clipRect.getAttribute('width'),
clipHeight = +clipRect.getAttribute('height');

expect(clipWidth).toBe(240);
expect(clipHeight).toBe(220);
});

it('should resize the main svgs', function() {
var mainSvgs = document.getElementsByClassName('main-svg');

for(var i = 0; i < mainSvgs.length; i++) {
var svg = mainSvgs[i],
svgWidth = +svg.getAttribute('width'),
svgHeight = +svg.getAttribute('height');

expect(svgWidth).toBe(400);
expect(svgHeight).toBe(400);
}
});
});

describe('Plots.purge', function() {
var gd;

Expand Down