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

Auto: error on underspecified reduce #1833

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/marks/auto.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ascending, InternSet} from "d3";
import {marks} from "../mark.js";
import {isColor, isNumeric, isObject, isOptions, isOrdinal, labelof, valueof} from "../options.js";
import {isColor, isIterable, isNumeric, isObject, isOptions, isOrdinal, isEvery, labelof, valueof} from "../options.js";
import {bin, binX, binY} from "../transforms/bin.js";
import {group, groupX, groupY} from "../transforms/group.js";
import {areaX, areaY} from "./area.js";
Expand All @@ -24,6 +24,13 @@ export function autoSpec(data, options) {
const C = materializeValue(data, color);
const S = materializeValue(data, size);

if (isIterable(data) && !isPrimitive(data)) {
for (const channel of ["x", "y", "color", "size"]) {
if (isUnderspecifiedReduce(options[channel]))
throw new Error(`setting ${channel} reducer to "${options[channel].reduce}" requires setting ${channel} field`);
}
}

// Compute the default options.
let {
fx,
Expand Down Expand Up @@ -340,6 +347,17 @@ function isReducer(reduce) {
return false;
}

function isUnderspecifiedReduce({value, reduce}) {
return value == null && reduce != null && !/^count$/i.test(reduce);
}

function isPrimitive(values) {
return isEvery(values, (d) => {
const type = typeof d;
return type === "number" || type === "boolean" || type === "string" || type === "bigint" || d instanceof Date;
});
}

function isHighCardinality(value) {
return value ? new InternSet(value).size > value.length >> 1 : false;
}
Expand Down
39 changes: 39 additions & 0 deletions test/marks/auto-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,42 @@ it("Plot.autoSpec makes a faceted heatmap", () => {
colorMode: "fill"
});
});

it("autoSpec rejects an underspecified reducer", () => {
// primitive array ok
assert.deepStrictEqual(Plot.autoSpec([74, 65], {x: Plot.identity, y: "max"}), {
fx: null,
fy: null,
x: {value: Plot.identity, reduce: null, zero: false},
y: {value: null, reduce: "max", zero: false},
color: {value: null, reduce: null},
size: {value: null, reduce: null},
mark: "rule",
markImpl: "ruleX",
markOptions: {
fx: undefined,
fy: undefined,
x: {value: Object.assign([74, 65], {label: undefined})},
y: undefined,
stroke: undefined,
z: undefined,
r: undefined,
tip: true
},
transformImpl: "binX",
transformOptions: {stroke: undefined, r: undefined, y: "max"},
colorMode: "stroke"
});
// in this case, reducer "max" has no input
assert.throws(
() =>
Plot.autoSpec(
[
{name: "Toph", height: 74},
{name: "Claire", height: 65}
],
{x: "height", y: "max"}
),
/^Error: setting y reducer to "max" requires setting y field$/
);
});