Skip to content

Commit c6ad4bb

Browse files
authored
dodge declares output channel (#930)
1 parent d0d072b commit c6ad4bb

File tree

5 files changed

+409
-2
lines changed

5 files changed

+409
-2
lines changed

src/options.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ export function maybeColumn(source) {
196196
return source == null ? [source] : column(source);
197197
}
198198

199+
// Used by initializers to declare channels that will be initialized. The
200+
// initial value of these channels is undefined (since they aren’t constructed
201+
// until the initializer is invoked).
202+
export function initialized() {
203+
return {transform() {}};
204+
}
205+
199206
export function labelof(value, defaultValue) {
200207
return typeof value === "string" ? value
201208
: value && value.label !== undefined ? value.label

src/transforms/dodge.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import IntervalTree from "interval-tree-1d";
22
import {finite, positive} from "../defined.js";
3-
import {identity, number, valueof} from "../options.js";
3+
import {identity, initialized, number, valueof} from "../options.js";
44
import {coerceNumbers} from "../scales.js";
55
import {initializer} from "./basic.js";
66

@@ -51,7 +51,7 @@ function dodge(y, x, anchor, padding, options) {
5151
options = {...options, channels: [...channels ?? [], {name: "r", value: r, scale: "r"}]};
5252
if (sort === undefined && reverse === undefined) options.sort = {channel: "r", order: "descending"};
5353
}
54-
return initializer(options, function(data, facets, {[x]: X, r: R}, scales, dimensions) {
54+
return initializer({...options, [y]: initialized()}, function(data, facets, {[x]: X, r: R}, scales, dimensions) {
5555
if (!X) throw new Error(`missing channel: ${x}`);
5656
X = coerceNumbers(valueof(X.value, scales[X.scale] || identity));
5757
const r = R ? undefined : this.r !== undefined ? this.r : options.r !== undefined ? number(options.r) : 3;

test/output/penguinDodgeVoronoi.svg

Lines changed: 386 additions & 0 deletions
Loading

test/plots/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export {default as penguinCulmenVoronoi} from "./penguin-culmen-voronoi.js";
129129
export {default as penguinVoronoi1D} from "./penguin-voronoi-1d.js";
130130
export {default as penguinDodge} from "./penguin-dodge.js";
131131
export {default as penguinDodgeHexbin} from "./penguin-dodge-hexbin.js";
132+
export {default as penguinDodgeVoronoi} from "./penguin-dodge-voronoi.js";
132133
export {default as penguinFacetDodge} from "./penguin-facet-dodge.js";
133134
export {default as penguinFacetDodgeIdentity} from "./penguin-facet-dodge-identity.js";
134135
export {default as penguinFacetDodgeIsland} from "./penguin-facet-dodge-island.js";

test/plots/penguin-dodge-voronoi.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as Plot from "@observablehq/plot";
2+
import * as d3 from "d3";
3+
4+
export default async function() {
5+
const penguins = await d3.csv("data/penguins.csv", d3.autoType);
6+
return Plot.plot({
7+
height: 200,
8+
marks: [
9+
Plot.voronoiMesh(penguins, Plot.dodgeY({x: "body_mass_g"})),
10+
Plot.dot(penguins, Plot.dodgeY({x: "body_mass_g", fill: "currentColor"}))
11+
]
12+
});
13+
}

0 commit comments

Comments
 (0)