Skip to content

Commit 556bcca

Browse files
authored
propagate title and href by default (#674)
* propagate title and href by default * DRY * count other * better other summarization
1 parent 8d07b26 commit 556bcca

File tree

3 files changed

+82
-20
lines changed

3 files changed

+82
-20
lines changed

src/transforms/group.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {group as grouper, sort, sum, deviation, min, max, mean, median, mode, variance, InternSet, minIndex, maxIndex} from "d3";
1+
import {group as grouper, sort, sum, deviation, min, max, mean, median, mode, variance, InternSet, minIndex, maxIndex, rollup} from "d3";
22
import {ascendingDefined, firstof} from "../defined.js";
3-
import {valueof, maybeColorChannel, maybeInput, maybeTuple, maybeLazyChannel, lazyChannel, first, identity, take, labelof, range} from "../options.js";
3+
import {valueof, maybeColorChannel, maybeInput, maybeTuple, maybeLazyChannel, lazyChannel, first, identity, take, labelof, range, second} from "../options.js";
44
import {basic} from "./basic.js";
55

66
// Group on {z, fill, stroke}.
@@ -135,7 +135,11 @@ export function hasOutput(outputs, ...names) {
135135
}
136136

137137
export function maybeOutputs(outputs, inputs) {
138-
return Object.entries(outputs).map(([name, reduce]) => {
138+
const entries = Object.entries(outputs);
139+
// Propagate standard mark channels by default.
140+
if (inputs.title != null && outputs.title === undefined) entries.push(["title", reduceTitle]);
141+
if (inputs.href != null && outputs.href === undefined) entries.push(["href", reduceFirst]);
142+
return entries.map(([name, reduce]) => {
139143
return reduce == null
140144
? {name, initialize() {}, scope() {}, reduce() {}}
141145
: maybeOutput(name, reduce, inputs);
@@ -266,6 +270,19 @@ const reduceFirst = {
266270
}
267271
};
268272

273+
const reduceTitle = {
274+
reduce(I, X) {
275+
const n = 5;
276+
const groups = sort(rollup(I, V => V.length, i => X[i]), second);
277+
const top = groups.slice(-n).reverse();
278+
if (top.length < groups.length) {
279+
const bottom = groups.slice(0, 1 - n);
280+
top[n - 1] = [`… ${bottom.length.toLocaleString("en-US")} more`, sum(bottom, second)];
281+
}
282+
return top.map(([key, value]) => `${key} (${value.toLocaleString("en-US")})`).join("\n");
283+
}
284+
};
285+
269286
const reduceLast = {
270287
reduce(I, X) {
271288
return X[I[I.length - 1]];

test/output/penguinMassSpecies.svg

Lines changed: 61 additions & 16 deletions
Loading

test/plots/penguin-mass-species.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default async function() {
1212
grid: true
1313
},
1414
marks: [
15-
Plot.rectY(data, Plot.binX({y: "count"}, {x: "body_mass_g", fill: "species"})),
15+
Plot.rectY(data, Plot.binX({y: "count"}, {x: "body_mass_g", fill: "species", title: d => `${d.species} ${d.sex}`})),
1616
Plot.ruleY([0])
1717
]
1818
});

0 commit comments

Comments
 (0)