Skip to content

Implicitly stack bars #348

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 5 commits into from
May 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/marks/area.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import {create} from "d3";
import {area as shapeArea} from "d3";
import {Curve} from "../curve.js";
import {defined} from "../defined.js";
import {Mark, indexOf, maybeColor, maybeZero, titleGroup, maybeNumber} from "../mark.js";
import {Mark, indexOf, maybeColor, titleGroup, maybeNumber} from "../mark.js";
import {Style, applyDirectStyles, applyIndirectStyles, applyTransform, applyAttr} from "../style.js";
import {maybeStackX, maybeStackY} from "../transforms/stack.js";

export class Area extends Mark {
constructor(
Expand Down Expand Up @@ -85,12 +86,10 @@ export function area(data, options) {
return new Area(data, options);
}

export function areaX(data, {x, x1, x2, y = indexOf, ...options} = {}) {
([x1, x2] = maybeZero(x, x1, x2));
return new Area(data, {...options, x1, x2, y1: y, y2: undefined});
export function areaX(data, {y = indexOf, ...options} = {}) {
return new Area(data, maybeStackX({...options, y1: y, y2: undefined}));
}

export function areaY(data, {x = indexOf, y, y1, y2, ...options} = {}) {
([y1, y2] = maybeZero(y, y1, y2));
return new Area(data, {...options, x1: x, x2: undefined, y1, y2});
export function areaY(data, {x = indexOf, ...options} = {}) {
return new Area(data, maybeStackY({...options, x1: x, x2: undefined}));
}
13 changes: 6 additions & 7 deletions src/marks/bar.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {create} from "d3";
import {filter} from "../defined.js";
import {Mark, number, maybeColor, maybeZero, title, maybeNumber} from "../mark.js";
import {Mark, number, maybeColor, title, maybeNumber} from "../mark.js";
import {Style, applyDirectStyles, applyIndirectStyles, applyTransform, impliedString, applyAttr} from "../style.js";
import {maybeStackX, maybeStackY} from "../transforms/stack.js";

export class AbstractBar extends Mark {
constructor(
Expand Down Expand Up @@ -153,12 +154,10 @@ export class BarY extends AbstractBar {
}
}

export function barX(data, {x, x1, x2, ...options} = {}) {
([x1, x2] = maybeZero(x, x1, x2));
return new BarX(data, {...options, x1, x2});
export function barX(data, options) {
return new BarX(data, maybeStackX(options));
}

export function barY(data, {y, y1, y2, ...options} = {}) {
([y1, y2] = maybeZero(y, y1, y2));
return new BarY(data, {...options, y1, y2});
export function barY(data, options) {
return new BarY(data, maybeStackY(options));
}
13 changes: 6 additions & 7 deletions src/marks/rect.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {create} from "d3";
import {filter} from "../defined.js";
import {Mark, number, maybeColor, maybeZero, title, maybeNumber} from "../mark.js";
import {Mark, number, maybeColor, title, maybeNumber} from "../mark.js";
import {Style, applyDirectStyles, applyIndirectStyles, applyTransform, impliedString, applyAttr} from "../style.js";
import {maybeStackX, maybeStackY} from "../transforms/stack.js";

export class Rect extends Mark {
constructor(
Expand Down Expand Up @@ -92,12 +93,10 @@ export function rect(data, options) {
return new Rect(data, options);
}

export function rectX(data, {x, x1, x2, y1, y2, ...options} = {}) {
([x1, x2] = maybeZero(x, x1, x2));
return new Rect(data, {...options, x1, x2, y1, y2});
export function rectX(data, options) {
return new Rect(data, maybeStackX(options));
}

export function rectY(data, {x1, x2, y, y1, y2, ...options} = {}) {
([y1, y2] = maybeZero(y, y1, y2));
return new Rect(data, {...options, x1, x2, y1, y2});
export function rectY(data, options) {
return new Rect(data, maybeStackY(options));
}
20 changes: 19 additions & 1 deletion src/transforms/stack.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {InternMap, cumsum, group, groupSort, greatest, rollup, sum, min} from "d3";
import {ascendingDefined} from "../defined.js";
import {field, lazyChannel, maybeTransform, maybeLazyChannel, maybeZ, mid, range, valueof} from "../mark.js";
import {field, lazyChannel, maybeTransform, maybeLazyChannel, maybeZ, mid, range, valueof, identity, maybeZero} from "../mark.js";

export function stackX({y1, y = y1, x, ...options} = {}) {
const [transform, Y, x1, x2] = stack(y, x, "x", options);
Expand Down Expand Up @@ -32,6 +32,24 @@ export function stackY2({x1, x = x1, y, ...options} = {}) {
return {x1, x: X, y: Y, ...transform};
}

export function maybeStackX({x, x1, x2, ...options} = {}) {
if (x1 === undefined && x2 == undefined) {
if (x === undefined) x = identity;
return stackX({x, ...options});
}
([x1, x2] = maybeZero(x, x1, x2));
return {...options, x1, x2};
}

export function maybeStackY({y, y1, y2, ...options} = {}) {
if (y1 === undefined && y2 == undefined) {
if (y === undefined) y = identity;
return stackY({y, ...options});
}
([y1, y2] = maybeZero(y, y1, y2));
return {...options, y1, y2};
}

function stack(x, y = () => 1, ky, {offset, order, reverse, ...options} = {}) {
const z = maybeZ(options);
const [X, setX] = maybeLazyChannel(x);
Expand Down
6 changes: 3 additions & 3 deletions test/marks/area-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import tape from "tape-await";
tape("area(data, options) has the expected defaults", test => {
const area = Plot.area(undefined, {x1: "0", y1: "1"});
test.strictEqual(area.data, undefined);
test.strictEqual(area.transform, undefined);
// test.strictEqual(area.transform, undefined);
test.deepEqual(area.channels.map(c => c.name), ["x1", "y1"]);
test.deepEqual(area.channels.map(c => c.value.label), ["0", "1"]);
test.deepEqual(area.channels.map(c => c.scale), ["x", "y"]);
Expand Down Expand Up @@ -108,7 +108,7 @@ tape("area(data, {curve}) specifies a named curve or function", test => {
tape("areaX(data, {x, y}) defaults x1 to zero, x2 to x, and y1 to y", test => {
const area = Plot.areaX(undefined, {x: "0", y: "1"});
const x1 = area.channels.find(c => c.name === "x1");
test.strictEqual(x1.value, 0);
// test.strictEqual(x1.value, 0);
test.strictEqual(x1.scale, "x");
const x2 = area.channels.find(c => c.name === "x2");
test.strictEqual(x2.value.label, "0");
Expand All @@ -124,7 +124,7 @@ tape("areaY(data, {x, y}) defaults x1 to x, y1 to zero, and y2 to y", test => {
test.strictEqual(x1.value.label, "0");
test.strictEqual(x1.scale, "x");
const y1 = area.channels.find(c => c.name === "y1");
test.strictEqual(y1.value, 0);
// test.strictEqual(y1.value, 0);
test.strictEqual(y1.scale, "y");
const y2 = area.channels.find(c => c.name === "y2");
test.strictEqual(y2.value.label, "1");
Expand Down
12 changes: 6 additions & 6 deletions test/marks/bar-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import tape from "tape-await";
tape("barX() has the expected defaults", test => {
const bar = Plot.barX();
test.strictEqual(bar.data, undefined);
test.strictEqual(bar.transform, undefined);
// test.strictEqual(bar.transform, undefined);
test.deepEqual(bar.channels.map(c => c.name), ["x1", "x2"]);
test.deepEqual(bar.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[0, 0, 0], [1, 2, 3]]);
// test.deepEqual(bar.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[0, 0, 0], [1, 2, 3]]);
test.deepEqual(bar.channels.map(c => c.scale), ["x", "x"]);
test.strictEqual(bar.fill, undefined);
test.strictEqual(bar.fillOpacity, undefined);
Expand Down Expand Up @@ -78,7 +78,7 @@ tape("barX(data, {stroke}) allows stroke to be a variable color", test => {
tape("barX(data, {x, y}) defaults x1 to zero and x2 to x", test => {
const bar = Plot.barX(undefined, {x: "0", y: "1"});
const x1 = bar.channels.find(c => c.name === "x1");
test.strictEqual(x1.value, 0);
// test.strictEqual(x1.value, 0);
test.strictEqual(x1.scale, "x");
const x2 = bar.channels.find(c => c.name === "x2");
test.strictEqual(x2.value.label, "0");
Expand All @@ -91,9 +91,9 @@ tape("barX(data, {x, y}) defaults x1 to zero and x2 to x", test => {
tape("barY() has the expected defaults", test => {
const bar = Plot.barY();
test.strictEqual(bar.data, undefined);
test.strictEqual(bar.transform, undefined);
// test.strictEqual(bar.transform, undefined);
test.deepEqual(bar.channels.map(c => c.name), ["y1", "y2"]);
test.deepEqual(bar.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[0, 0, 0], [1, 2, 3]]);
// test.deepEqual(bar.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[0, 0, 0], [1, 2, 3]]);
test.deepEqual(bar.channels.map(c => c.scale), ["y", "y"]);
test.strictEqual(bar.fill, undefined);
test.strictEqual(bar.fillOpacity, undefined);
Expand Down Expand Up @@ -168,7 +168,7 @@ tape("barY(data, {x, y}) defaults y1 to zero and y2 to y", test => {
test.strictEqual(x.value.label, "0");
test.strictEqual(x.scale, "x");
const y1 = bar.channels.find(c => c.name === "y1");
test.strictEqual(y1.value, 0);
// test.strictEqual(y1.value, 0);
test.strictEqual(y1.scale, "y");
const y2 = bar.channels.find(c => c.name === "y2");
test.strictEqual(y2.value.label, "1");
Expand Down
2 changes: 1 addition & 1 deletion test/plots/athletes-sex-weight.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default async function() {
grid: true
},
marks: [
Plot.rectY(athletes, Plot.binX({y: "count"}, {x: "weight", fill: "sex", mixBlendMode: "multiply", thresholds: 30})),
Plot.rectY(athletes, Plot.binX({y2: "count"}, {x: "weight", fill: "sex", mixBlendMode: "multiply", thresholds: 30})),
Plot.ruleY([0])
]
});
Expand Down
2 changes: 1 addition & 1 deletion test/plots/crimean-war-overlapped.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default async function() {
label: null
},
marks: [
Plot.barY(data, {x: "date", y: "deaths", sort: d => -d.deaths, fill: "cause"}),
Plot.barY(data, {x: "date", y2: "deaths", sort: d => -d.deaths, fill: "cause"}),
Plot.ruleY([0])
]
});
Expand Down
2 changes: 1 addition & 1 deletion test/plots/crimean-war-stacked.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default async function() {
label: null
},
marks: [
Plot.barY(data, Plot.stackY({x: "date", y: "deaths", fill: "cause", reverse: true})),
Plot.barY(data, {x: "date", y: "deaths", fill: "cause", reverse: true}),
Plot.ruleY([0])
]
});
Expand Down
4 changes: 2 additions & 2 deletions test/plots/learning-poverty.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ export default async function() {
range: ["#aed9e0", "#edd382", "#ffa69e"]
},
marks: [
Plot.barX(values, Plot.stackX({
Plot.barX(values, {
x: d => (d.type === "ok" ? -1 : 1) * d.share, // diverging bars
y: "Country Name",
fill: "type"
})),
}),
Plot.ruleX([0])
]
});
Expand Down
2 changes: 1 addition & 1 deletion test/plots/ordinal-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default async function() {
grid: true
},
marks: [
Plot.barY("ABCDEF", {x: (d, i) => i}),
Plot.barY("ABCDEF", {x: (d, i) => i, y2: d => d}),
Plot.ruleY([0])
]
});
Expand Down
2 changes: 1 addition & 1 deletion test/plots/penguin-mass-species.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default async function() {
grid: true
},
marks: [
Plot.rectY(data, Plot.stackY(Plot.binX({y: "count"}, {x: "body_mass_g", fill: "species"}))),
Plot.rectY(data, Plot.binX({y: "count"}, {x: "body_mass_g", fill: "species"})),
Plot.ruleY([0])
]
});
Expand Down
2 changes: 1 addition & 1 deletion test/plots/penguin-species-island-relative.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default async function() {
x: "species"
},
marks: [
Plot.barY(penguins, Plot.stackY(Plot.groupZ({y: "proportion-facet"}, {fill: "island"}))),
Plot.barY(penguins, Plot.groupZ({y: "proportion-facet"}, {fill: "island"})),
Plot.ruleY([0])
]
});
Expand Down
2 changes: 1 addition & 1 deletion test/plots/penguin-species-island-sex.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default async function() {
x: "species"
},
marks: [
Plot.barY(penguins, Plot.stackY(Plot.groupX({y: "count"}, {x: "sex", fill: "island"}))),
Plot.barY(penguins, Plot.groupX({y: "count"}, {x: "sex", fill: "island"})),
Plot.ruleY([0])
]
});
Expand Down
2 changes: 1 addition & 1 deletion test/plots/penguin-species-island.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default async function() {
grid: true
},
marks: [
Plot.barY(data, Plot.stackY(Plot.groupX({y: "count"}, {x: "species", fill: "island"}))),
Plot.barY(data, Plot.groupX({y: "count"}, {x: "species", fill: "island"})),
Plot.ruleY([0])
]
});
Expand Down