From ac10e88a7ff9c815a21f31ae1fb07b6106d1af83 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Wed, 7 Apr 2021 17:57:06 -0700 Subject: [PATCH 1/4] identity scale --- src/mark.js | 10 ++++++++-- src/scales.js | 2 +- test/plots/identity-scale.js | 24 ++++++++++++++++++++++++ test/plots/index.js | 1 + 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 test/plots/identity-scale.js 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..045da89676 100644 --- a/src/scales.js +++ b/src/scales.js @@ -60,7 +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 undefined: return; + case "identity": case undefined: return; default: throw new Error(`unknown scale type: ${options.type}`); } } diff --git a/test/plots/identity-scale.js b/test/plots/identity-scale.js new file mode 100644 index 0000000000..26c3b73c8a --- /dev/null +++ b/test/plots/identity-scale.js @@ -0,0 +1,24 @@ +import * as Plot from "@observablehq/plot"; + +export default async function() { + return Plot.plot({ + height: 396, + x: { + type: "identity" + }, + y: { + type: "identity" + }, + color: { + type: "identity" + }, + marks: [ + Plot.dot({length: 100}, { + x: () => 600 * Math.random(), + y: () => 100 + 500 * Math.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"; From 8b4709ec0d7371a8c01ec36b29d9b9bdacc550da Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Thu, 8 Apr 2021 13:53:08 -0700 Subject: [PATCH 2/4] identity test --- test/output/identityScale.svg | 104 ++++++++++++++++++++++++++++++++++ test/plots/identity-scale.js | 7 ++- 2 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 test/output/identityScale.svg diff --git a/test/output/identityScale.svg b/test/output/identityScale.svg new file mode 100644 index 0000000000..93027dd7ac --- /dev/null +++ b/test/output/identityScale.svg @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/plots/identity-scale.js b/test/plots/identity-scale.js index 26c3b73c8a..6fc8e4b269 100644 --- a/test/plots/identity-scale.js +++ b/test/plots/identity-scale.js @@ -1,4 +1,7 @@ import * as Plot from "@observablehq/plot"; +import * as d3 from "d3"; + +const random = d3.randomLcg(42); export default async function() { return Plot.plot({ @@ -14,8 +17,8 @@ export default async function() { }, marks: [ Plot.dot({length: 100}, { - x: () => 600 * Math.random(), - y: () => 100 + 500 * Math.random(), + x: () => 600 * random(), + y: () => 100 + 500 * random(), fill: () => "red", stroke: () => "blue" }) From 347ef9d9f473caa6ab37a9585a90bcac7302a4e0 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Thu, 8 Apr 2021 14:20:21 -0700 Subject: [PATCH 3/4] adopt scaleIdentity --- src/scales.js | 5 +++-- src/scales/quantitative.js | 22 ++++++++++++++++------ test/plots/identity-scale.js | 1 - 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/scales.js b/src/scales.js index 045da89676..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,7 +60,8 @@ 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": case undefined: return; + 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/plots/identity-scale.js b/test/plots/identity-scale.js index 6fc8e4b269..b916f72e1f 100644 --- a/test/plots/identity-scale.js +++ b/test/plots/identity-scale.js @@ -5,7 +5,6 @@ const random = d3.randomLcg(42); export default async function() { return Plot.plot({ - height: 396, x: { type: "identity" }, From d1a44e95aea668006d1754d21ee67baa69728c76 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Thu, 8 Apr 2021 14:21:38 -0700 Subject: [PATCH 4/4] fix test --- test/output/identityScale.svg | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/test/output/identityScale.svg b/test/output/identityScale.svg index 93027dd7ac..8227c3569a 100644 --- a/test/output/identityScale.svg +++ b/test/output/identityScale.svg @@ -1,4 +1,47 @@ + + + 350 + + + 300 + + + 250 + + + 200 + + + 150 + + + 100 + + + 50 + + + + + 100 + + + 200 + + + 300 + + + 400 + + + 500 + + + 600 + +