Skip to content

Commit e313945

Browse files
committed
layout.legend.indentation property for indenting the legend entries relative to the title
1 parent 259abab commit e313945

File tree

7 files changed

+81
-7
lines changed

7 files changed

+81
-7
lines changed

src/components/legend/attributes.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ module.exports = {
105105
editType: 'legend',
106106
description: 'Determines what entrywidth means.',
107107
},
108+
indentation: {
109+
valType: 'number',
110+
min: 0,
111+
dflt: 0,
112+
editType: 'legend',
113+
description: 'Sets the indentation (in px) of the legend entries.',
114+
},
108115
itemsizing: {
109116
valType: 'enumerated',
110117
values: ['trace', 'constant'],

src/components/legend/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ function groupDefaults(legendId, layoutIn, layoutOut, fullData) {
178178

179179
coerce('entrywidth');
180180
coerce('entrywidthmode');
181+
coerce('indentation');
181182
coerce('itemsizing');
182183
coerce('itemwidth');
183184

src/components/legend/draw.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ function drawTexts(g, gd, legendObj) {
541541
.call(Drawing.font, font)
542542
.text(isEditable ? ensureLength(name, maxNameLength) : name);
543543

544-
var textGap = legendObj.itemwidth + constants.itemGap * 2;
544+
var textGap = legendObj.indentation + legendObj.itemwidth + constants.itemGap * 2;
545545
svgTextUtils.positionText(textEl, textGap, 0);
546546

547547
if(isEditable) {
@@ -700,10 +700,10 @@ function computeTextDimensions(g, gd, legendObj, aTitle) {
700700
bw + lineHeight
701701
);
702702
} else { // legend item
703-
var x = constants.itemGap * 2 + legendObj.itemwidth;
703+
var x = constants.itemGap * 2 + legendObj.indentation + legendObj.itemwidth;
704704
if(legendItem.groupTitle) {
705705
x = constants.itemGap;
706-
width -= legendObj.itemwidth;
706+
width -= legendObj.indentation + legendObj.itemwidth;
707707
}
708708

709709
svgTextUtils.positionText(textEl,

src/components/legend/style.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,17 @@ module.exports = function style(s, gd, legend) {
4747
var layers = Lib.ensureSingle(traceGroup, 'g', 'layers');
4848
layers.style('opacity', d[0].trace.opacity);
4949

50+
var indentation = legend.indentation;
5051
var valign = legend.valign;
5152
var lineHeight = d[0].lineHeight;
5253
var height = d[0].height;
53-
54-
if(valign === 'middle' || !lineHeight || !height) {
54+
if((valign === 'middle' && indentation === 0) || !lineHeight || !height) {
5555
layers.attr('transform', null);
5656
} else {
5757
var factor = {top: 1, bottom: -1}[valign];
58-
var markerOffsetY = factor * (0.5 * (lineHeight - height + 3));
59-
layers.attr('transform', strTranslate(0, markerOffsetY));
58+
var markerOffsetY = (factor * (0.5 * (lineHeight - height + 3))) || 0;
59+
var markerOffsetX = legend.indentation;
60+
layers.attr('transform', strTranslate(markerOffsetX, markerOffsetY));
6061
}
6162

6263
var fill = layers
50.4 KB
Loading
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"data": [{
3+
"y": [1, 5, 3, 4, 5],
4+
"name": "Scatter",
5+
"type": "scatter",
6+
"showlegend": true
7+
},
8+
{
9+
"y": [3, 2, 5, 1, 5],
10+
"name": "Scatter2",
11+
"type": "scatter",
12+
"showlegend": true
13+
},
14+
{
15+
"type": "pie",
16+
"labels": ["a", "b", "c", "d"],
17+
"values": [10, 15, 13, 17],
18+
"domain": {"x": [0.5, 1]}
19+
},
20+
{
21+
"y": [1, 3, 2, 5, 5],
22+
"name": "Scatter 3",
23+
"type": "scatter",
24+
"showlegend": true,
25+
"legend": "legend2"
26+
},
27+
{
28+
"y": [1, 2, 3, 4, 5],
29+
"name": "Bar",
30+
"type": "bar",
31+
"showlegend": true,
32+
"legend": "legend2"
33+
}
34+
],
35+
"layout": {
36+
"title": "Legend Indentation",
37+
"width": 800,
38+
"xaxis": {"domain": [0, 0.5]},
39+
"legend": {
40+
"bgcolor": "lightblue",
41+
"y": 1,
42+
"indentation": 0,
43+
"title": { "text": "No indent" },
44+
"font": {
45+
"size": 10
46+
}
47+
},
48+
"legend2": {
49+
"bgcolor": "lightgreen",
50+
"y": 0,
51+
"indentation": 30,
52+
"title": { "text": "indent 30" },
53+
"font": {
54+
"size": 10
55+
}
56+
}
57+
}
58+
}

test/plot-schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,6 +2848,13 @@
28482848
"valType": "number"
28492849
}
28502850
},
2851+
"indentation": {
2852+
"description": "Sets the indentation (in px) of the legend entries.",
2853+
"dflt": 0,
2854+
"editType": "legend",
2855+
"min": 0,
2856+
"valType": "number"
2857+
},
28512858
"itemclick": {
28522859
"description": "Determines the behavior on legend item click. *toggle* toggles the visibility of the item clicked on the graph. *toggleothers* makes the clicked item the sole visible item on the graph. *false* disables legend item click interactions.",
28532860
"dflt": "toggle",

0 commit comments

Comments
 (0)