-
Notifications
You must be signed in to change notification settings - Fork 185
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -98,6 +98,10 @@ function binn( | |||||||||||||||||||
// Compute the outputs. | ||||||||||||||||||||
outputs = maybeBinOutputs(outputs, inputs); | ||||||||||||||||||||
reduceData = maybeBinReduce(reduceData, identity); | ||||||||||||||||||||
if (typeof sort === "string" && sort.startsWith("-")) { | ||||||||||||||||||||
sort = sort.slice(1); | ||||||||||||||||||||
reverse = !reverse; | ||||||||||||||||||||
} | ||||||||||||||||||||
Comment on lines
+101
to
+104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A subtle gotcha here: 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): Lines 195 to 196 in 0490ba6
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: Lines 143 to 149 in bef0ec9
I think this might mean that the maybeSort helper should take an order argument, and that the bin and group transform should support |
||||||||||||||||||||
sort = sort == null ? undefined : maybeBinOutput("sort", sort, inputs); | ||||||||||||||||||||
filter = filter == null ? undefined : maybeBinEvaluator("filter", filter, inputs); | ||||||||||||||||||||
|
||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import type {ChannelReducers, ChannelValue} from "../channel.js"; | ||
import type {Reducer} from "../reducer.js"; | ||
import type {ReducerSort, Reducer} from "../reducer.js"; | ||
import type {Transformed} from "./basic.js"; | ||
|
||
/** Options for outputs of the group (and bin) transform. */ | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is incorrectly narrowing the type of |
||
|
||
/** If true, reverse the order of generated groups; defaults to false. */ | ||
reverse?: boolean; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,10 @@ function groupn( | |
// Compute the outputs. | ||
outputs = maybeOutputs(outputs, inputs); | ||
reduceData = maybeReduce(reduceData, identity); | ||
if (typeof sort === "string" && sort.startsWith("-")) { | ||
sort = sort.slice(1); | ||
reverse = !reverse; | ||
} | ||
Comment on lines
+81
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here re. reverse. |
||
sort = sort == null ? undefined : maybeOutput("sort", sort, inputs); | ||
filter = filter == null ? undefined : maybeEvaluator("filter", filter, inputs); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,6 +81,10 @@ function stack(x, y = one, kx, ky, {offset, order, reverse}, options) { | |
const [Y2, setY2] = column(y); | ||
Y1.hint = Y2.hint = lengthy; | ||
offset = maybeOffset(offset); | ||
if (typeof order === "string" && order.startsWith("-")) { | ||
order = order.slice(1); | ||
reverse = !reverse; | ||
} | ||
Comment on lines
+84
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
order = maybeOrder(order, offset, ky); | ||
return [ | ||
basic(options, (data, facets, plotOptions) => { | ||
|
There was a problem hiding this comment.
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:sort: "foo", reverse: true
sort: {value: "foo", order: "descending"}
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"}
.