Skip to content

Commit 39293cc

Browse files
committed
adjust legend alignments & layout
1 parent 2da3fe4 commit 39293cc

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/components/legend/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ module.exports = {
1818
// number of px between legend title and (left) side of legend (always in x direction and from inner border)
1919
titlePad: 2,
2020
// number of px between each legend item (x and/or y direction)
21-
itemGap: 5
21+
itemGap: 1
2222
};

src/components/legend/draw.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,19 @@ module.exports = function draw(gd, opts) {
134134
var gs = fullLayout._size;
135135
var bw = opts.borderwidth;
136136

137-
var lx = gs.l + gs.w * opts.x - FROM_TL[getXanchor(opts)] * opts._width;
137+
var xanchor = getXanchor(opts);
138+
var yanchor = getYanchor(opts);
139+
// Adjust centering of the legend box
140+
var lx;
141+
if (xanchor === 'center' && yanchor === 'top') {
142+
lx = (gs.w * opts.x - 0.5 * opts._width) * 1.8;
143+
} else if (xanchor === 'center' && yanchor === 'bottom') {
144+
lx = (gs.l + gs.w * opts.x - FROM_TL[getXanchor(opts)] * opts._width)-30;
145+
} else if (xanchor === 'center' && yanchor === 'middle') {
146+
lx = (gs.l + gs.w * opts.x - FROM_TL[getXanchor(opts)] * opts._width)-15;
147+
} else {
148+
lx = gs.l + gs.w * opts.x - FROM_TL[getXanchor(opts)] * opts._width;
149+
}
138150
var ly = gs.t + gs.h * (1 - opts.y) - FROM_TL[getYanchor(opts)] * opts._effHeight;
139151

140152
if(opts._main && fullLayout.margin.autoexpand) {
@@ -640,6 +652,7 @@ function computeLegendDimensions(gd, groups, traces, opts) {
640652
}
641653
} else {
642654
var xanchor = getXanchor(opts);
655+
var isCenterOfPlotArea = (xanchor === 'center');
643656
var isLeftOfPlotArea = opts.x < 0 || (opts.x === 0 && xanchor === 'right');
644657
var isRightOfPlotArea = opts.x > 1 || (opts.x === 1 && xanchor === 'left');
645658
var isBeyondPlotAreaY = isAbovePlotArea || isBelowPlotArea;
@@ -649,6 +662,7 @@ function computeLegendDimensions(gd, groups, traces, opts) {
649662
// - else if below/above plot area and anchored in the margin, extend to opposite margin,
650663
// - otherwise give it the maximum potential margin-push value
651664
opts._maxWidth = Math.max(
665+
isCenterOfPlotArea ? fullLayout.width :
652666
isLeftOfPlotArea ? ((isBeyondPlotAreaY && xanchor === 'left') ? gs.l + gs.w : hw) :
653667
isRightOfPlotArea ? ((isBeyondPlotAreaY && xanchor === 'right') ? gs.r + gs.w : hw) :
654668
gs.w,
@@ -668,6 +682,7 @@ function computeLegendDimensions(gd, groups, traces, opts) {
668682
var maxGroupHeightInRow = 0;
669683
var groupOffsetX = 0;
670684
var groupOffsetY = 0;
685+
var next = 0;
671686
groups.each(function() {
672687
var maxWidthInGroup = 0;
673688
var offsetY = 0;
@@ -682,7 +697,13 @@ function computeLegendDimensions(gd, groups, traces, opts) {
682697
});
683698
maxGroupHeightInRow = Math.max(maxGroupHeightInRow, offsetY);
684699

685-
var next = maxWidthInGroup + itemGap;
700+
if (yanchor === 'top') {
701+
next = fullLayout.width / 3.99;
702+
} else if (yanchor === 'bottom') {
703+
next = maxWidthInGroup + itemGap;
704+
} else {
705+
next = maxWidthInGroup + itemGap;
706+
}
686707

687708
if((next + bw + groupOffsetX) > opts._maxWidth) {
688709
maxRowWidth = Math.max(maxRowWidth, groupOffsetX);

0 commit comments

Comments
 (0)