Skip to content

Commit 407c361

Browse files
committed
implementation of valign for legends
1 parent 3a32ac0 commit 407c361

File tree

6 files changed

+45
-1
lines changed

6 files changed

+45
-1
lines changed

src/components/legend/attributes.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,15 @@ module.exports = {
120120
'or *bottom* of the legend.'
121121
].join(' ')
122122
},
123-
editType: 'legend'
123+
editType: 'legend',
124+
valign: {
125+
valType: 'enumerated',
126+
values: ['top', 'middle', 'bottom'],
127+
dflt: 'middle',
128+
role: 'style',
129+
editType: 'legend',
130+
description: [
131+
'Sets the vertical alignment of the symbol with respect to the `text`.',
132+
].join(' ')
133+
}
124134
};

src/components/legend/defaults.js

+1
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,6 @@ module.exports = function legendDefaults(layoutIn, layoutOut, fullData) {
103103
coerce('xanchor', defaultXAnchor);
104104
coerce('y', defaultY);
105105
coerce('yanchor', defaultYAnchor);
106+
coerce('valign')
106107
Lib.noneOrAll(containerIn, containerOut, ['x', 'y']);
107108
};

src/components/legend/draw.js

+1
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ function computeTextDimensions(g, gd) {
536536
// to avoid getBoundingClientRect
537537
var textY = lineHeight * (0.3 + (1 - textLines) / 2);
538538
svgTextUtils.positionText(text, constants.textOffsetX, textY);
539+
legendItem.lineHeight = lineHeight;
539540
}
540541

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

src/components/legend/style.js

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ module.exports = function style(s, gd) {
2525
var layers = Lib.ensureSingle(traceGroup, 'g', 'layers');
2626
layers.style('opacity', d[0].trace.opacity);
2727

28+
// Marker vertical alignment
29+
var valignFactor = 0;
30+
if(gd._fullLayout.legend.valign === 'top') valignFactor = 1.0;
31+
if(gd._fullLayout.legend.valign === 'bottom') valignFactor = -1.0;
32+
var markerOffsetY = valignFactor * (0.5 * (d[0].lineHeight - d[0].height + 3));
33+
if(markerOffsetY) layers.attr('transform', 'translate(0,' + markerOffsetY + ')');
34+
2835
var fill = layers
2936
.selectAll('g.legendfill')
3037
.data([d]);
55.9 KB
Loading

test/image/mocks/legend_valign.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"data": [{
3+
"y": [1, 5, 3, 4, 5],
4+
"name": "Super long name<br>Super long name<br>Super long name<br>Super long name",
5+
"type": "scatter",
6+
"showlegend": true
7+
},
8+
{
9+
"y": [3, 2, 5, 1, 5],
10+
"name": "Also super long name<br>Also super long name<br>Also super long name",
11+
"type": "scatter",
12+
"showlegend": true
13+
}
14+
],
15+
"layout": {
16+
"width": 800,
17+
"legend": {
18+
"bgcolor": "rgba(0,255,255,1)",
19+
"valign": "top",
20+
"font": {
21+
"size": 20
22+
}
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)