Skip to content

automatic opt-out of opacity scale #1310

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 4 commits into from
Mar 7, 2023
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
10 changes: 6 additions & 4 deletions src/channel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ascending, descending, rollup, sort} from "d3";
import {first, isColor, isEvery, isIterable, labelof, map, maybeValue, range, valueof} from "./options.js";
import {first, isColor, isEvery, isIterable, isOpacity, labelof, map, maybeValue, range, valueof} from "./options.js";
import {registry} from "./scales/index.js";
import {isSymbol, maybeSymbol} from "./symbols.js";
import {maybeReduce} from "./transforms/group.js";
Expand Down Expand Up @@ -42,14 +42,14 @@ export function inferChannelScale(name, channel) {
case "fill":
case "stroke":
case "color":
channel.scale = isEvery(value, isColor) ? null : "color";
channel.scale = scale !== true && isEvery(value, isColor) ? null : "color";
break;
case "fillOpacity":
case "strokeOpacity":
channel.scale = "opacity";
channel.scale = scale !== true && isEvery(value, isOpacity) ? null : "opacity";
break;
case "symbol":
if (isEvery(value, isSymbol)) {
if (scale !== true && isEvery(value, isSymbol)) {
channel.scale = null;
channel.value = map(value, maybeSymbol);
} else {
Expand All @@ -60,6 +60,8 @@ export function inferChannelScale(name, channel) {
channel.scale = registry.has(name) ? name : null;
break;
}
} else if (scale === false) {
channel.scale = null;
} else if (scale != null && !registry.has(scale)) {
throw new Error(`unknown scale: ${scale}`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/marks/raster.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export function sampler(name, options = {}) {
}
}
}
return {data: V, facets, channels: {[name]: {value: V, scale: true}}};
return {data: V, facets, channels: {[name]: {value: V, scale: "auto"}}};
});
}

Expand Down
4 changes: 4 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ export function isColor(value) {
);
}

export function isOpacity(value) {
return typeof value === "number" && ((0 <= value && value <= 1) || isNaN(value));
}

export function isNoneish(value) {
return value == null || isNone(value);
}
Expand Down
6 changes: 3 additions & 3 deletions src/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ export function styles(
href: {value: href, optional: true},
ariaLabel: {value: variaLabel, optional: true},
fill: {value: vfill, scale: "auto", optional: true},
fillOpacity: {value: vfillOpacity, scale: "opacity", optional: true},
fillOpacity: {value: vfillOpacity, scale: "auto", optional: true},
stroke: {value: vstroke, scale: "auto", optional: true},
strokeOpacity: {value: vstrokeOpacity, scale: "opacity", optional: true},
strokeOpacity: {value: vstrokeOpacity, scale: "auto", optional: true},
strokeWidth: {value: vstrokeWidth, optional: true},
opacity: {value: vopacity, scale: "opacity", optional: true}
opacity: {value: vopacity, scale: "auto", optional: true}
};
}

Expand Down
12 changes: 8 additions & 4 deletions src/transforms/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,18 @@ export function maybeOutputs(outputs, inputs) {
if (inputs.href != null && outputs.href === undefined) entries.push(["href", reduceFirst]);
return entries
.filter(([, reduce]) => reduce !== undefined)
.map(([name, reduce]) => {
return reduce === null ? {name, initialize() {}, scope() {}, reduce() {}} : maybeOutput(name, reduce, inputs);
});
.map(([name, reduce]) => (reduce === null ? nullOutput(name) : maybeOutput(name, reduce, inputs)));
}

export function maybeOutput(name, reduce, inputs) {
let scale; // optional per-channel scale override
if (isObject(reduce) && typeof reduce.reduce !== "function") (scale = reduce.scale), (reduce = reduce.reduce);
const evaluator = maybeEvaluator(name, reduce, inputs);
const [output, setOutput] = column(evaluator.label);
let O;
return {
name,
output,
output: scale === undefined ? output : {value: output, scale},
initialize(data) {
evaluator.initialize(data);
O = setOutput([]);
Expand All @@ -202,6 +202,10 @@ export function maybeOutput(name, reduce, inputs) {
};
}

function nullOutput(name) {
return {name, initialize() {}, scope() {}, reduce() {}};
}

export function maybeEvaluator(name, reduce, inputs) {
const input = maybeInput(name, inputs);
const reducer = maybeReduce(reduce, input);
Expand Down
8 changes: 4 additions & 4 deletions src/transforms/hexbin.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ export function hexbin(outputs = {fill: "count"}, {binWidth, ...options} = {}) {
x: {value: BX},
y: {value: BY},
...(Z && {z: {value: GZ}}),
...(F && {fill: {value: GF, scale: true}}),
...(S && {stroke: {value: GS, scale: true}}),
...(Q && {symbol: {value: GQ, scale: true}}),
...(F && {fill: {value: GF, scale: "auto"}}),
...(S && {stroke: {value: GS, scale: "auto"}}),
...(Q && {symbol: {value: GQ, scale: "auto"}}),
...Object.fromEntries(
outputs.map(({name, output}) => [
name,
{scale: true, radius: name === "r" ? binWidth / 2 : undefined, value: output.transform()}
{scale: "auto", radius: name === "r" ? binWidth / 2 : undefined, value: output.transform()}
])
)
};
Expand Down
18 changes: 9 additions & 9 deletions test/output/markovChain.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion test/scales/scales-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,10 @@ it("plot(…).scale('opacity') can return a linear scale for penguins", async ()

it("plot(…).scale('opacity') respects the percent option, affecting domain", async () => {
const penguins = await d3.csv("data/penguins.csv", d3.autoType);
const plot = Plot.rectX(penguins, Plot.binX({fillOpacity: "proportion"}, {x: "body_mass_g", thresholds: 20})).plot({
const plot = Plot.rectX(
penguins,
Plot.binX({fillOpacity: {reduce: "proportion", scale: true}}, {x: "body_mass_g", thresholds: 20})
).plot({
opacity: {percent: true}
});
scaleEqual(plot.scale("opacity"), {
Expand Down