diff --git a/src/mark.js b/src/mark.js index 630611c297..a7a35b9e64 100644 --- a/src/mark.js +++ b/src/mark.js @@ -280,9 +280,15 @@ export function numberChannel(source) { // TODO use Float64Array.from for position and radius scales? export function values(channels = [], scales) { const values = Object.create(null); - for (const [name, {value, scale}] of channels) { + for (let [name, {value, scale}] of channels) { if (name !== undefined) { - values[name] = scale === undefined ? value : Array.from(value, scales[scale]); + if (scale !== undefined) { + scale = scales[scale]; + if (scale !== undefined) { + value = Array.from(value, scale); + } + } + values[name] = value; } } return values; diff --git a/src/scales.js b/src/scales.js index 1e805f0b51..e6e2f93440 100644 --- a/src/scales.js +++ b/src/scales.js @@ -1,5 +1,5 @@ import {registry, position, radius} from "./scales/index.js"; -import {ScaleDiverging, ScaleLinear, ScalePow, ScaleLog, ScaleSymlog} from "./scales/quantitative.js"; +import {ScaleDiverging, ScaleLinear, ScalePow, ScaleLog, ScaleSymlog, ScaleIdentity} from "./scales/quantitative.js"; import {ScaleTime, ScaleUtc} from "./scales/temporal.js"; import {ScaleOrdinal, ScalePoint, ScaleBand} from "./scales/ordinal.js"; @@ -60,6 +60,7 @@ function Scale(key, channels = [], options = {}) { case "time": return ScaleTime(key, channels, options); case "point": return ScalePoint(key, channels, options); case "band": return ScaleBand(key, channels, options); + case "identity": return registry.get(key) === position ? ScaleIdentity(key, channels, options) : undefined; case undefined: return; default: throw new Error(`unknown scale type: ${options.type}`); } diff --git a/src/scales/quantitative.js b/src/scales/quantitative.js index 239558819b..8d7d00eb79 100644 --- a/src/scales/quantitative.js +++ b/src/scales/quantitative.js @@ -1,14 +1,15 @@ -import {min, max, quantile, reverse as reverseof} from "d3"; import { + min, + max, + quantile, + reverse as reverseof, + piecewise, interpolateHcl, interpolateHsl, interpolateLab, interpolateNumber, interpolateRgb, interpolateRound, - piecewise -} from "d3"; -import { interpolateBlues, interpolateBrBG, interpolateBuGn, @@ -46,9 +47,14 @@ import { interpolateYlGn, interpolateYlGnBu, interpolateYlOrBr, - interpolateYlOrRd + interpolateYlOrRd, + scaleDiverging, + scaleLinear, + scaleLog, + scalePow, + scaleSymlog, + scaleIdentity } from "d3"; -import {scaleDiverging, scaleLinear, scaleLog, scalePow, scaleSymlog} from "d3"; import {registry, radius, opacity, color} from "./index.js"; import {positive, negative} from "../defined.js"; @@ -187,6 +193,10 @@ export function ScaleSymlog(key, channels, {constant = 1, ...options}) { return ScaleQ(key, scaleSymlog().constant(constant), channels, options); } +export function ScaleIdentity() { + return {type: "identity", scale: scaleIdentity()}; +} + export function ScaleDiverging(key, channels, { nice, clamp, diff --git a/test/output/identityScale.svg b/test/output/identityScale.svg new file mode 100644 index 0000000000..8227c3569a --- /dev/null +++ b/test/output/identityScale.svg @@ -0,0 +1,147 @@ + + + + 350 + + + 300 + + + 250 + + + 200 + + + 150 + + + 100 + + + 50 + + + + + 100 + + + 200 + + + 300 + + + 400 + + + 500 + + + 600 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/plots/identity-scale.js b/test/plots/identity-scale.js new file mode 100644 index 0000000000..b916f72e1f --- /dev/null +++ b/test/plots/identity-scale.js @@ -0,0 +1,26 @@ +import * as Plot from "@observablehq/plot"; +import * as d3 from "d3"; + +const random = d3.randomLcg(42); + +export default async function() { + return Plot.plot({ + x: { + type: "identity" + }, + y: { + type: "identity" + }, + color: { + type: "identity" + }, + marks: [ + Plot.dot({length: 100}, { + x: () => 600 * random(), + y: () => 100 + 500 * random(), + fill: () => "red", + stroke: () => "blue" + }) + ] + }); +} diff --git a/test/plots/index.js b/test/plots/index.js index 3b0d94bfd9..1912c66796 100644 --- a/test/plots/index.js +++ b/test/plots/index.js @@ -28,6 +28,7 @@ export {default as gistempAnomaly} from "./gistemp-anomaly.js"; export {default as gistempAnomalyMoving} from "./gistemp-anomaly-moving.js"; export {default as hadcrutWarmingStripes} from "./hadcrut-warming-stripes.js"; export {default as highCardinalityOrdinal} from "./high-cardinality-ordinal.js"; +export {default as identityScale} from "./identity-scale.js"; export {default as industryUnemployment} from "./industry-unemployment.js"; export {default as industryUnemploymentShare} from "./industry-unemployment-share.js"; export {default as industryUnemploymentStream} from "./industry-unemployment-stream.js";