Skip to content

Commit

Permalink
make ordered category algo skip over visible false traces
Browse files Browse the repository at this point in the history
  • Loading branch information
etpinard committed Mar 20, 2018
1 parent 6e0b63a commit 463d7ce
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
17 changes: 5 additions & 12 deletions src/plots/cartesian/ordered_categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,25 @@ var d3 = require('d3');

// flattenUniqueSort :: String -> Function -> [[String]] -> [String]
function flattenUniqueSort(axisLetter, sortFunction, data) {

// Bisection based insertion sort of distinct values for logarithmic time complexity.
// Can't use a hashmap, which is O(1), because ES5 maps coerce keys to strings. If it ever becomes a bottleneck,
// code can be separated: a hashmap (JS object) based version if all values encountered are strings; and
// downgrading to this O(log(n)) array on the first encounter of a non-string value.

var categoryArray = [];

var traceLines = data.map(function(d) {return d[axisLetter];});

var i, j, tracePoints, category, insertionIndex;

var bisector = d3.bisector(sortFunction).left;

for(i = 0; i < traceLines.length; i++) {

tracePoints = traceLines[i];

for(j = 0; j < tracePoints.length; j++) {
for(var i = 0; i < traceLines.length; i++) {
var tracePoints = traceLines[i] || [];

category = tracePoints[j];
for(var j = 0; j < tracePoints.length; j++) {
var category = tracePoints[j];

// skip loop: ignore null and undefined categories
if(category === null || category === undefined) continue;

insertionIndex = bisector(categoryArray, category);
var insertionIndex = bisector(categoryArray, category);

// skip loop on already encountered values
if(insertionIndex < categoryArray.length && categoryArray[insertionIndex] === category) continue;
Expand Down
21 changes: 21 additions & 0 deletions test/jasmine/tests/calcdata_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,27 @@ describe('calculated data and points', function() {

expect(gd._fullLayout.xaxis._categories).toEqual(['1']);
});

it('should skip over visible-false traces', function() {
Plotly.plot(gd, [{
x: [1, 2, 3],
y: [7, 6, 5],
visible: false
}, {
x: [10, 9, 8],
y: ['A', 'B', 'C'],
yaxis: 'y2'
}], {
yaxis2: {
categoryorder: 'category descending'
}
});

expect(gd.calcdata[1][0]).toEqual(jasmine.objectContaining({x: 10, y: 2}));
expect(gd.calcdata[1][1]).toEqual(jasmine.objectContaining({x: 9, y: 1}));
expect(gd.calcdata[1][2]).toEqual(jasmine.objectContaining({x: 8, y: 0}));
expect(gd._fullLayout.yaxis2._categories).toEqual(['C', 'B', 'A']);
});
});

describe('explicit category ordering', function() {
Expand Down

0 comments on commit 463d7ce

Please sign in to comment.