Skip to content

Commit

Permalink
Merge pull request #6324 from plotly/complete-legend-entrywidth
Browse files Browse the repository at this point in the history
Complete legend `entrywidth` feature
  • Loading branch information
archmoj authored Sep 23, 2022
2 parents bd1d6bd + 8a926aa commit 6f01227
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
1 change: 1 addition & 0 deletions draftlogs/6324_add.md
Original file line number Diff line number Diff line change
@@ -0,0 +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)]
6 changes: 5 additions & 1 deletion src/components/legend/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ module.exports = {
valType: 'number',
min: 0,
editType: 'legend',
description: 'Sets the width (in px or fraction) of the legend.',
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*.'
].join(' ')
},
entrywidthmode: {
valType: 'enumerated',
Expand Down
20 changes: 8 additions & 12 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,21 +351,16 @@ function _draw(gd, legendObj) {
}], gd);
}

function getTraceWidth(trace, legendObj, textGap) {
var legendItem = trace[0];
function getTraceWidth(d, legendObj, textGap) {
var legendItem = d[0];
var legendWidth = legendItem.width;
var mode = legendObj.entrywidthmode;

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

if(traceLegendWidth) {
if(legendObj.entrywidthmode === 'pixels') {
return traceLegendWidth + textGap;
} else {
return legendObj._maxWidth * traceLegendWidth;
}
}
if(mode === 'fraction') return legendObj._maxWidth * traceLegendWidth;

return legendWidth + textGap;
return textGap + (traceLegendWidth || legendWidth);
}

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

var isVertical = helpers.isVertical(legendObj);
var isGrouped = helpers.isGrouped(legendObj);
var isFraction = legendObj.entrywidthmode === 'fraction';

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

if(legendObj.entrywidthmode !== 'fraction') {
if(!isFraction) {
next += itemGap;
}

Expand Down Expand Up @@ -831,7 +827,7 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
traceWidth = legendGroupWidths[legendgroup];
}
var w = isEditable ? textGap : (toggleRectWidth || traceWidth);
if(!isVertical && legendObj.entrywidthmode !== 'fraction') {
if(!isVertical && !isFraction) {
w += itemGap / 2;
}
Drawing.setRect(traceToggle, 0, -h / 2, w, h);
Expand Down
4 changes: 2 additions & 2 deletions test/jasmine/tests/legend_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2393,7 +2393,7 @@ describe('legend with custom legendwidth', function() {
});

it('should change width when legend has entrywidth', function(done) {
var extendedLayout = Lib.extendDeep([], layout);
var extendedLayout = Lib.extendDeep({}, layout);
var width = 50;
extendedLayout.legend.entrywidth = width;

Expand Down Expand Up @@ -2421,7 +2421,7 @@ describe('legend with custom legendwidth', function() {
});

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

Expand Down
2 changes: 1 addition & 1 deletion test/plot-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2756,7 +2756,7 @@
},
"editType": "legend",
"entrywidth": {
"description": "Sets the width (in px or fraction) of the legend.",
"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*.",
"editType": "legend",
"min": 0,
"valType": "number"
Expand Down

0 comments on commit 6f01227

Please sign in to comment.