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

quantize scale #829

Merged
merged 8 commits into from
Mar 26, 2022
Merged

quantize scale #829

merged 8 commits into from
Mar 26, 2022

Conversation

mbostock
Copy link
Member

@mbostock mbostock commented Mar 25, 2022

Fixes #828.

Screen Shot 2022-03-24 at 8 21 14 PM

TODO

  • handle descending domains?
  • what if you want an exact number of range values?
  • what if the range is specified an array of values?

@mbostock mbostock requested a review from Fil March 25, 2022 03:22
@mbostock mbostock force-pushed the mbostock/quantize branch from 4db2425 to e9e5fd7 Compare March 25, 2022 03:40
@Fil
Copy link
Contributor

Fil commented Mar 25, 2022

I've added a bit of documentation.

handle descending domains?

see #830

what if you want an exact number of range values?

Since we're using d3.ticks, I don't think we can guarantee that the values will be "round". In that case, I think we should leave it to the user to analyze the domain, and return the appropriate threshold scale.

It might help if we accepted a domain function as a scale option (independently of its type); the input would be the flattened set of channel values (or maybe just a nested array of values to make it more efficient, replacing flatMap by map):

--- a/src/scales.js
+++ b/src/scales.js
@@ -133,6 +133,9 @@ export function normalizeScale(key, scale, hint) {
 }

 function Scale(key, channels = [], options = {}) {
+  if (typeof options.domain === "function") options.domain = options.domain(channels.flatMap(({value}) => value));
   const type = inferScaleType(key, channels, options);

   // Warn for common misuses of implicit ordinal scales. We disable this test if

then in simpson-views.js we could specify:

    color: {
      type: "threshold",
      domain: (data) => [2, 7, 12, 20],
      legend: true
    },

what if the range is specified an array of values?

Currently this works if the range's length is equal to the computed number of thresholds + 1 — otherwise we get blank values to the right; we could add this to remove the thresholds that have no matching color:

if (range) thresholds = thresholds.slice(0, range.length - 1);

but it would create a false impression of what the domain is (the last bin would be much larger than the previous ones), and it's probably better if the chart fails visibly in that case. And if we add the domain function of the previous item, it would still allow users to set the thresholds efficiently.

Fil and others added 3 commits March 25, 2022 13:44
* reverse and descending

* tests for quantize scales

* remove dead code

* update test snapshot

* handle non-array domain

Co-authored-by: Mike Bostock <[email protected]>
@mbostock mbostock marked this pull request as ready for review March 25, 2022 21:07
@mbostock
Copy link
Member Author

I’ve now made it so you can pass an explicit range to a quantile or quantize scale, and it will infer a domain of the appropriate corresponding cardinality.

src/scales/quantitative.js Outdated Show resolved Hide resolved
@mbostock mbostock merged commit 81f17ea into main Mar 26, 2022
@mbostock mbostock deleted the mbostock/quantize branch March 26, 2022 02:38
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.

Quantize scales
2 participants