Skip to content

Commit e94c9e8

Browse files
committed
channel aliases; mark.with
1 parent 2cb0639 commit e94c9e8

File tree

6 files changed

+632
-6
lines changed

6 files changed

+632
-6
lines changed

src/options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export function valueof(data, value, arrayType) {
1515
: type === "function" ? array.from(data, value)
1616
: type === "number" || value instanceof Date || type === "boolean" ? array.from(data, constant(value))
1717
: value && typeof value.transform === "function" ? arrayify(value.transform(data), arrayType)
18+
: value && value.alias !== undefined ? value // TODO cleaner?
1819
: arrayify(value, arrayType); // preserve undefined type
1920
}
2021

src/plot.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ export function plot(options = {}) {
3939
if (markChannels.has(mark)) throw new Error("duplicate mark");
4040
const {index, channels} = mark.initialize();
4141
for (const [, channel] of channels) {
42-
const {scale} = channel;
43-
if (scale !== undefined) {
42+
const {value, scale} = channel;
43+
if (value && value.alias !== undefined) {
44+
channel.value = value.alias(markChannels);
45+
} else if (scale !== undefined) {
4446
const scaled = scaleChannels.get(scale);
4547
const {percent, transform = percent ? x => x * 100 : undefined} = options[scale] || {};
4648
if (transform != null) channel.value = Array.from(channel.value, transform);
@@ -153,6 +155,7 @@ export class Mark {
153155
const {facet = "auto", sort, dx, dy, clip} = options;
154156
const names = new Set();
155157
this.data = data;
158+
this.options = options;
156159
this.sort = isOptions(sort) ? sort : null;
157160
this.facet = facet == null || facet === false ? null : keyword(facet === true ? "include" : facet, "facet", ["auto", "include", "exclude"]);
158161
const {transform} = basic(options);
@@ -196,6 +199,19 @@ export class Mark {
196199
plot({marks = [], ...options} = {}) {
197200
return plot({...options, marks: [...marks, this]});
198201
}
202+
with(Mark, options) {
203+
const m = [
204+
this,
205+
Mark(this.data, {
206+
...this.options,
207+
...Object.fromEntries(this.channels.map(({name}) => [name, ({alias: (channels) => channels.get(this).find(([n]) => n === name)[1].value})])),
208+
...options
209+
})
210+
];
211+
m.with = () => m; // TODO chained with
212+
m.plot = Mark.prototype.plot;
213+
return m;
214+
}
199215
}
200216

201217
export function marks(...marks) {

test/output/randomWith.svg

Lines changed: 600 additions & 0 deletions
Loading

test/plots/covid-ihme-projected-deaths.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ export default async function() {
3636
x: "date",
3737
y: "mean",
3838
fill: "currentColor"
39-
}),
40-
Plot.text([data[i]], {
41-
x: "date",
42-
y: "mean",
39+
}).with(Plot.text, {
4340
text: "mean",
4441
textAnchor: "start",
4542
dx: 6

test/plots/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export {default as randomBins} from "./random-bins.js";
119119
export {default as randomBinsXY} from "./random-bins-xy.js";
120120
export {default as randomQuantile} from "./random-quantile.js";
121121
export {default as randomWalk} from "./random-walk.js";
122+
export {default as randomWith} from "./random-with.js";
122123
export {default as seattlePrecipitationRule} from "./seattle-precipitation-rule.js";
123124
export {default as seattleTemperatureBand} from "./seattle-temperature-band.js";
124125
export {default as seattleTemperatureCell} from "./seattle-temperature-cell.js";

test/plots/random-with.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as Plot from "@observablehq/plot";
2+
import * as d3 from "d3";
3+
4+
export default async function() {
5+
const randomNormal = d3.randomNormal.source(d3.randomLcg(42))();
6+
return Plot.plot({
7+
marks: [
8+
Plot.lineY({length: 500}, Plot.mapY("cumsum", {y: randomNormal, stroke: "blue"})).with(Plot.dot)
9+
]
10+
});
11+
}

0 commit comments

Comments
 (0)