Skip to content

Commit 76d4691

Browse files
Filmbostock
andauthored
expose a deduplicated ordinal domain (#1813)
* expose a deduplicated ordinal domain closes #1812 * use scale’s deduplicated domain --------- Co-authored-by: Mike Bostock <[email protected]>
1 parent b16a2be commit 76d4691

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/scales/ordinal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function createScaleO(key, scale, channels, {type, interval, domain, range, reve
1717
if (domain === undefined) domain = inferDomain(channels, interval, key);
1818
if (type === "categorical" || type === ordinalImplicit) type = "ordinal"; // shorthand for color schemes
1919
if (reverse) domain = reverseof(domain);
20-
scale.domain(domain);
20+
domain = scale.domain(domain).domain(); // deduplicate
2121
if (range !== undefined) {
2222
// If the range is specified as a function, pass it the domain.
2323
if (typeof range === "function") range = range(domain);

test/scales/scales-test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,6 +2102,36 @@ it("plot(…).scale(name).apply and invert return the expected functions", () =>
21022102
]);
21032103
});
21042104

2105+
it("plot(…).scale(name) returns a deduplicated ordinal domain", () => {
2106+
const letters = "abbbcaabbcc";
2107+
const plot = Plot.dotX(letters).plot({x: {domain: letters}});
2108+
scaleEqual(plot.scale("x"), {
2109+
align: 0.5,
2110+
bandwidth: 0,
2111+
domain: ["a", "b", "c"],
2112+
padding: 0.5,
2113+
range: [20, 620],
2114+
round: true,
2115+
step: 200,
2116+
type: "point"
2117+
});
2118+
});
2119+
2120+
it("plot(…).scale(name) returns a deduplicated ordinal/temporal domain", () => {
2121+
const dates = ["2001", "2002", "2004", "2004"].map(d3.isoParse);
2122+
const plot = Plot.dotX(dates).plot({x: {type: "point", domain: dates}});
2123+
scaleEqual(plot.scale("x"), {
2124+
align: 0.5,
2125+
bandwidth: 0,
2126+
domain: dates.slice(0, 3),
2127+
padding: 0.5,
2128+
range: [20, 620],
2129+
round: true,
2130+
step: 200,
2131+
type: "point"
2132+
});
2133+
});
2134+
21052135
// Given a plot specification (or, as shorthand, an array of marks or a single
21062136
// mark), asserts that the given named scales, when materialized from the first
21072137
// plot and used to produce a second plot, produce the same output and the same

0 commit comments

Comments
 (0)