Skip to content

Commit

Permalink
Merge pull request #4441 from plotly/fix-export-legend-gradients-pr4386
Browse files Browse the repository at this point in the history
Fix image exports for new #4386 mocks with SVG gradients
  • Loading branch information
etpinard authored Dec 24, 2019
2 parents 3920eed + 55d76b2 commit 4137ed2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
20 changes: 17 additions & 3 deletions src/components/drawing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,10 @@ drawing.gradient = function(sel, gd, gradientID, type, colorscale, prop) {
}
}

var fullID = 'g' + gd._fullLayout._uid + '-' + gradientID;
var fullLayout = gd._fullLayout;
var fullID = 'g' + fullLayout._uid + '-' + gradientID;

var gradient = gd._fullLayout._defs.select('.gradients')
var gradient = fullLayout._defs.select('.gradients')
.selectAll('#' + fullID)
.data([type + colorStops.join(';')], Lib.identity);

Expand Down Expand Up @@ -333,6 +334,13 @@ drawing.gradient = function(sel, gd, gradientID, type, colorscale, prop) {

sel.style(prop, getFullUrl(fullID, gd))
.style(prop + '-opacity', null);

var className2query = function(s) {
return '.' + s.attr('class').replace(/\s/g, '.');
};
var k = className2query(d3.select(sel.node().parentNode)) +
'>' + className2query(sel);
fullLayout._gradientUrlQueryParts[k] = 1;
};

/*
Expand All @@ -343,8 +351,14 @@ drawing.gradient = function(sel, gd, gradientID, type, colorscale, prop) {
* The upside of this is arbitrary points can share gradient defs
*/
drawing.initGradients = function(gd) {
var gradientsGroup = Lib.ensureSingle(gd._fullLayout._defs, 'g', 'gradients');
var fullLayout = gd._fullLayout;

var gradientsGroup = Lib.ensureSingle(fullLayout._defs, 'g', 'gradients');
gradientsGroup.selectAll('linearGradient,radialGradient').remove();

// initialize stash of query parts filled in Drawing.gradient,
// used to fix URL strings during image exports
fullLayout._gradientUrlQueryParts = {};
};


Expand Down
34 changes: 21 additions & 13 deletions src/snapshot/tosvg.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,29 @@ module.exports = function toSVG(gd, format, scale) {
}
});

svg.selectAll('.point, .scatterpts, .legendfill>path, .legendlines>path, .cbfill').each(function() {
var pt = d3.select(this);

// similar to font family styles above,
// we must remove " after the SVG DOM has been serialized
var fill = this.style.fill;
if(fill && fill.indexOf('url(') !== -1) {
pt.style('fill', fill.replace(DOUBLEQUOTE_REGEX, DUMMY_SUB));
}

var stroke = this.style.stroke;
if(stroke && stroke.indexOf('url(') !== -1) {
pt.style('stroke', stroke.replace(DOUBLEQUOTE_REGEX, DUMMY_SUB));
if(fullLayout._gradientUrlQueryParts) {
var queryParts = [];
for(var k in fullLayout._gradientUrlQueryParts) queryParts.push(k);

if(queryParts.length) {
svg.selectAll(queryParts.join(',')).each(function() {
var pt = d3.select(this);

// similar to font family styles above,
// we must remove " after the SVG DOM has been serialized
var fill = this.style.fill;
if(fill && fill.indexOf('url(') !== -1) {
pt.style('fill', fill.replace(DOUBLEQUOTE_REGEX, DUMMY_SUB));
}

var stroke = this.style.stroke;
if(stroke && stroke.indexOf('url(') !== -1) {
pt.style('stroke', stroke.replace(DOUBLEQUOTE_REGEX, DUMMY_SUB));
}
});
}
});
}

if(format === 'pdf' || format === 'eps') {
// these formats make the extra line MathJax adds around symbols look super thick in some cases
Expand Down
18 changes: 18 additions & 0 deletions test/jasmine/tests/snapshot_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,24 @@ describe('Plotly.Snapshot', function() {
.catch(failTest)
.then(done);
});

it('- legend3dandfriends case', function(done) {
var fig = Lib.extendDeep({}, require('@mocks/geo_choropleth-legend.json'));

Plotly.plot(gd, fig)
.then(function() { return Plotly.Snapshot.toSVG(gd); })
.then(function(svg) {
var svgDOM = parser.parseFromString(svg, 'image/svg+xml');

var fillItems = svgDOM.getElementsByClassName('legend3dandfriends');
expect(fillItems.length).toBe(4, '# of legend items');
for(var i = 0; i < fillItems.length; i++) {
checkURL(fillItems[i].style.fill, 'fill gradient ' + i);
}
})
.catch(failTest)
.then(done);
});
});

it('should adapt *viewBox* attribute under *scale* option', function(done) {
Expand Down

0 comments on commit 4137ed2

Please sign in to comment.