From b5f00bfdfedb62159e3b4bad7c2c62657beb96ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Fri, 18 Dec 2020 09:23:55 +0100 Subject: [PATCH 1/3] identity scale (closes #56) --- src/scales.js | 3 ++- src/scales/ordinal.js | 5 +++++ test/identity-scale-test.html | 27 +++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/identity-scale-test.html diff --git a/src/scales.js b/src/scales.js index ef76725ab7..054e976b47 100644 --- a/src/scales.js +++ b/src/scales.js @@ -1,7 +1,7 @@ import {registry, position, radius} from "./scales/index.js"; import {ScaleDiverging, ScaleLinear, ScalePow, ScaleLog, ScaleSymlog} from "./scales/quantitative.js"; import {ScaleTime, ScaleUtc} from "./scales/temporal.js"; -import {ScaleOrdinal, ScalePoint, ScaleBand} from "./scales/ordinal.js"; +import {ScaleOrdinal, ScalePoint, ScaleBand, ScaleIdentity} from "./scales/ordinal.js"; export function Scales(channels, {inset, round, nice, align, padding, ...options} = {}) { const scales = {}; @@ -43,6 +43,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 ScaleIdentity(key, channels, options); case undefined: return; default: throw new Error(`unknown scale type: ${options.type}`); } diff --git a/src/scales/ordinal.js b/src/scales/ordinal.js index ca92a0e301..e3a5c939dc 100644 --- a/src/scales/ordinal.js +++ b/src/scales/ordinal.js @@ -165,6 +165,11 @@ export function ScaleBand(key, channels, { ); } +// An identity scale that works for strings and numbers +export function ScaleIdentity(key, channels, options) { + return ScaleO(Object.assign(x => x, { domain: () => {} }), channels, options); +} + function inferDomain(channels) { const domain = new Set(); for (const {value} of channels) { diff --git a/test/identity-scale-test.html b/test/identity-scale-test.html new file mode 100644 index 0000000000..ed1e205669 --- /dev/null +++ b/test/identity-scale-test.html @@ -0,0 +1,27 @@ + + + + + + From 068431abc3fafc1ea2f448385f2863ec993a404d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Fri, 18 Dec 2020 09:43:48 +0100 Subject: [PATCH 2/3] ordinal and quantitative identity --- src/scales/ordinal.js | 20 +++++++++++++++++--- test/identity-scale-test.html | 7 ++++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/scales/ordinal.js b/src/scales/ordinal.js index e3a5c939dc..c654932e39 100644 --- a/src/scales/ordinal.js +++ b/src/scales/ordinal.js @@ -1,5 +1,5 @@ import {reverse, sort} from "d3-array"; -import {scaleBand, scaleOrdinal, scalePoint} from "d3-scale"; +import {scaleBand, scaleIdentity, scaleOrdinal, scalePoint} from "d3-scale"; import { schemeAccent, schemeBlues, @@ -41,6 +41,7 @@ import { } from "d3-scale-chromatic"; import {ascendingDefined} from "../defined.js"; import {registry, color} from "./index.js"; +import {ScaleQ} from "./quantitative.js"; // TODO Allow this to be extended. const schemes = new Map([ @@ -165,9 +166,22 @@ export function ScaleBand(key, channels, { ); } -// An identity scale that works for strings and numbers export function ScaleIdentity(key, channels, options) { - return ScaleO(Object.assign(x => x, { domain: () => {} }), channels, options); + let type = "quantitative"; + for (const c of channels) { + for (const v of c.value) { + if (typeof v === "string") { + type = "ordinal"; + break; + } + } + } + switch (type) { + case "ordinal": + return ScaleO(Object.assign(x => x, { domain: () => {} }), channels, options); + case "quantitative": + return ScaleQ(key, scaleIdentity(), channels, options); + } } function inferDomain(channels) { diff --git a/test/identity-scale-test.html b/test/identity-scale-test.html index ed1e205669..7ae033ab76 100644 --- a/test/identity-scale-test.html +++ b/test/identity-scale-test.html @@ -6,12 +6,13 @@