Skip to content

Commit 60ffe4f

Browse files
authored
Merge pull request #3435 from plotly/legend-valign-fix
legend valign fix for pie traces
2 parents 51b5c25 + f51ee96 commit 60ffe4f

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

src/components/legend/draw.js

+17-20
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,20 @@ module.exports = function draw(gd) {
108108
traces.enter().append('g').attr('class', 'traces');
109109
traces.exit().remove();
110110

111-
traces.call(style, gd)
112-
.style('opacity', function(d) {
113-
var trace = d[0].trace;
114-
if(Registry.traceIs(trace, 'pie')) {
115-
return hiddenSlices.indexOf(d[0].label) !== -1 ? 0.5 : 1;
116-
} else {
117-
return trace.visible === 'legendonly' ? 0.5 : 1;
118-
}
119-
})
120-
.each(function() {
121-
d3.select(this)
122-
.call(drawTexts, gd, maxLength)
123-
.call(setupTraceToggle, gd);
124-
});
111+
traces.style('opacity', function(d) {
112+
var trace = d[0].trace;
113+
if(Registry.traceIs(trace, 'pie')) {
114+
return hiddenSlices.indexOf(d[0].label) !== -1 ? 0.5 : 1;
115+
} else {
116+
return trace.visible === 'legendonly' ? 0.5 : 1;
117+
}
118+
})
119+
.each(function() {
120+
d3.select(this)
121+
.call(drawTexts, gd, maxLength)
122+
.call(setupTraceToggle, gd);
123+
})
124+
.call(style, gd);
125125

126126
Lib.syncOrAsync([Plots.previousPromises,
127127
function() {
@@ -522,8 +522,7 @@ function computeTextDimensions(g, gd) {
522522
width = mathjaxBB.width;
523523

524524
Drawing.setTranslate(mathjaxGroup, 0, (height / 4));
525-
}
526-
else {
525+
} else {
527526
var text = g.select('.legendtext');
528527
var textLines = svgTextUtils.lineCount(text);
529528
var textNode = text.node();
@@ -535,12 +534,10 @@ function computeTextDimensions(g, gd) {
535534
// to avoid getBoundingClientRect
536535
var textY = lineHeight * (0.3 + (1 - textLines) / 2);
537536
svgTextUtils.positionText(text, constants.textOffsetX, textY);
538-
legendItem.lineHeight = lineHeight;
539537
}
540538

541-
height = Math.max(height, 16) + 3;
542-
543-
legendItem.height = height;
539+
legendItem.lineHeight = lineHeight;
540+
legendItem.height = Math.max(height, 16) + 3;
544541
legendItem.width = width;
545542
}
546543

src/components/legend/style.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = function style(s, gd) {
3131
var height = d[0].height;
3232

3333
if(valign === 'middle' || !lineHeight || !height) {
34-
layers.attr('transform', null); // this here is a fun d3 trick to unset DOM attributes
34+
layers.attr('transform', null);
3535
} else {
3636
var factor = {top: 1, bottom: -1}[valign];
3737
var markerOffsetY = factor * (0.5 * (lineHeight - height + 3));
-1.83 KB
Loading

test/image/mocks/legend_valign_top.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,22 @@
1010
"name": "Also super long name<br>Also super long name<br>Also super long name",
1111
"type": "scatter",
1212
"showlegend": true
13+
},
14+
{
15+
"type": "pie",
16+
"labels": ["a very<br>long<br>legend item", "a", "b", "c"],
17+
"values": [10, 15, 13, 17],
18+
"domain": {"x": [0.5, 1]}
1319
}
1420
],
1521
"layout": {
1622
"width": 800,
23+
"xaxis": {"domain": [0, 0.5]},
1724
"legend": {
1825
"bgcolor": "rgba(0,255,255,1)",
1926
"valign": "top",
2027
"font": {
21-
"size": 20
28+
"size": 10
2229
}
2330
}
2431
}

0 commit comments

Comments
 (0)