observablehq / plot

A concise API for exploratory data visualization implementing a layered grammar of graphics
https://observablehq.com/plot/
ISC License
4.32k stars 176 forks source link

The normalize transform should mirror the map transform more closely #1376

Open mbostock opened 1 year ago

mbostock commented 1 year ago

The normalizeX and normalizeY transforms mirror mapX and mapY, the only difference being that you specify a shorthand basis instead of a generic map. But the normalize method isn’t a transform, like the map transform: it returns a map implementation. And hence you have to use it in conjunction with the map transform like so:

Plot.map({title: Plot.normalize("first")}, {x: "Date", title: "Close", stroke: "Symbol"})

It would be shorter and more consistent to have normalize be a transform just like map, where you declare outputs and inputs:

Plot.normalize({title: "first"}, {x: "Date", title: "Close", stroke: "Symbol"})

If desired, we could retain the existing normalize method as Plot.normalizer or some such. (We might offer a similar Plot.reducer for reducer implementations in the future.) Possibly we could change Plot.normalize while retaining backwards compatibility by checking what you pass in, but it’s a little dicey. :slightly_smiling_face:

Inspired by #1354.

mbostock commented 1 year ago

Plot.window is like Plot.normalize, too (and not Plot.map).

mbostock commented 1 year ago

Another example. Instead of this

Plot.map({stroke: Plot.window({k: 2, reduce: "difference"})}, {x: "date", y: "unemployment", z: "division", stroke: "unemployment"})

we could say this

Plot.window({k: 2, stroke: "difference"}, {x: "date", y: "unemployment", z: "division", stroke: "unemployment"})

You have to mix in the window options with the output channels, but I think that’s okay, and the bin transform does that too.

mbostock commented 1 year ago

I ran into this again because I wanted to apply a tip to a line with a window transform using the title channel. I was confused why the non-windowed values were appearing in the tip because the windowY transform doesn’t affect the title channel.