Skip to content

Commit

Permalink
use fullLayout._gradientUrlQueryParts stash to fix gradient URLs
Browse files Browse the repository at this point in the history
... during image exports, instead of having hard-coded list
    of query-selectors in toSVG routine.
  • Loading branch information
etpinard committed Dec 24, 2019
1 parent b4ac79e commit 55d76b2
Show file tree
Hide file tree
Showing 2 changed files with 38 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, path.legend3dandfriends, .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

0 comments on commit 55d76b2

Please sign in to comment.