Skip to content
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

propagate title and href by default #674

Merged
merged 4 commits into from
Jan 18, 2022
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
23 changes: 20 additions & 3 deletions src/transforms/group.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {group as grouper, sort, sum, deviation, min, max, mean, median, mode, variance, InternSet, minIndex, maxIndex} from "d3";
import {group as grouper, sort, sum, deviation, min, max, mean, median, mode, variance, InternSet, minIndex, maxIndex, rollup} from "d3";
import {ascendingDefined, firstof} from "../defined.js";
import {valueof, maybeColorChannel, maybeInput, maybeTuple, maybeLazyChannel, lazyChannel, first, identity, take, labelof, range} from "../options.js";
import {valueof, maybeColorChannel, maybeInput, maybeTuple, maybeLazyChannel, lazyChannel, first, identity, take, labelof, range, second} from "../options.js";
import {basic} from "./basic.js";

// Group on {z, fill, stroke}.
Expand Down Expand Up @@ -135,7 +135,11 @@ export function hasOutput(outputs, ...names) {
}

export function maybeOutputs(outputs, inputs) {
return Object.entries(outputs).map(([name, reduce]) => {
const entries = Object.entries(outputs);
// Propagate standard mark channels by default.
if (inputs.title != null && outputs.title === undefined) entries.push(["title", reduceTitle]);
if (inputs.href != null && outputs.href === undefined) entries.push(["href", reduceFirst]);
return entries.map(([name, reduce]) => {
return reduce == null
? {name, initialize() {}, scope() {}, reduce() {}}
: maybeOutput(name, reduce, inputs);
Expand Down Expand Up @@ -266,6 +270,19 @@ const reduceFirst = {
}
};

const reduceTitle = {
reduce(I, X) {
const n = 5;
const groups = sort(rollup(I, V => V.length, i => X[i]), second);
const top = groups.slice(-n).reverse();
if (top.length < groups.length) {
const bottom = groups.slice(0, 1 - n);
top[n - 1] = [`… ${bottom.length.toLocaleString("en-US")} more`, sum(bottom, second)];
}
return top.map(([key, value]) => `${key} (${value.toLocaleString("en-US")})`).join("\n");
}
};

const reduceLast = {
reduce(I, X) {
return X[I[I.length - 1]];
Expand Down
77 changes: 61 additions & 16 deletions test/output/penguinMassSpecies.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.binX({y: "count"}, {x: "body_mass_g", fill: "species"})),
Plot.rectY(data, Plot.binX({y: "count"}, {x: "body_mass_g", fill: "species", title: d => `${d.species} ${d.sex}`})),
Plot.ruleY([0])
]
});
Expand Down