Skip to content

expose scales #425

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

Closed
wants to merge 21 commits into from
Closed

expose scales #425

wants to merge 21 commits into from

Conversation

Fil
Copy link
Contributor

@Fil Fil commented May 31, 2021

The scales are exposed on the figure through a .scales property, a function that lazily computes scale option objects that can be used to reproduce the same scales in a different plot.

The function takes a scale name as parameter, and returns a scale definition that matches the given scale as instanciated by the plot's data. (If no scale name is given, it returns all the scales.)

With a lot of unit tests.

A (very messy) notebook with the build is https://observablehq.com/@fil/expose-scales-425

TODO:

  • return the scales as options
  • expose Plot.scale() to create a D3 scale from options.
  • README
  • Make sure we can build all the things we want (color legends, radius legends), "inside" the plot as well as outside
  • play a bit with it, and once we set on good legends, bake them in and expose them as Plot.colorLegend(), Plot.colorSwatches() or Plot.radiusLegend(). (Easy to extend via a Plot definition.)

Capture d’écran 2021-05-31 à 17 31 46

Capture d’écran 2021-05-31 à 19 24 27

@Fil Fil requested a review from mbostock May 31, 2021 15:30
@Fil Fil self-assigned this May 31, 2021
@Fil Fil added enhancement New feature or request question Further information is needed labels May 31, 2021
@Fil Fil marked this pull request as draft May 31, 2021 15:36
@Fil
Copy link
Contributor Author

Fil commented May 31, 2021

Same concept (legend drawn with Plot) for a radius legend
Capture d’écran 2021-05-31 à 21 26 57

Fil added 3 commits May 31, 2021 21:38
accepts a function that receives the scale; if that function returns a DOM node, adds it to the figure.
@Fil
Copy link
Contributor Author

Fil commented Jun 1, 2021

Now we can add color: {legend: function(color) { … }} to embed the resulting legend in the figure; also works for the r scale (and others if necessary).

Fil added 2 commits June 7, 2021 16:35
… be used to recreate the scale in another plot. In particular it doesn't show the (D3) scale anymore. Instead, a convenience function Plot.scale(options) is provided, that will turn a scale *options* object into a D3 scale, ready to use in another context. (This also clears the question of mutability.)
@Fil
Copy link
Contributor Author

Fil commented Jun 7, 2021

For anyone following this, 228565b exposes Plot.scale(options), allowing to create a D3 scale from Plot options.

@Fil Fil marked this pull request as ready for review June 9, 2021 20:46
Fil added 2 commits June 10, 2021 11:45
ensure we don't compute the scale options twice
always return label, clamp, type
@Fil Fil mentioned this pull request Jun 10, 2021
6 tasks
@Fil
Copy link
Contributor Author

Fil commented Jun 10, 2021

Color legends in #432 !

@Fil Fil marked this pull request as draft June 11, 2021 16:07
@Fil
Copy link
Contributor Author

Fil commented Jun 11, 2021

I have to work again on this, since some options are missing (like exponent: 0.3 for type="exp" scales), and we want all options including those that are not consumed by the scales, but could be used by the legend (like "columns" for swatches).

Fil added 2 commits June 11, 2021 19:17
…", "identity")

and type ("utc", "time" / "linear", "sqrt", "log"… / "categorical" / "identity")
except the following which are part of the definition of:
scale.domain() // domain, nice,
scale.range() // range, inset, reverse,
scale.interpolate() // interpolate, clamp, round
paddingInner & paddingOuter // padding
@Fil Fil marked this pull request as ready for review June 11, 2021 19:17
@mbostock
Copy link
Member

mbostock commented Aug 4, 2021

I plan on looking at this next. Exposing (materialized) scales will be great for debugging, too. I’m leaning towards just exposing plot.scales to start, and not the plot.scale method which returns a D3 scale instance. But mostly that’s so I can focus on getting this landed sooner and minimizing new API. 🙂

@Fil Fil mentioned this pull request Aug 5, 2021
7 tasks
@Fil
Copy link
Contributor Author

Fil commented Aug 5, 2021

deferring to #484

@Fil Fil closed this Aug 5, 2021
@Fil Fil deleted the fil/expose-scales branch September 10, 2021 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants