-
Notifications
You must be signed in to change notification settings - Fork 197
fix group domain #271
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
fix group domain #271
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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import {group as grouper, sort, sum, InternSet} from "d3"; | ||
| import {defined, firstof} from "../defined.js"; | ||
| import {firstof} from "../defined.js"; | ||
| import {valueof, maybeColor, maybeTransform, maybeValue, maybeLazyChannel, lazyChannel, first, identity, take, maybeTuple, labelof} from "../mark.js"; | ||
|
|
||
| // Group on {z, fill, stroke}. | ||
|
|
@@ -85,10 +85,10 @@ function group2(xv, yv, {z, fill, stroke, weight, domain, normalize, ...options} | |
| for (const facet of facets) { | ||
| const groupFacet = []; | ||
| if (normalize === "facet") n = W ? sum(facet, i => W[i]) : facet.length; | ||
| for (const [, I] of groups(facet, G, defined1)) { | ||
| for (const [, I] of groups(facet, G)) { | ||
| if (normalize === "z") n = W ? sum(I, i => W[i]) : I.length; | ||
| for (const [y, fy] of groups(I, Y, ydefined)) { | ||
| for (const [x, f] of groups(fy, X, xdefined)) { | ||
| for (const [y, fy] of groups(I, Y, ydefined, ydomain)) { | ||
| for (const [x, f] of groups(fy, X, xdefined, xdomain)) { | ||
|
Comment on lines
+90
to
+91
Member
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. I’ve been thinking about this a little more, and I think it makes more sense that the corresponding scale’s domain drive which groups are shown, rather than needing to pass the domain explicitly to the group transform. I think this means that we should remove any filtering here, and just group all the data, and then we simply won’t show the resulting marks (e.g., bar will filter it out using maybeCoords). This now intersects with the “grand unification” of group and reduce I’ve been working on, so I’ll try to merge these two PRs. |
||
| const l = W ? sum(f, i => W[i]) : f.length; | ||
| groupFacet.push(i++); | ||
| groupData.push(take(data, f)); | ||
|
|
@@ -113,7 +113,7 @@ function group2(xv, yv, {z, fill, stroke, weight, domain, normalize, ...options} | |
| } | ||
|
|
||
| function maybeDomain(domain) { | ||
| if (domain === undefined) return defined1; | ||
| if (domain === undefined) return; | ||
| if (domain === null) return () => false; | ||
| domain = new InternSet(domain); | ||
| return ([key]) => domain.has(key); | ||
|
|
@@ -129,10 +129,10 @@ function maybeNormalize(normalize) { | |
| throw new Error("invalid normalize"); | ||
| } | ||
|
|
||
| function defined1([key]) { | ||
| return defined(key); | ||
| } | ||
|
|
||
| export function groups(I, X, defined = defined1) { | ||
| return X ? sort(grouper(I, i => X[i]), first).filter(defined) : [[, I]]; | ||
| export function groups(I, X, filter, domain) { | ||
| if (!X) return [[, I]]; | ||
| const G = grouper(I, i => X[i]); | ||
| return domain | ||
| ? domain.map(x => [x, G.has(x) ? G.get(x) : []]) | ||
| : sort(filter ? Array.from(G).filter(filter) : G, first); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import * as Plot from "@observablehq/plot"; | ||
| import * as d3 from "d3"; | ||
|
|
||
| export default async function() { | ||
| const data = await d3.csv("data/penguins.csv", d3.autoType); | ||
| return Plot.plot({ | ||
| facet: { | ||
| data, | ||
| x: "species" | ||
| }, | ||
| fx: { | ||
| domain: d3.groupSort(data, ({length}) => length, d => d.species).reverse() | ||
| }, | ||
| x: { | ||
| domain: ["MALE", "FEMALE", null], | ||
| tickFormat: d => d === null ? "N/A" : d | ||
| }, | ||
| y: { | ||
| grid: true | ||
| }, | ||
| color: { | ||
| scheme: "greys" | ||
| }, | ||
| marks: [ | ||
| Plot.barY(data, Plot.stackY(Plot.groupX({x: "sex", fill: "island", stroke: "black"}))), | ||
| Plot.ruleY([0]) | ||
| ] | ||
| }); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import * as Plot from "@observablehq/plot"; | ||
| import tape from "tape-await"; | ||
|
|
||
| tape("groupX respects the domain option (#255)", test => { | ||
| const data = ["A", "A", "C"]; | ||
| const options = {x: d => d, domain: ["C", "B", "A"]}; | ||
| const mark = Plot.dot(data, Plot.groupX(options)); | ||
| const A = mark.initialize(); | ||
| test.deepEqual(A.index, [0, 1, 2]); | ||
| test.deepEqual(A.channels.find(d => d[0] === "x")[1].value, ["C", "B", "A"]); | ||
| test.deepEqual(A.channels.find(d => d[0] === "y")[1].value, [1, 0, 2]); | ||
| }); | ||
|
|
||
| tape("groupY respects the domain option (#255)", test => { | ||
| const data = ["A", "A", "C"]; | ||
| const options = {x: d => d, domain: ["C", "B", "A"]}; | ||
| const mark = Plot.dot(data, Plot.groupY(options)); | ||
| const A = mark.initialize(); | ||
| test.deepEqual(A.index, [0, 1, 2]); | ||
| test.deepEqual(A.channels.find(d => d[0] === "y")[1].value, ["C", "B", "A"]); | ||
| test.deepEqual(A.channels.find(d => d[0] === "x")[1].value, [1, 0, 2]); | ||
| }); | ||
|
|
||
| tape("group respects the domain option (#255)", test => { | ||
| const data = ["A", "A", "C", "A", "C"]; | ||
| const options = {x: d => d, y: d => d, domain: ["C", "B", "A"]}; | ||
| const mark = Plot.dot(data, Plot.group(options)); | ||
| const A = mark.initialize(); | ||
| test.deepEqual(A.index, [0, 1, 2, 3, 4, 5, 6, 7, 8]); | ||
| test.deepEqual(A.channels.find(d => d[0] === "fill")[1].value, [ | ||
| //C, B, A | ||
| 2, 0, 0, // C | ||
| 0, 0, 0, // B | ||
| 0, 0, 3 // A | ||
| ]); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.