Skip to content

Commit 8d2d1b3

Browse files
authored
include scale name in error (#1298)
1 parent 65b3651 commit 8d2d1b3

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

src/scales/ordinal.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ export function ScaleOrdinal(key, channels, {type, interval, domain, range, sche
5252
}
5353
}
5454
}
55-
if (unknown === scaleImplicit) throw new Error("implicit unknown is not supported");
55+
if (unknown === scaleImplicit) {
56+
throw new Error(`implicit unknown on ${key} scale is not supported`);
57+
}
5658
return ScaleO(key, scaleOrdinal().unknown(unknown), channels, {...options, type, domain, range, hint});
5759
}
5860

@@ -98,8 +100,9 @@ function inferDomain(channels, interval, key) {
98100
const [min, max] = extent(values).map(interval.floor, interval);
99101
return interval.range(min, interval.offset(max));
100102
}
101-
if (values.size > 10e3 && registry.get(key) === position)
102-
throw new Error("implicit ordinal position domain has more than 10,000 values");
103+
if (values.size > 10e3 && registry.get(key) === position) {
104+
throw new Error(`implicit ordinal domain of ${key} scale has more than 10,000 values`);
105+
}
103106
return sort(values, ascendingDefined);
104107
}
105108

src/scales/quantitative.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
quantile,
1414
quantize,
1515
reverse as reverseof,
16-
pairs,
1716
scaleLinear,
1817
scaleLog,
1918
scalePow,
@@ -225,9 +224,9 @@ export function ScaleThreshold(
225224
reverse
226225
}
227226
) {
228-
const sign = orderof(arrayify(domain)); // preserve descending domain
229-
if (!pairs(domain).every(([a, b]) => isOrdered(a, b, sign)))
230-
throw new Error(`the ${key} scale has a non-monotonic domain`);
227+
domain = arrayify(domain);
228+
const sign = orderof(domain); // preserve descending domain
229+
if (!isOrdered(domain, sign)) throw new Error(`the ${key} scale has a non-monotonic domain`);
231230
if (reverse) range = reverseof(range); // domain ascending, so reverse range
232231
return {
233232
type: "threshold",
@@ -237,9 +236,12 @@ export function ScaleThreshold(
237236
};
238237
}
239238

240-
function isOrdered(a, b, sign) {
241-
const s = descending(a, b);
242-
return s === 0 || s === sign;
239+
function isOrdered(domain, sign) {
240+
for (let i = 1, n = domain.length, d = domain[0]; i < n; ++i) {
241+
const s = descending(d, (d = domain[i]));
242+
if (s !== 0 && s !== sign) return false;
243+
}
244+
return true;
243245
}
244246

245247
export function ScaleIdentity() {

test/scales/scales-test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import it from "../jsdom.js";
55

66
it("Plot throws an error if an ordinal position scale has a huge inferred domain", () => {
77
assert.ok(Plot.cellX({length: 10000}, {x: d3.randomLcg(42)}).plot());
8-
assert.throws(() => Plot.cellX({length: 10001}, {x: d3.randomLcg(42)}).plot());
8+
assert.throws(() => Plot.cellX({length: 10001}, {x: d3.randomLcg(42)}).plot(), /implicit ordinal domain of x scale/);
9+
});
10+
11+
it("Plot throws an error if scale.unknown is set to d3.scaleImplicit", () => {
12+
assert.throws(
13+
() => Plot.plot({color: {type: "ordinal", unknown: d3.scaleImplicit}}),
14+
/implicit unknown on color scale/
15+
);
916
});
1017

1118
it("Plot does not throw an error if an ordinal color scale has a huge inferred domain", () => {

0 commit comments

Comments
 (0)