Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete legend entrywidth feature #6324

Merged
merged 6 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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