diff --git a/src/options.js b/src/options.js index a655888ed7..b61e0e526e 100644 --- a/src/options.js +++ b/src/options.js @@ -14,6 +14,7 @@ export function valueof(data, value, arrayType) { : type === "function" ? array.from(data, value) : type === "number" || value instanceof Date || type === "boolean" ? array.from(data, constant(value)) : value && typeof value.transform === "function" ? arrayify(value.transform(data), arrayType) + : value && value.alias !== undefined ? value // TODO cleaner? : arrayify(value, arrayType); // preserve undefined type } diff --git a/src/plot.js b/src/plot.js index 8e7ed9e028..8a4fc7c11f 100644 --- a/src/plot.js +++ b/src/plot.js @@ -37,9 +37,11 @@ export function plot(options = {}) { for (const mark of marks) { if (markChannels.has(mark)) throw new Error("duplicate mark"); const {index, channels} = mark.initialize(); - for (const [, channel] of channels) { - const {scale} = channel; - if (scale !== undefined) { + for (const [name, channel] of channels) { + const {value, scale} = channel; + if (value && value.alias !== undefined) { + channel.value = markChannels.get(value.alias).find(([n]) => name === n)[1].value; // TODO handle errors + } else if (scale !== undefined) { const scaled = scaleChannels.get(scale); const {percent, transform = percent ? x => x * 100 : undefined} = options[scale] || {}; if (transform != null) channel.value = Array.from(channel.value, transform); diff --git a/test/plots/covid-ihme-projected-deaths.js b/test/plots/covid-ihme-projected-deaths.js index 560ab55b46..3db6aea418 100644 --- a/test/plots/covid-ihme-projected-deaths.js +++ b/test/plots/covid-ihme-projected-deaths.js @@ -4,6 +4,11 @@ import * as d3 from "d3"; export default async function() { const data = await d3.csv("data/covid-ihme-projected-deaths.csv", d3.autoType); const i = data.findIndex(d => d.projected) - 1; + const dot = Plot.dot([data[i]], { + x: "date", + y: "mean", + fill: "currentColor" + }); return Plot.plot({ width: 960, height: 600, @@ -32,14 +37,10 @@ export default async function() { title: () => "projected values", strokeDasharray: "2,2" }), - Plot.dot([data[i]], { - x: "date", - y: "mean", - fill: "currentColor" - }), - Plot.text([data[i]], { - x: "date", - y: "mean", + dot, + Plot.text(dot.data, { + x: {alias: dot}, + y: {alias: dot}, text: "mean", textAnchor: "start", dx: 6