Skip to content

Commit a657457

Browse files
committed
sort ordinal domains by "ascending", null (or "input"), "count"; defaults to "ascending".
related to #388
1 parent 87e39a2 commit a657457

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/scales/ordinal.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {InternSet, reverse as reverseof, sort} from "d3";
1+
import {InternMap, InternSet, reverse as reverseof, sort as sorter} from "d3";
22
import {quantize} from "d3";
33
import {scaleBand, scaleOrdinal, scalePoint} from "d3";
44
import {
@@ -180,7 +180,8 @@ function Scheme(scheme) {
180180
}
181181

182182
export function ScaleO(scale, channels, {
183-
domain = inferDomain(channels),
183+
sort,
184+
domain = inferDomain(channels, sort),
184185
range,
185186
reverse,
186187
inset
@@ -245,11 +246,23 @@ function maybeRound(scale, channels, options = {}) {
245246
return scale;
246247
}
247248

248-
function inferDomain(channels) {
249+
function inferDomain(channels, sort = "ascending") {
249250
const domain = new InternSet();
251+
if (sort === "count") {
252+
const counts = new InternMap();
253+
for (const {value} of channels) {
254+
if (value === undefined) continue;
255+
for (const v of value) {
256+
domain.add(v);
257+
counts.set(v, counts.has(v) ? counts.get(v) + 1 : 1);
258+
}
259+
}
260+
return sort(domain, key => counts.get(key));
261+
}
250262
for (const {value} of channels) {
251263
if (value === undefined) continue;
252264
for (const v of value) domain.add(v);
253265
}
254-
return sort(domain, ascendingDefined);
266+
if (sort === "ascending") return sorter(domain, ascendingDefined);
267+
return domain;
255268
}

0 commit comments

Comments
 (0)