-
Notifications
You must be signed in to change notification settings - Fork 187
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
can the standalone Plot.legend
returned object have .scale
to get the scale Descriptor?
#624
Comments
It's true that Plot uses d3-scale internally, but we don't expose the object on purpose, for two reasons:
However the scales descriptions which are exposed contain enough information if one needs to recreate them in d3-scale, and, maybe more importantly, two methods (apply and invert) that can be used directly on data. Exposing the scale on the Plot.legend sounds like a good idea! PR in #625. |
👍 good, I've just see it called a btw, another way I've tried, is to create a d3-scale function first, then need to pass a const scaleColor = d3.scaleOrdinal()
.domain(["A", "B", "C", ...])
.range(d3.schemeCategorial...) but didn't find a convenient way to do so: lgd = Plot.legend({
color: {
type: "ordinal", // does a d3-scale function have a way to infer a type for plot?
domain: scaleColor.domain(),
range: scaleColor.range(),
apply: scaleColor,
// ... infer more options from the d3-scale function, like clamping, ... etc
},
}) can there be a feature request for Plot.legend to take a d3-scale function if already have one? even got the is there some way (or plan) to use a |
Plot.legend
returned object have .scale
get the scale object?Plot.legend
returned object have .scale
to get the scale Descriptor?
If the scale is categorical or linear, we can pass scale.domain() and scale.range(); if it's non-linear, we can generally pass it as an interpolator and ticks. However if we wanted to cover all the supported scales, there would be quite a lot of work to do. (Just to work on the color legend we have more than a hundred commits, and dozens of unit tests.) This is not something we want to support (and document) officially, but (exercise for the reader…) it is probably possible to fork https://observablehq.com/@d3/color-legend so that it returns a useful scale descriptor instead of a legend. The scale descriptor has no invert for ordinal scales, since ordinal scales have no invert. This is something that might be added in some cases, but it's a separate issue. |
then can still request the internal d3-scale function be exposed somehow? under discussion #611 the actual wanted is a mid point of
but, when xScale is not linear, the calculation is not that simple, and specific to different scale, e.g. can I suggest all marks not only accept an object, but also accept a function signature like:
|
We’re not going to expose the D3 scale object, but we do expose an apply function that calls the scale (and likewise an invert function that calls scale.invert). So, you can call that and it’s equivalent to calling the underlying D3 scale. We’re just not exposing the scale’s getter and setter methods. |
sometimes it's better to create a standalone legend (with
Plot.legend
), not from any pre-existing plot, and usehtl.html ...
with css to organize better layout,from https://observablehq.com/@observablehq/plot-legends?collection=@observablehq/plot I'm playing around a bit:
you see the sportsPlot (returned value from
Plot.plot
) is anHTMLElement
with.scale
and.legend
but if I give it a name to
wx = Plot.legend({...})
to whatever value returned fromPlot.legend
, it has nothing other than an emptySVGSVGElement {}
then what's an easy way to get the scale object? and where to get d3-scale function from it?
I'm expecting it can have a way to return the scale object, something like
sportsScale
in the example,current workaround may be to use
fakePlot = Plot.plot(...)
create a fake plot first, and get the scale object from... = fakePlot.scale(...)
, and then thefakePlot
is not needed and wastedThe text was updated successfully, but these errors were encountered: