Skip to content
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

handle descending domain for quantize scales #830

Merged
merged 5 commits into from
Mar 25, 2022

Conversation

Fil
Copy link
Contributor

@Fil Fil commented Mar 25, 2022

Capture d’écran 2022-03-25 à 12 50 34

Capture d’écran 2022-03-25 à 12 50 40

Capture d’écran 2022-03-25 à 12 50 46

Capture d’écran 2022-03-25 à 12 50 43

@Fil Fil mentioned this pull request Mar 25, 2022
3 tasks
Copy link
Member

@mbostock mbostock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the help! 🙏

@@ -93,7 +93,8 @@ export function legendRamp(color, {

// Threshold
else if (type === "threshold") {
const thresholds = domain;
const thresholds = domain.slice();
if (color.descending) thresholds.reverse();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn’t appear that color.descending is ever set?

@@ -151,8 +151,10 @@ export function ScaleQuantize(key, channels, {
range,
reverse
}) {
domain = extent(domain); // TODO preserve descending domains?
const descending = order(domain) < 0; // preserve descending domain
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure we can assume that domain is an array here. I believe it’s allowed to be an iterable, so we’d need to arrayify it first. Also, would it be simpler to flip reverse here if the domain is decending, rather than reversing the thresholds later? Then we won’t need to support descending thresholds for reversed scales generally.

  if (order(domain) < 0) reverse = !reverse; // preserve descending domain

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To answer my own question here, flipping reverse would produce a different result: it flips the range of the scale rather than the domain. So, you’d get this:

Screen Shot 2022-03-25 at 12 50 54 PM

Instead of this:

Screen Shot 2022-03-25 at 12 50 35 PM

The behavior of the scale is the same, but the appearance of the legend is different. If we want to be consistent with descending domains on continuous scales, then we want to flip the domain rather than the range.

@@ -168,9 +170,16 @@ export function ScaleThreshold(key, channels, {
range = interpolate !== undefined ? quantize(interpolate, domain.length + 1) : registry.get(key) === color ? ordinalRange(scheme, domain.length + 1) : undefined,
reverse
}) {
const descending = domain[0] > domain[1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We’d need to use order(domain) here again. But as I hinted at in the other comment, I don’t think we need to add support for descending domains for the threshold scales; we only need to do it for quantize scales. Threshold scales require you to specify explicit thresholds rather than an extent, so it seems reasonable to require that those thresholds are in ascending order.

Also, it looks like ScaleThreshold doesn’t correctly handle when the domain is specified as an iterable rather than an array (because it references domain.length), and it probably should.

@@ -168,9 +170,16 @@ export function ScaleThreshold(key, channels, {
range = interpolate !== undefined ? quantize(interpolate, domain.length + 1) : registry.get(key) === color ? ordinalRange(scheme, domain.length + 1) : undefined,
reverse
}) {
const descending = domain[0] > domain[1];
if (descending) domain.reverse();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would mutate the caller’s domain! Don’t mutate inputs! 😬

@mbostock mbostock merged commit e8f8ba8 into mbostock/quantize Mar 25, 2022
@mbostock mbostock deleted the fil/quantize-descending branch March 25, 2022 20:44
mbostock added a commit that referenced this pull request Mar 26, 2022
* quantize scale

* tests

* document quantize scales

* handle descending domain for quantize scales (#830)

* reverse and descending

* tests for quantize scales

* remove dead code

* update test snapshot

* handle non-array domain

Co-authored-by: Mike Bostock <[email protected]>

* Update README

* exact thresholds when range is specified

* rename quantiles option to n

* Update README

Co-authored-by: Philippe Rivière <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants