Skip to content

more descending shorthand #1606

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

Closed
wants to merge 3 commits into from
Closed

more descending shorthand #1606

wants to merge 3 commits into from

Conversation

Fil
Copy link
Contributor

@Fil Fil commented May 21, 2023

I've struggled a bit with the types, but I think the feature is complete (EDIT: well, no).

@Fil Fil requested a review from mbostock May 21, 2023 18:14
@Fil Fil changed the base branch from main to mbostock/descending-shorthand May 21, 2023 18:14
@Fil Fil mentioned this pull request May 21, 2023
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.

Thanks for the contribution. There are a variety of subtle considerations here.

Comment on lines +128 to +131
if (typeof value === "string" && value?.startsWith("-")) {
value = value.slice(1);
order = reverseOrder(order);
}
Copy link
Member

Choose a reason for hiding this comment

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

If value is a string here, it is a field name rather than a reducer or a channel name. Because field names come from the user’s data, they are much more diverse than channel names and reducer names (which are generally fixed by Plot, though we do allow “extra” channels for the tip mark). This would break field names that start with a hyphen, which is admittedly unlikely and uncommon, but still possible. And since we aren’t going to allow x: "-foo" for field names in general, this is somewhat inconsistent.

I don’t think we should introduce sort: "-foo" shorthand for field names. There are already three ways to do this, and that seems reasonable enough:

  1. sort: "foo", reverse: true
  2. sort: {value: "foo", order: "descending"}
  3. sort: (d) => -d.foo (assuming foo is numeric)

We could introduce a value helper, perhaps: sort: Plot.negate("foo"). This would take the same arguments that Plot.valueof takes for value, but would return the negative value. It would only work for numbers, though, of course…

Or perhaps we could also introduce a sort helper: sort: Plot.descending("foo") which just returns {value: "foo", order: "descending"}.

Comment on lines +101 to +104
if (typeof sort === "string" && sort.startsWith("-")) {
sort = sort.slice(1);
reverse = !reverse;
}
Copy link
Member

Choose a reason for hiding this comment

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

A subtle gotcha here: sort: "-x" doesn’t mean “sort by x in natural ascending order and then reverse the resulting order”; it means “sort by x in natural descending order”. The difference is that we want nulls/unordered values to be last when using -x, whereas reversing after sorting would put them first.

The ChannelDomainOptions interface is confusing in this regard because it calls the option reverse, but it really means whether to use ascending or descending order (per the documentation):

plot/src/channel.d.ts

Lines 195 to 196 in 0490ba6

/** If true, use descending instead of ascending order. */
reverse?: boolean;

I commented on this indirectly in #1460, and I’m going to add some context to that issue now to clarify the confusion.

In any case, here we ideally don’t want to flip the reverse option; we want to use descending rather than ascending natural order. That’s why I added the reverseOrder helper for the basic sort transform:

function reverseOrder(order) {
return order === ascendingDefined
? descendingDefined
: order === descendingDefined
? ascendingDefined
: (i, j) => order(j, i);
}

I think this might mean that the maybeSort helper should take an order argument, and that the bin and group transform should support order: "descending" and order: "ascending" as complements to the sort option, instead of only supporting a reverse option?

@@ -29,7 +29,7 @@ export interface GroupOutputOptions<T = Reducer> {
* Plot.groupX({y: "count", sort: "count"}, {fill: "sex", x: "sport"})
* ```
*/
sort?: T | null;
sort?: ReducerSort | null;
Copy link
Member

Choose a reason for hiding this comment

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

This is incorrectly narrowing the type of T (e.g., in the case that T is BinReducer instead of Reducer). We’ll need to find another way to type this.

Comment on lines +81 to +84
if (typeof sort === "string" && sort.startsWith("-")) {
sort = sort.slice(1);
reverse = !reverse;
}
Copy link
Member

Choose a reason for hiding this comment

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

Same here re. reverse.

Comment on lines +84 to +87
if (typeof order === "string" && order.startsWith("-")) {
order = order.slice(1);
reverse = !reverse;
}
Copy link
Member

Choose a reason for hiding this comment

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

Same here re. reverse. And also as commented above, order can be a field name, and I don’t think we should support -field syntax since it is more likely to conflict with a user-supplied column name.

@mbostock mbostock mentioned this pull request May 22, 2023
@mbostock mbostock force-pushed the mbostock/descending-shorthand branch from bef0ec9 to 00338c8 Compare May 22, 2023 16:52
Base automatically changed from mbostock/descending-shorthand to main May 22, 2023 19:05
@Fil Fil closed this May 29, 2023
@Fil Fil deleted the fil/more-descending-shorthand branch May 29, 2023 13:49
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