Skip to content
Open
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/7959_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix numeric color sorting for bundled parallel-categories paths [[#7959](https://github.com/plotly/plotly.js/pull/7959)]
23 changes: 14 additions & 9 deletions src/traces/parcats/parcats.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,18 @@ function compareRawColor(a, b) {
}
}

function compareArrays(a, b) {
for(var i = 0; i < Math.min(a.length, b.length); i++) {
if(a[i] < b[i]) {
return -1;
} else if(a[i] > b[i]) {
return 1;
}
}

return a.length - b.length;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of adding a JSDoc comment and a block to handle NaN values? This likely wouldn't happen in practice, but it could.

Suggested change
}
/**
* Compare two sort arrays element by element in ascending order.
* Values that do not order against each other, for example NaN, sort last.
* The shorter array sorts first when one array is a prefix of the other.
*
* @param {Array} a
* @param {Array} b
*/
function compareArrays(a, b) {
for (let i = 0; i < Math.min(a.length, b.length); i++) {
const valA = a[i];
const valB = b[i];
if (valA < valB) return -1;
if (valA > valB) return 1;
// Handle values that do not order against each other (NaN, undefined, etc.)
if (valA !== valB) {
// Sort these after every orderable value.
const badA = isNaN(valA);
const badB = isNaN(valB);
if (badA !== badB) return badA ? 1 : -1;
}
}
return a.length - b.length;
}


/**
* Handle path mouseover
* @param {PathViewModel} d
Expand Down Expand Up @@ -1734,15 +1746,8 @@ function updatePathViewModels(parcatsViewModel) {
sortArray2.unshift(v2.rawColor);
}

// colors equal, sort by display categories
if(sortArray1 < sortArray2) {
return -1;
}
if(sortArray1 > sortArray2) {
return 1;
}

return 0;
// Sort by color, then display categories
return compareArrays(sortArray1, sortArray2);
});

// Create path models
Expand Down
23 changes: 23 additions & 0 deletions test/jasmine/tests/parcats_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,29 @@ describe('Basic parcats trace', function() {
.then(done, done.fail);
});

it('should sort bundled paths by numeric color values', function(done) {
var trace = {
type: 'parcats',
dimensions: [
{values: ['a', 'a', 'a', 'a']},
{values: ['b', 'b', 'b', 'b']}
],
line: {color: [1, 10, 2, 20]},
bundlecolors: true
};

Plotly.newPlot(gd, [trace])
.then(function() {
var parcatsViewModel = d3Select('g.trace.parcats').datum();
var pathColors = parcatsViewModel.paths.map(function(path) {
return path.model.rawColor;
});

expect(pathColors).toEqual([1, 2, 10, 20]);
})
.then(done, done.fail);
});

it('should compute initial model views properly', function(done) {
Plotly.newPlot(gd, basicMock)
.then(function() {
Expand Down
Loading