Skip to content

Commit 9069e34

Browse files
authored
Merge pull request #6874 from my-tien/indentation
legend indentation
2 parents 0291f77 + cedc0c6 commit 9069e34

File tree

8 files changed

+99
-8
lines changed

8 files changed

+99
-8
lines changed

draftlogs/6874_add.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Add `indentation` to legend [[#6874](https://github.com/plotly/plotly.js/pull/6874)]

src/components/legend/attributes.js

+7
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: -15,
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

+1
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

+4-4
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,
@@ -765,7 +765,7 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
765765
var bw = legendObj.borderwidth;
766766
var bw2 = 2 * bw;
767767
var itemGap = constants.itemGap;
768-
var textGap = legendObj.itemwidth + itemGap * 2;
768+
var textGap = legendObj.indentation + legendObj.itemwidth + itemGap * 2;
769769
var endPad = 2 * (bw + itemGap);
770770

771771
var yanchor = getYanchor(legendObj);

src/components/legend/style.js

+5-4
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
36.4 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"data": [{
3+
"y": [1, 5, 3, 4, 5],
4+
"name": "Scatter",
5+
"type": "scatter",
6+
"showlegend": true
7+
},
8+
{
9+
"y": [1, 2, 2, 4, 3],
10+
"name": "Bar",
11+
"type": "bar",
12+
"showlegend": true
13+
},
14+
{
15+
"y": [1, 3, 2, 5, 5],
16+
"name": "Scatter 2",
17+
"type": "scatter",
18+
"showlegend": true,
19+
"legend": "legend2"
20+
},
21+
{
22+
"y": [1, 2, 3, 4, 5],
23+
"name": "Bar 2",
24+
"type": "bar",
25+
"showlegend": true,
26+
"legend": "legend2"
27+
},
28+
{
29+
"y": [5, 2, 3, 1, 4],
30+
"name": "Bar 3",
31+
"type": "bar",
32+
"showlegend": true,
33+
"legend": "legend3"
34+
},
35+
{
36+
"y": [2, 1, 3, 4, 4],
37+
"name": "Scatter 3",
38+
"type": "scatter",
39+
"showlegend": true,
40+
"legend": "legend3"
41+
}
42+
],
43+
"layout": {
44+
"title": { "text": "Legend Indentation" },
45+
"width": 400,
46+
"legend": {
47+
"bgcolor": "lightblue",
48+
"y": 1,
49+
"indentation": 0,
50+
"title": { "text": "No indent" },
51+
"font": {
52+
"size": 10
53+
}
54+
},
55+
"legend2": {
56+
"bgcolor": "lightgreen",
57+
"y": 0.5,
58+
"indentation": 30,
59+
"title": { "text": "indent 30" },
60+
"font": {
61+
"size": 10
62+
}
63+
},
64+
"legend3": {
65+
"bgcolor": "pink",
66+
"y": 0,
67+
"indentation": -15,
68+
"title": { "text": "indent -15" },
69+
"font": {
70+
"size": 10
71+
}
72+
}
73+
}
74+
}

test/plot-schema.json

+7
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": -15,
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)