Skip to content

Commit

Permalink
propagate title and href by default (#674)
Browse files Browse the repository at this point in the history
* propagate title and href by default

* DRY

* count other

* better other summarization
  • Loading branch information
mbostock authored Jan 18, 2022
1 parent 8d07b26 commit 556bcca
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 20 deletions.
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

0 comments on commit 556bcca

Please sign in to comment.