Skip to content

Commit

Permalink
Merge pull request #148 from mhkeller/sort-ordinal-scales
Browse files Browse the repository at this point in the history
Sort uniques used for ordinal domain
  • Loading branch information
mhkeller authored Sep 20, 2023
2 parents 76b2d37 + 553f38f commit 6781953
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
===

# 8.0.0

> 2023-09-19
When calculating uniques for an ordinal scale's domain, sort the list. This will help in case your input data changes order, in which case your colors may be off (if using that dimension for a color scale). This is technically a breaking change although it may not actually affect any live charts.

* [PR#142](https://github.com/mhkeller/layercake/pull/142)

# 7.6.1

> 2023-07-19
Expand Down
4 changes: 2 additions & 2 deletions src/lib/helpers/calcScaleExtents.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export default function calcScaleExtents (flatData, getters, activeScales) {
let extents = {};
if (scaleGroups.ordinal) {
// @ts-ignore
extents = calcUniques(flatData, scaleGroups.ordinal)
extents = calcUniques(flatData, scaleGroups.ordinal, { sort: true });
}
if (scaleGroups.other) {
// @ts-ignore
extents = { ...extents, ...calcExtents(flatData, scaleGroups.other) }
extents = { ...extents, ...calcExtents(flatData, scaleGroups.other) };
}

return extents;
Expand Down
5 changes: 4 additions & 1 deletion src/lib/lib/calcUniques.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export default function calcUniques (data, fields, { sort = false } = {}) {
}
}
const results = Array.from(set);
uniques[s] = sort === true ? results.sort() : results;
if (sort === true) {
results.sort();
}
uniques[s] = results;
}
return uniques;
}
14 changes: 14 additions & 0 deletions test/calcScaleExtents.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ const tests = [
{ x: scaleSqrt(), y: scalePoint() }
], expected: { x: [0, 3], y: ['0', '1', '2', '3'] }
},
{
args: [
[{ mx: 0, my: '1' }, { mx: 1, my: '0' }, { mx: 2, my: '2' }, { mx: 3, my: '3' }],
{ x: d => d.mx, y: d => d.my },
{ x: scaleSqrt(), y: scalePoint() }
], expected: { x: [0, 3], y: ['0', '1', '2', '3'] }
},
{
args: [
[{ mx: 0, my: '1' }, { mx: 1, my: '0' }, { mx: 2, my: '2' }, { mx: 3, my: '3' }],
{ x: d => d.mx, y: d => d.my },
{ x: scaleSqrt(), y: scalePoint() }
], expected: { x: [0, 3], y: ['0', '1', '2', '3'] }
},
{
args: [
[{ mx: 0, my: '0' }, { mx: 1, my: '1' }, { mx: 2, my: '2' }, { mx: 3, my: '3' }],
Expand Down

0 comments on commit 6781953

Please sign in to comment.