Skip to content

Commit e0d75fc

Browse files
committed
Merge pull request #509 from plotly/fix-clip-path-angular
Fix subplot clip paths in AngularJS
2 parents 6306e37 + b9f4160 commit e0d75fc

File tree

4 files changed

+127
-5
lines changed

4 files changed

+127
-5
lines changed

src/components/rangeslider/range_plot.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
'use strict';
1010

11+
var d3 = require('d3');
12+
1113
var Symbols = require('../drawing/symbol_defs');
1214
var Drawing = require('../drawing');
1315

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

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

4446

src/plot_api/plot_api.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -2919,10 +2919,8 @@ function lsInner(gd) {
29192919
});
29202920

29212921

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

29272925
var xlw = Drawing.crispRound(gd, xa.linewidth, 1),
29282926
ylw = Drawing.crispRound(gd, ya.linewidth, 1),

test/jasmine/tests/drawing_test.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var Drawing = require('@src/components/drawing');
2+
3+
var d3 = require('d3');
4+
5+
6+
describe('Drawing.setClipUrl', function() {
7+
'use strict';
8+
9+
beforeEach(function() {
10+
this.svg = d3.select('body').append('svg');
11+
this.g = this.svg.append('g');
12+
});
13+
14+
afterEach(function() {
15+
this.svg.remove();
16+
this.g.remove();
17+
});
18+
19+
it('should set the clip-path attribute', function() {
20+
expect(this.g.attr('clip-path')).toBe(null);
21+
22+
Drawing.setClipUrl(this.g, 'id1');
23+
24+
expect(this.g.attr('clip-path')).toEqual('url(#id1)');
25+
});
26+
27+
it('should unset the clip-path if arg is falsy', function() {
28+
this.g.attr('clip-path', 'url(#id2)');
29+
30+
Drawing.setClipUrl(this.g, false);
31+
32+
expect(this.g.attr('clip-path')).toBe(null);
33+
});
34+
35+
it('should append window URL to clip-path if <base> is present', function() {
36+
37+
// append <base> with href
38+
var base = d3.select('body')
39+
.append('base')
40+
.attr('href', 'https://plot.ly');
41+
42+
// grab window URL
43+
var href = window.location.href;
44+
45+
Drawing.setClipUrl(this.g, 'id3');
46+
47+
expect(this.g.attr('clip-path'))
48+
.toEqual('url(' + href + '#id3)');
49+
50+
base.remove();
51+
});
52+
});

test/jasmine/tests/plot_interact_test.js

+70
Original file line numberDiff line numberDiff line change
@@ -436,3 +436,73 @@ describe('Test plot structure', function() {
436436
});
437437
});
438438
});
439+
440+
describe('plot svg clip paths', function() {
441+
442+
// plot with all features that rely on clip paths
443+
function plot() {
444+
return Plotly.plot(createGraphDiv(), [{
445+
type: 'contour',
446+
z: [[1,2,3], [2,3,1]]
447+
}, {
448+
type: 'scatter',
449+
y: [2, 1, 2]
450+
}], {
451+
showlegend: true,
452+
xaxis: {
453+
rangeslider: {}
454+
},
455+
shapes: [{
456+
xref: 'x',
457+
yref: 'y',
458+
x0: 0,
459+
y0: 0,
460+
x1: 3,
461+
y1: 3
462+
}]
463+
});
464+
}
465+
466+
afterEach(destroyGraphDiv);
467+
468+
it('should set clip path url to ids (base case)', function(done) {
469+
plot().then(function() {
470+
471+
d3.selectAll('[clip-path]').each(function() {
472+
var cp = d3.select(this).attr('clip-path');
473+
474+
expect(cp.substring(0, 5)).toEqual('url(#');
475+
expect(cp.substring(cp.length - 1)).toEqual(')');
476+
});
477+
478+
done();
479+
});
480+
});
481+
482+
it('should set clip path url to ids appended to window url', function(done) {
483+
484+
// this case occurs in some past versions of AngularJS
485+
// https://github.com/angular/angular.js/issues/8934
486+
487+
// append <base> with href
488+
var base = d3.select('body')
489+
.append('base')
490+
.attr('href', 'https://plot.ly');
491+
492+
// grab window URL
493+
var href = window.location.href;
494+
495+
plot().then(function() {
496+
497+
d3.selectAll('[clip-path]').each(function() {
498+
var cp = d3.select(this).attr('clip-path');
499+
500+
expect(cp.substring(0, 5 + href.length)).toEqual('url(' + href + '#');
501+
expect(cp.substring(cp.length - 1)).toEqual(')');
502+
});
503+
504+
base.remove();
505+
done();
506+
});
507+
});
508+
});

0 commit comments

Comments
 (0)