Skip to content

Commit 6f01227

Browse files
authored
Merge pull request #6324 from plotly/complete-legend-entrywidth
Complete legend `entrywidth` feature
2 parents bd1d6bd + 8a926aa commit 6f01227

File tree

5 files changed

+17
-16
lines changed

5 files changed

+17
-16
lines changed

draftlogs/6324_add.md

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

src/components/legend/attributes.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ module.exports = {
7878
valType: 'number',
7979
min: 0,
8080
editType: 'legend',
81-
description: 'Sets the width (in px or fraction) of the legend.',
81+
description: [
82+
'Sets the width (in px or fraction) of the legend.',
83+
'Use 0 to size the entry based on the text width,',
84+
'when `entrywidthmode` is set to *pixels*.'
85+
].join(' ')
8286
},
8387
entrywidthmode: {
8488
valType: 'enumerated',

src/components/legend/draw.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -351,21 +351,16 @@ function _draw(gd, legendObj) {
351351
}], gd);
352352
}
353353

354-
function getTraceWidth(trace, legendObj, textGap) {
355-
var legendItem = trace[0];
354+
function getTraceWidth(d, legendObj, textGap) {
355+
var legendItem = d[0];
356356
var legendWidth = legendItem.width;
357+
var mode = legendObj.entrywidthmode;
357358

358359
var traceLegendWidth = legendItem.trace.legendwidth || legendObj.entrywidth;
359360

360-
if(traceLegendWidth) {
361-
if(legendObj.entrywidthmode === 'pixels') {
362-
return traceLegendWidth + textGap;
363-
} else {
364-
return legendObj._maxWidth * traceLegendWidth;
365-
}
366-
}
361+
if(mode === 'fraction') return legendObj._maxWidth * traceLegendWidth;
367362

368-
return legendWidth + textGap;
363+
return textGap + (traceLegendWidth || legendWidth);
369364
}
370365

371366
function clickOrDoubleClick(gd, legend, legendItem, numClicks, evt) {
@@ -641,6 +636,7 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
641636

642637
var isVertical = helpers.isVertical(legendObj);
643638
var isGrouped = helpers.isGrouped(legendObj);
639+
var isFraction = legendObj.entrywidthmode === 'fraction';
644640

645641
var bw = legendObj.borderwidth;
646642
var bw2 = 2 * bw;
@@ -772,7 +768,7 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
772768
var w = getTraceWidth(d, legendObj, textGap, isGrouped);
773769
var next = (oneRowLegend ? w : maxItemWidth);
774770

775-
if(legendObj.entrywidthmode !== 'fraction') {
771+
if(!isFraction) {
776772
next += itemGap;
777773
}
778774

@@ -831,7 +827,7 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
831827
traceWidth = legendGroupWidths[legendgroup];
832828
}
833829
var w = isEditable ? textGap : (toggleRectWidth || traceWidth);
834-
if(!isVertical && legendObj.entrywidthmode !== 'fraction') {
830+
if(!isVertical && !isFraction) {
835831
w += itemGap / 2;
836832
}
837833
Drawing.setRect(traceToggle, 0, -h / 2, w, h);

test/jasmine/tests/legend_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2393,7 +2393,7 @@ describe('legend with custom legendwidth', function() {
23932393
});
23942394

23952395
it('should change width when legend has entrywidth', function(done) {
2396-
var extendedLayout = Lib.extendDeep([], layout);
2396+
var extendedLayout = Lib.extendDeep({}, layout);
23972397
var width = 50;
23982398
extendedLayout.legend.entrywidth = width;
23992399

@@ -2421,7 +2421,7 @@ describe('legend with custom legendwidth', function() {
24212421
});
24222422

24232423
it('should change width when legend has entrywidth and entrywidthmode is fraction', function(done) {
2424-
var extendedLayout = Lib.extendDeep([], layout);
2424+
var extendedLayout = Lib.extendDeep({}, layout);
24252425
extendedLayout.legend.entrywidthmode = 'fraction';
24262426
extendedLayout.legend.entrywidth = 0.3;
24272427

test/plot-schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2756,7 +2756,7 @@
27562756
},
27572757
"editType": "legend",
27582758
"entrywidth": {
2759-
"description": "Sets the width (in px or fraction) of the legend.",
2759+
"description": "Sets the width (in px or fraction) of the legend. Use 0 to size the entry based on the text width, when `entrywidthmode` is set to *pixels*.",
27602760
"editType": "legend",
27612761
"min": 0,
27622762
"valType": "number"

0 commit comments

Comments
 (0)