Skip to content

the Plot.stack offset option can be a function #814

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

Merged
merged 6 commits into from
Mar 19, 2022
Merged

Conversation

Fil
Copy link
Contributor

@Fil Fil commented Mar 17, 2022

use case: diverging semantic bars following a Likert scale
see https://talk.observablehq.com/t/diverging-stacked-bar-chart-in-plot/6028

Capture d’écran 2022-03-17 à 16 35 10

@Fil Fil requested a review from mbostock March 17, 2022 15:35
README.md Outdated
@@ -1866,6 +1866,7 @@ After all values have been stacked from zero, an optional **offset** can be appl
- *expand* (or *normalize*) - rescale each stack to fill [0, 1]
- *center* (or *silhouette*) - align the centers of all stacks
- *wiggle* - translate stacks to minimize apparent movement
- a function that receives as arguments a nested index of stacks, the X1 and X2 arrays (resp. Y1 and Y2 for stackY), and Z, and can modify X1 and X2, typically by subtracting the same offset from each of the X1 and X2 values that pertain to a stack.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d like this bulleted list to fit on one line. Can you use a short description here, and move the longer description to the paragraph below that talks about offsets? E.g., “If offset is a function, blah blah blah.” We use this style elsewhere in the README if you want to find precedent.

Copy link
Member

@mbostock mbostock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this.

I also wonder if we should have a diverging offset, where the z channel is defined as -2, -1, 0, 1, 2, etc. values, and then you can set the order to z, too, and not need any custom code to do this.

@Fil
Copy link
Contributor Author

Fil commented Mar 17, 2022

Yes this could be another possibility, but we'd have to pass z independently of fill (which would still be the labels for the color scale), and it might be hard to pass more than one neutral z (since there's only one zero—or maybe two if we check the sign of -0 ;-) ).

@Fil
Copy link
Contributor Author

Fil commented Mar 17, 2022

I wonder if allowing the user to mutate values in-place is the correct API here. Maybe when it's a function we want them to return [X1, X2]?

@mbostock
Copy link
Member

I wonder if allowing the user to mutate values in-place is the correct API here.

I had the same thought. For precedent, you could take a look at the map transform: when the map is implemented a function, it returns an array of values:

const M = f(take(S, I));

However if the map is implemented a map object with a map method, we use mutation by assigning to the passed T array:

const mapCumsum = {
map(I, S, T) {
let sum = 0;
for (const i of I) T[i] = sum += S[i];
}
};

Also d3-shape’s stack.offset uses mutation:

https://github.com/d3/d3-shape/blob/main/README.md#stack_offset

I think mutation is fine here, mostly because it feels like a pretty low-level API already given the arguments.

@Fil Fil requested a review from mbostock March 17, 2022 21:55
Copy link
Member

@mbostock mbostock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored the example to make it easier to reuse this technique. (I expect in the future we could make these even easier with a new option for the stack transform, but this is fine for now.)

I also switched the dataset to CSV and added attribution.

@mbostock mbostock merged commit db209c5 into main Mar 19, 2022
@mbostock mbostock deleted the fil/offset-function branch March 19, 2022 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants