Skip to content

More support for labels with channel reducers and values #1364

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

Open
Fil opened this issue Mar 21, 2023 · 4 comments
Open

More support for labels with channel reducers and values #1364

Fil opened this issue Mar 21, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@Fil
Copy link
Contributor

Fil commented Mar 21, 2023

This should display "# of specimens" as the y label:

Plot.barY(
  penguins,
  Plot.groupX(
    {
      y: {reduce: "count", label: "# of specimens"},
      fill: (values) => d3.mode(values)
    },
    {x: "species", fill: "island", fy: "sex"}
  )
);

Excerpted from #1360

First tentative: 9dfeb79

@Fil Fil added the enhancement New feature or request label Mar 21, 2023
Fil added a commit that referenced this issue Mar 21, 2023
@Fil Fil mentioned this issue Mar 21, 2023
9 tasks
@mbostock
Copy link
Member

We do already implement reducer labels, but you have to specify it as a reducer implementation (and ignore the TypeScript lint because we haven’t declared label as an optional property on the ReducerImplementation interface):

Screenshot 2023-03-21 at 11 41 29 AM

Plot.barY(
  penguins,
  Plot.groupX(
    {
      y: {reduce: (index, values) => d3.count(index, (i) => values[i]), label: "# of specimens"},
      fill: (values) => d3.mode(values)
    },
    {x: "species", fill: "island", fy: "sex"}
  )
).plot()

We could also support this as a decorated reducer function

Plot.barY(
  penguins,
  Plot.groupX(
    {
      y: Object.assign((values) => d3.count(values), {label: "# of specimens"}),
      fill: (values) => d3.mode(values)
    },
    {x: "species", fill: "island", fy: "sex"}
  )
).plot()

if we changed reduceFunction like so

function reduceFunction(f) {
  return {
    label: labelof(f),
    reduce(I, X, extent) {
      return f(take(X, I), extent);
    }
  };
}

There are maybe some other places where we could generate default reducer labels, too.

The TODO in the code here is referring to the fact that we haven’t documented the existing support for labels on ReducerImplementation, not that we haven’t implemented it.

// TODO scope, label
export interface BinReducerImplementation {
reduce(index: number[], values: any[], extent: {x1: any; y1: any; x2: any; y2: any}): any;
}

Whereas the TODO here was referring to the fact that it would be nice to specify a label as part of a ChannelValueSpec, parallel to a scale override:

export type ChannelValueSpec = ChannelValue | {value: ChannelValue; scale?: Channel["scale"]}; // TODO label

So, if we add label to a ChannelReducerSpec, we should do that to ChannelValueSpec, too. And it would complicated the duck test that I propose we add in #1367.

@mbostock mbostock changed the title Implement reducer label More support for labels with channel reducers and values Mar 21, 2023
@mbostock
Copy link
Member

I think there’s a similar bug here reading inputs e.g. in the map transform. For example I was expecting to be able to feed the map transform x: {value: "foo", label: "Foo"}, because you can feed that e.g. to a line mark. But the map transform’s maybeInput doesn’t check for scale overrides and labels, so it’s not able to read this channel values, and therefore you can’t propagate the channel label.

@Fil
Copy link
Contributor Author

Fil commented Aug 31, 2023

I've updated the examples/test with the newer reduceIndex syntax.

@mbostock
Copy link
Member

mbostock commented Nov 1, 2023

Related #1873?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants