Skip to content

Commit 46278f5

Browse files
authored
Merge pull request #6711 from 28raining/center-legend
Adding horixontal alignment for horizontal legend title
2 parents 9a039bd + e2d0bae commit 46278f5

9 files changed

+17
-7
lines changed

Diff for: src/components/legend/attributes.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,15 @@ module.exports = {
267267
}),
268268
side: {
269269
valType: 'enumerated',
270-
values: ['top', 'left', 'top left'],
270+
values: ['top', 'left', 'top left', 'top center', 'top right'],
271271
editType: 'legend',
272272
description: [
273273
'Determines the location of legend\'s title',
274274
'with respect to the legend items.',
275275
'Defaulted to *top* with `orientation` is *h*.',
276276
'Defaulted to *left* with `orientation` is *v*.',
277277
'The *top left* options could be used to expand',
278+
'top center and top right are for horizontal alignment',
278279
'legend area in both x and y sides.'
279280
].join(' ')
280281
},

Diff for: src/components/legend/draw.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -661,13 +661,18 @@ function computeTextDimensions(g, gd, legendObj, aTitle) {
661661
// approximation to height offset to center the font
662662
// to avoid getBoundingClientRect
663663
if(aTitle === MAIN_TITLE) {
664+
var titleOffset = 0;
664665
if(legendObj.title.side === 'left') {
665666
// add extra space between legend title and itmes
666667
width += constants.itemGap * 2;
668+
} else if(legendObj.title.side === 'top center') {
669+
if(legendObj._width) titleOffset = 0.5 * (legendObj._width - 2 * bw - 2 * constants.titlePad - width);
670+
} else if(legendObj.title.side === 'top right') {
671+
if(legendObj._width) titleOffset = legendObj._width - 2 * bw - 2 * constants.titlePad - width;
667672
}
668673

669674
svgTextUtils.positionText(textEl,
670-
bw + constants.titlePad,
675+
bw + constants.titlePad + titleOffset,
671676
bw + lineHeight
672677
);
673678
} else { // legend item

Diff for: test/image/baselines/legend_horizontal_autowrap.png

-80 Bytes
Loading

Diff for: test/image/baselines/legend_horizontal_one_row.png

-5.25 KB
Loading

Diff for: test/image/baselines/legend_itemwidth_dashline.png

510 Bytes
Loading

Diff for: test/image/mocks/legend_horizontal_autowrap.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,8 @@
547547
"size": 11.9552
548548
},
549549
"y": -0.4,
550-
"orientation": "h"
550+
"orientation": "h",
551+
"title": { "text": "Legend Title", "side": "top right" }
551552
},
552553
"hovermode": "closest",
553554
"width": 680,

Diff for: test/image/mocks/legend_horizontal_one_row.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
],
2222
"layout": {
2323
"title": {"text": "Average Distribution per Month"},
24-
"legend": { "orientation": "h" }
24+
"legend": { "orientation": "h", "title": { "text": "Legend Title", "side": "top center" }}
2525
}
2626
}

Diff for: test/image/mocks/legend_itemwidth_dashline.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
],
4040
"layout": {
4141
"legend": {
42-
"itemwidth": 60
42+
"itemwidth": 60,
43+
"title": { "text": "Title", "side": "top center" }
4344
}
4445
}
4546
}

Diff for: test/plot-schema.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -2923,13 +2923,15 @@
29232923
},
29242924
"role": "object",
29252925
"side": {
2926-
"description": "Determines the location of legend's title with respect to the legend items. Defaulted to *top* with `orientation` is *h*. Defaulted to *left* with `orientation` is *v*. The *top left* options could be used to expand legend area in both x and y sides.",
2926+
"description": "Determines the location of legend's title with respect to the legend items. Defaulted to *top* with `orientation` is *h*. Defaulted to *left* with `orientation` is *v*. The *top left* options could be used to expand top center and top right are for horizontal alignment legend area in both x and y sides.",
29272927
"editType": "legend",
29282928
"valType": "enumerated",
29292929
"values": [
29302930
"top",
29312931
"left",
2932-
"top left"
2932+
"top left",
2933+
"top center",
2934+
"top right"
29332935
]
29342936
},
29352937
"text": {

0 commit comments

Comments
 (0)