Skip to content

expose a deduplicated ordinal domain #1813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/scales/ordinal.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function createScaleO(key, scale, channels, {type, interval, domain, range, reve
if (domain === undefined) domain = inferDomain(channels, interval, key);
if (type === "categorical" || type === ordinalImplicit) type = "ordinal"; // shorthand for color schemes
if (reverse) domain = reverseof(domain);
scale.domain(domain);
domain = scale.domain(domain).domain(); // deduplicate
if (range !== undefined) {
// If the range is specified as a function, pass it the domain.
if (typeof range === "function") range = range(domain);
Expand Down
30 changes: 30 additions & 0 deletions test/scales/scales-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2102,6 +2102,36 @@ it("plot(…).scale(name).apply and invert return the expected functions", () =>
]);
});

it("plot(…).scale(name) returns a deduplicated ordinal domain", () => {
const letters = "abbbcaabbcc";
const plot = Plot.dotX(letters).plot({x: {domain: letters}});
scaleEqual(plot.scale("x"), {
align: 0.5,
bandwidth: 0,
domain: ["a", "b", "c"],
padding: 0.5,
range: [20, 620],
round: true,
step: 200,
type: "point"
});
});

it("plot(…).scale(name) returns a deduplicated ordinal/temporal domain", () => {
const dates = ["2001", "2002", "2004", "2004"].map(d3.isoParse);
const plot = Plot.dotX(dates).plot({x: {type: "point", domain: dates}});
scaleEqual(plot.scale("x"), {
align: 0.5,
bandwidth: 0,
domain: dates.slice(0, 3),
padding: 0.5,
range: [20, 620],
round: true,
step: 200,
type: "point"
});
});

// Given a plot specification (or, as shorthand, an array of marks or a single
// mark), asserts that the given named scales, when materialized from the first
// plot and used to produce a second plot, produce the same output and the same
Expand Down