Skip to content

legend valign fix for pie traces #3435

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 2 commits into from
Jan 14, 2019
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
37 changes: 17 additions & 20 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,20 @@ module.exports = function draw(gd) {
traces.enter().append('g').attr('class', 'traces');
traces.exit().remove();

traces.call(style, gd)
.style('opacity', function(d) {
var trace = d[0].trace;
if(Registry.traceIs(trace, 'pie')) {
return hiddenSlices.indexOf(d[0].label) !== -1 ? 0.5 : 1;
} else {
return trace.visible === 'legendonly' ? 0.5 : 1;
}
})
.each(function() {
d3.select(this)
.call(drawTexts, gd, maxLength)
.call(setupTraceToggle, gd);
});
traces.style('opacity', function(d) {
var trace = d[0].trace;
if(Registry.traceIs(trace, 'pie')) {
return hiddenSlices.indexOf(d[0].label) !== -1 ? 0.5 : 1;
} else {
return trace.visible === 'legendonly' ? 0.5 : 1;
}
})
.each(function() {
d3.select(this)
.call(drawTexts, gd, maxLength)
.call(setupTraceToggle, gd);
})
.call(style, gd);

Lib.syncOrAsync([Plots.previousPromises,
function() {
Expand Down Expand Up @@ -522,8 +522,7 @@ function computeTextDimensions(g, gd) {
width = mathjaxBB.width;

Drawing.setTranslate(mathjaxGroup, 0, (height / 4));
}
else {
} else {
var text = g.select('.legendtext');
var textLines = svgTextUtils.lineCount(text);
var textNode = text.node();
Expand All @@ -535,12 +534,10 @@ function computeTextDimensions(g, gd) {
// to avoid getBoundingClientRect
var textY = lineHeight * (0.3 + (1 - textLines) / 2);
svgTextUtils.positionText(text, constants.textOffsetX, textY);
legendItem.lineHeight = lineHeight;
}

height = Math.max(height, 16) + 3;

legendItem.height = height;
legendItem.lineHeight = lineHeight;
legendItem.height = Math.max(height, 16) + 3;
legendItem.width = width;
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/legend/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = function style(s, gd) {
var height = d[0].height;

if(valign === 'middle' || !lineHeight || !height) {
layers.attr('transform', null); // this here is a fun d3 trick to unset DOM attributes
layers.attr('transform', null);
} else {
var factor = {top: 1, bottom: -1}[valign];
var markerOffsetY = factor * (0.5 * (lineHeight - height + 3));
Expand Down
Binary file modified test/image/baselines/legend_valign_top.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion test/image/mocks/legend_valign_top.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@
"name": "Also super long name<br>Also super long name<br>Also super long name",
"type": "scatter",
"showlegend": true
},
{
"type": "pie",
"labels": ["a very<br>long<br>legend item", "a", "b", "c"],
"values": [10, 15, 13, 17],
"domain": {"x": [0.5, 1]}
}
],
"layout": {
"width": 800,
"xaxis": {"domain": [0, 0.5]},
"legend": {
"bgcolor": "rgba(0,255,255,1)",
"valign": "top",
"font": {
"size": 20
"size": 10
}
}
}
Expand Down