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
Prev Previous commit
Next Next commit
rank groups based on minimum legendranks within each group
  • Loading branch information
archmoj committed Jun 16, 2021
commit 32a890c9bf513155820857ff840a229051ac2588
37 changes: 29 additions & 8 deletions src/components/legend/get_legend_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ module.exports = function getLegendData(calcdata, opts) {
// collapse all groups into one if all groups are blank
var shouldCollapse = !hasOneNonBlankGroup || !grouped;

// rearrange lgroupToTraces into a d3-friendly array of arrays
var legendData;

legendData = [];
var legendData = [];
for(i = 0; i < lgroups.length; i++) {
var t = lgroupToTraces[lgroups[i]];
if(shouldCollapse) {
Expand All @@ -86,19 +83,43 @@ module.exports = function getLegendData(calcdata, opts) {
}
if(shouldCollapse) legendData = [legendData];

var orderFn = function(a, b) {
for(i = 0; i < legendData.length; i++) {
// find minimum rank within group
var groupMinRank = Infinity;
for(j = 0; j < legendData[i].length; j++) {
var rank = legendData[i][j].trace.legendrank;
if(groupMinRank > rank) groupMinRank = rank;
}

// record on first group element
legendData[i][0]._groupMinRank = groupMinRank;
legendData[i][0]._preGroupSort = i;
}

var orderFn1 = function(a, b) {
return (
(a[0]._groupMinRank - b[0]._groupMinRank) ||
(a[0]._preGroupSort - b[0]._preGroupSort) // fallback for old Chrome < 70 https://bugs.chromium.org/p/v8/issues/detail?id=90
);
};

var orderFn2 = function(a, b) {
return (
(a.trace.legendrank - b.trace.legendrank) ||
(a._preSort - b._preSort) // fallback for old Chrome < 70 https://bugs.chromium.org/p/v8/issues/detail?id=90
);
};

// sort considering minimum group legendrank
legendData.forEach(function(a, k) { a[0]._preGroupSort = k; });
legendData.sort(orderFn1);
for(i = 0; i < legendData.length; i++) {
// sort considering trace.legendrank and legend.traceorder
legendData[i].forEach(function(a, i) { a._preSort = i; });
legendData[i].sort(orderFn);
legendData[i].forEach(function(a, k) { a._preSort = k; });
legendData[i].sort(orderFn2);
if(reversed) legendData[i].reverse();

// add extra dim
// rearrange lgroupToTraces into a d3-friendly array of arrays
for(j = 0; j < legendData[i].length; j++) {
legendData[i][j] = [
legendData[i][j]
Expand Down
24 changes: 12 additions & 12 deletions test/jasmine/tests/legend_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ describe('legend getLegendData user-defined legendrank', function() {
legendgroup: 'group',
showlegend: true
}}],
[{_preSort: 0, trace: {
[{_groupMinRank: 1, _preGroupSort: 0, _preSort: 0, trace: {
legendrank: 3,
type: 'scatter',
visible: true,
Expand All @@ -301,7 +301,7 @@ describe('legend getLegendData user-defined legendrank', function() {
}}]
],
[
[{_preSort: 0, trace: {
[{_groupMinRank: 2, _preGroupSort: 1, _preSort: 0, trace: {
legendrank: 2,
type: 'bar',
visible: 'legendonly',
Expand Down Expand Up @@ -361,7 +361,7 @@ describe('legend getLegendData user-defined legendrank', function() {
legendgroup: '',
showlegend: true
}}],
[{_preSort: 0, trace: {
[{_groupMinRank: 1, _preGroupSort: 0, _preSort: 0, trace: {
legendrank: 3,
type: 'scatter',
visible: true,
Expand Down Expand Up @@ -439,7 +439,7 @@ describe('legend getLegendData user-defined legendrank', function() {

expected = [
[
[{_preSort: 0, trace: {
[{_groupMinRank: 1, _preGroupSort: 0, _preSort: 0, trace: {
legendrank: 3,
type: 'scatter',
visible: true,
Expand Down Expand Up @@ -499,7 +499,7 @@ describe('legend getLegendData user-defined legendrank', function() {

expected = [
[
[{_preSort: 0, trace: {
[{_groupMinRank: 1, _preGroupSort: 0, _preSort: 0, trace: {
legendrank: 3,
type: 'scatter',
visible: true,
Expand All @@ -515,7 +515,7 @@ describe('legend getLegendData user-defined legendrank', function() {
}}]
],
[
[{_preSort: 0, trace: {
[{_groupMinRank: 2, _preGroupSort: 1, _preSort: 0, trace: {
legendrank: 2,
type: 'bar',
visible: 'legendonly',
Expand Down Expand Up @@ -564,7 +564,7 @@ describe('legend getLegendData default legendrank', function() {

expected = [
[
[{_preSort: 0, trace: {
[{_groupMinRank: Infinity, _preGroupSort: 0, _preSort: 0, trace: {
type: 'scatter',
visible: true,
legendgroup: 'group',
Expand All @@ -578,7 +578,7 @@ describe('legend getLegendData default legendrank', function() {
}}]
],
[
[{_preSort: 0, trace: {
[{_groupMinRank: Infinity, _preGroupSort: 1, _preSort: 0, trace: {
type: 'bar',
visible: 'legendonly',
legendgroup: '',
Expand Down Expand Up @@ -620,7 +620,7 @@ describe('legend getLegendData default legendrank', function() {

expected = [
[
[{_preSort: 0, trace: {
[{_groupMinRank: Infinity, _preGroupSort: 0, _preSort: 0, trace: {
type: 'scatter',
visible: true,
legendgroup: '',
Expand Down Expand Up @@ -715,7 +715,7 @@ describe('legend getLegendData default legendrank', function() {
legendgroup: '',
showlegend: true
}}],
[{_preSort: 0, trace: {
[{_groupMinRank: Infinity, _preGroupSort: 0, _preSort: 0, trace: {
type: 'scatter',
visible: true,
legendgroup: '',
Expand Down Expand Up @@ -763,15 +763,15 @@ describe('legend getLegendData default legendrank', function() {
legendgroup: 'group',
showlegend: true
}}],
[{_preSort: 0, trace: {
[{_groupMinRank: Infinity, _preGroupSort: 0, _preSort: 0, trace: {
type: 'scatter',
visible: true,
legendgroup: 'group',
showlegend: true
}}]
],
[
[{_preSort: 0, trace: {
[{_groupMinRank: Infinity, _preGroupSort: 1, _preSort: 0, trace: {
type: 'bar',
visible: 'legendonly',
legendgroup: '',
Expand Down
0