Skip to content

Fix subplot clip paths in AngularJS #509

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 6 commits into from
May 5, 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
4 changes: 3 additions & 1 deletion src/components/rangeslider/range_plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

'use strict';

var d3 = require('d3');

var Symbols = require('../drawing/symbol_defs');
var Drawing = require('../drawing');

Expand Down Expand Up @@ -38,7 +40,7 @@ module.exports = function rangePlot(gd, w, h) {
clipDefs.appendChild(clip);

var rangePlot = document.createElementNS(svgNS, 'g');
rangePlot.setAttribute('clip-path', 'url(#range-clip-path)');
d3.select(rangePlot).call(Drawing.setClipUrl, 'range-clip-path');
rangePlot.appendChild(clipDefs);


Expand Down
6 changes: 2 additions & 4 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2913,10 +2913,8 @@ function lsInner(gd) {
});


plotinfo.plot.attr({
'transform': 'translate(' + xa._offset + ', ' + ya._offset + ')',
'clip-path': 'url(#' + clipId + ')'
});
plotinfo.plot.call(Lib.setTranslate, xa._offset, ya._offset);
plotinfo.plot.call(Drawing.setClipUrl, clipId);

var xlw = Drawing.crispRound(gd, xa.linewidth, 1),
ylw = Drawing.crispRound(gd, ya.linewidth, 1),
Expand Down
52 changes: 52 additions & 0 deletions test/jasmine/tests/drawing_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var Drawing = require('@src/components/drawing');

var d3 = require('d3');


describe('Drawing.setClipUrl', function() {
'use strict';

beforeEach(function() {
this.svg = d3.select('body').append('svg');
this.g = this.svg.append('g');
});

afterEach(function() {
this.svg.remove();
this.g.remove();
});

it('should set the clip-path attribute', function() {
expect(this.g.attr('clip-path')).toBe(null);

Drawing.setClipUrl(this.g, 'id1');

expect(this.g.attr('clip-path')).toEqual('url(#id1)');
});

it('should unset the clip-path if arg is falsy', function() {
this.g.attr('clip-path', 'url(#id2)');

Drawing.setClipUrl(this.g, false);

expect(this.g.attr('clip-path')).toBe(null);
});

it('should append window URL to clip-path if <base> is present', function() {

// append <base> with href
var base = d3.select('body')
.append('base')
.attr('href', 'https://plot.ly');

// grab window URL
var href = window.location.href;

Drawing.setClipUrl(this.g, 'id3');

expect(this.g.attr('clip-path'))
.toEqual('url(' + href + '#id3)');

base.remove();
});
});
70 changes: 70 additions & 0 deletions test/jasmine/tests/plot_interact_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,73 @@ describe('Test plot structure', function() {
});
});
});

describe('plot svg clip paths', function() {

// plot with all features that rely on clip paths
function plot() {
return Plotly.plot(createGraphDiv(), [{
type: 'contour',
z: [[1,2,3], [2,3,1]]
}, {
type: 'scatter',
y: [2, 1, 2]
}], {
showlegend: true,
xaxis: {
rangeslider: {}
},
shapes: [{
xref: 'x',
yref: 'y',
x0: 0,
y0: 0,
x1: 3,
y1: 3
}]
});
}

afterEach(destroyGraphDiv);

it('should set clip path url to ids (base case)', function(done) {
plot().then(function() {

d3.selectAll('[clip-path]').each(function() {
var cp = d3.select(this).attr('clip-path');

expect(cp.substring(0, 5)).toEqual('url(#');
expect(cp.substring(cp.length - 1)).toEqual(')');
});

done();
});
});

it('should set clip path url to ids appended to window url', function(done) {

// this case occurs in some past versions of AngularJS
// https://github.com/angular/angular.js/issues/8934

// append <base> with href
var base = d3.select('body')
.append('base')
.attr('href', 'https://plot.ly');

// grab window URL
var href = window.location.href;

plot().then(function() {

d3.selectAll('[clip-path]').each(function() {
var cp = d3.select(this).attr('clip-path');

expect(cp.substring(0, 5 + href.length)).toEqual('url(' + href + '#');
expect(cp.substring(cp.length - 1)).toEqual(')');
});

base.remove();
done();
});
});
});