Skip to content

can the standalone Plot.legend returned object have .scale to get the scale Descriptor? #624

@tx0c

Description

@tx0c

sometimes it's better to create a standalone legend (with Plot.legend), not from any pre-existing plot, and use htl.html ... with css to organize better layout,

from https://observablehq.com/@observablehq/plot-legends?collection=@observablehq/plot I'm playing around a bit:

image

you see the sportsPlot (returned value from Plot.plot) is an HTMLElement with .scale and .legend

HTMLElement {scale: ƒ(e), legend: ƒ(n, r)}

but if I give it a name to wx = Plot.legend({...}) to whatever value returned from Plot.legend, it has nothing other than an empty SVGSVGElement {}

wx = Plot.legend({
  color: {
    // interpolate: (t) => wavelengthToColor(400 + t * 350),
    domain: [400, 750]
  },
  width: 350,
  ticks: 10,
  label: "Wavelength (nm) →"
})

then what's an easy way to get the scale object? and where to get d3-scale function from it?

image

I'm expecting it can have a way to return the scale object, something like sportsScale in the example,

> lgd.scale("color")
Object {
  type: "ordinal",
  domain: Array(4) ["A", "B", "H", "I", "J", ...],
  range: Array(10) ["#1f77b4", "#ff7f0e", ...],
  label: "...",
  apply: ƒ(e),
}

current workaround may be to use fakePlot = Plot.plot(...) create a fake plot first, and get the scale object from ... = fakePlot.scale(...), and then the fakePlot is not needed and wasted

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions