Skip to content

expose instantiated scales descriptors in the render API #1810

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 13 commits into from
Aug 16, 2023
Prev Previous commit
Next Next commit
don’t expose domain and range for identity (yet)
  • Loading branch information
mbostock committed Aug 16, 2023
commit ad43e422698113469f5d0bc0c9d37714106fc790
4 changes: 2 additions & 2 deletions src/scales.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,11 +528,11 @@ function exposeScale({scale, type, range, domain, interpolate, interval, transfo
// The domain and range may be missing for non-position identity scales (e.g.,
// color), and for position identity scales, only the range is computed
// internally (by autoScaleRange) and then promoted to the domain here.
if (type === "identity") domain = range;
if (type === "identity") return {type: "identity", apply: (d) => d, invert: (d) => d};
const unknown = scale.unknown ? scale.unknown() : undefined;
return {
type,
...(domain !== undefined && {domain: slice(domain)}), // defensive copy
domain: slice(domain), // defensive copy
...(range !== undefined && {range: slice(range)}), // defensive copy
...(transform !== undefined && {transform}),
...(percent && {percent}), // only exposed if truthy
Expand Down
5 changes: 1 addition & 4 deletions src/scales/quantitative.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,8 @@ function isOrdered(domain, sign) {
// identity scale because it coerces to number; and we can’t compute the domain
// (and equivalently range) since we can’t know whether the values are
// continuous or discrete.
const identityScale = (d) => d;
identityScale.invert = identityScale;

export function createScaleIdentity(key) {
return {type: "identity", scale: hasNumericRange(registry.get(key)) ? scaleIdentity() : identityScale};
return {type: "identity", scale: hasNumericRange(registry.get(key)) ? scaleIdentity() : (d) => d};
}

export function inferDomain(channels, f = finite) {
Expand Down
2 changes: 1 addition & 1 deletion test/scales/scales-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2080,7 +2080,7 @@ it("plot(…).scale(name) reflects the given transform", async () => {

it("plot(…).scale(name) can return an identity scale, ignoring all other options", () => {
const plot = Plot.dot([1, 2], {x: (d) => d, fill: (d) => d}).plot({x: {type: "identity"}, color: {type: "identity"}});
scaleEqual(plot.scale("x"), {domain: [20, 620], range: [20, 620], type: "identity"});
scaleEqual(plot.scale("x"), {type: "identity"});
scaleEqual(plot.scale("color"), {type: "identity"});
});

Expand Down