8000 Implement legendrank attribute in traces by archmoj · Pull Request #5591 · plotly/plotly.js · GitHub
[go: up one dir, main page]

Skip to content

Implement legendrank attribute in traces #5591

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

Merged
merged 20 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
implement legendrank
  • Loading branch information
archmoj committed Apr 15, 2021
commit 3cee835e320ede161ea72c4a9c90f72901bba235
22 changes: 17 additions & 5 deletions src/components/legend/get_legend_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,33 @@ module.exports = function getLegendData(calcdata, opts) {
var ltraces;
var legendData;

// sort considering trace.legendrank and legend.traceorder
var dir = helpers.isReversed(opts) ? -1 : 1;
var orderFn = function(a, b) {
var A = a[0].trace;
var B = b[0].trace;
var delta = A.legendrank - B.legendrank;
if(!delta) delta = A.index - B.index;

return dir * delta;
};

if(hasOneNonBlankGroup && helpers.isGrouped(opts)) {
legendData = new Array(lgroupsLength);

for(i = 0; i < lgroupsLength; i++) {
ltraces = lgroupToTraces[lgroups[i]];
legendData[i] = helpers.isReversed(opts) ? ltraces.reverse() : ltraces;
legendData[i] = ltraces.sort(orderFn);
}
} else {
// collapse all groups into one if all groups are blank
legendData = [new Array(lgroupsLength)];

legendData = [[]];
for(i = 0; i < lgroupsLength; i++) {
ltraces = lgroupToTraces[lgroups[i]][0];
legendData[0][helpers.isReversed(opts) ? lgroupsLength - i - 1 : i] = ltraces;
legendData[0].push(
lgroupToTraces[lgroups[i]][0]
);
}
legendData[0] = legendData[0].sort(orderFn);
lgroupsLength = 1;
}

Expand Down
10 changes: 10 additions & 0 deletions src/plots/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ module.exports = {
'when toggling legend items.'
].join(' ')
},
legendrank: {
valType: 'number',
dflt: 1000,
editType: 'style',
description: [
'Sets the legend rank for this trace.',
'Items with smaller ranks would be presented on top/left side while',
'with `*reversed* `legend.traceorder` they would be on bottom/right side.'
].join(' ')
},
opacity: {
valType: 'number',
min: 0,
Expand Down
1 change: 1 addition & 0 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,7 @@ plots.supplyTraceDefaults = function(traceIn, traceOut, colorIndex, layout, trac
);

coerce('legendgroup');
coerce('legendrank');

traceOut._dfltShowLegend = true;
} else {
Expand Down
1 change: 1 addition & 0 deletions src/traces/parcats/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ module.exports = {
hoverlabel: undefined,
ids: undefined,
legendgroup: undefined,
legendrank: undefined,
opacity: undefined,
selectedpoints: undefined,
showlegend: undefined
Expand Down
0