Skip to content

Commit

Permalink
Merge pull request #979 from dlowder-salesforce/dlowder/sortedDomain
Browse files Browse the repository at this point in the history
Fix #973 (Legend ordering)
  • Loading branch information
jameshadfield authored Mar 24, 2020
2 parents f9c8ad2 + 43da507 commit 13b2c39
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
9 changes: 2 additions & 7 deletions src/util/colorScale.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getExtraVals } from "./colorHelpers";
import { isColorByGenotype, decodeColorByGenotype } from "./getGenotype";
import { setGenotype, orderOfGenotypeAppearance } from "./setGenotype";
import { getTraitFromNode } from "./treeMiscHelpers";
import { sortedDomain } from "./sortedDomain";

const unknownColor = "#AAAAAA";

Expand Down Expand Up @@ -39,13 +40,7 @@ const getDiscreteValuesFromTree = (nodes, nodesToo, attr) => {
stateCount.set(state, currentCount+1);
}
}
const domain = Array.from(stateCount.keys()).filter((x) => isValueValid(x));
/* sorting technique depends on the colorBy */
if (attr === "clade_membership") {
domain.sort();
} else {
domain.sort((a, b) => stateCount[a] > stateCount[b] ? 1 : -1);
}
const domain = sortedDomain(Array.from(stateCount.keys()).filter((x) => isValueValid(x)), attr, stateCount);
return domain;
};

Expand Down
15 changes: 15 additions & 0 deletions src/util/sortedDomain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const sortedDomain = (domain, attr, stateCount) => {
/* sorting technique depends on the colorBy */
const sorted = Array.from(domain);
if (attr === "clade_membership") {
sorted.sort();
} else {
sorted.sort(
(a, b) =>
stateCount.get(a) === stateCount.get(b)
? a < b ? -1 : 1
: stateCount.get(a) > stateCount.get(b) ? -1 : 1
);
}
return sorted;
};
67 changes: 67 additions & 0 deletions test/sortedDomain.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { sortedDomain } from "../src/util/sortedDomain";

test("sortedDomain works correctly in normal case", () => {
const stateCount = new Map([
["Italy", 8],
["Lombardy", 9],
["USA", 1],
["Iran", 18],
["Comunitat Valenciana", 1],
["Hubei", 35],
["China", 2],
["Grand Princess", 2],
["Hong Kong", 1],
["South Korea", 1],
["Europe", 1],
["UK", 1]
]);
const domain = stateCount.keys();
const sorted = sortedDomain(domain, "", stateCount);
expect(sorted).toMatchObject([
"Hubei",
"Iran",
"Lombardy",
"Italy",
"China",
"Grand Princess",
"Comunitat Valenciana",
"Europe",
"Hong Kong",
"South Korea",
"UK",
"USA"
]);
});

test("sortedDomain works correctly in special case", () => {
const stateCount = new Map([
["Italy", 8],
["Lombardy", 9],
["USA", 1],
["Iran", 18],
["Comunitat Valenciana", 1],
["Hubei", 35],
["China", 2],
["Grand Princess", 2],
["Hong Kong", 1],
["South Korea", 1],
["Europe", 1],
["UK", 1]
]);
const domain = stateCount.keys();
const sorted = sortedDomain(domain, "clade_membership", stateCount);
expect(sorted).toMatchObject([
"China",
"Comunitat Valenciana",
"Europe",
"Grand Princess",
"Hong Kong",
"Hubei",
"Iran",
"Italy",
"Lombardy",
"South Korea",
"UK",
"USA"
]);
});

0 comments on commit 13b2c39

Please sign in to comment.