Skip to content

Commit 759c46f

Browse files
committed
reusable interval option
1 parent 26ee70b commit 759c46f

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

src/scales.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ function exposeScale({
408408
range,
409409
label,
410410
interpolate,
411+
interval,
411412
transform,
412413
percent,
413414
pivot
@@ -422,6 +423,7 @@ function exposeScale({
422423
...percent && {percent}, // only exposed if truthy
423424
...label !== undefined && {label},
424425
...unknown !== undefined && {unknown},
426+
...interval !== undefined && {interval},
425427

426428
// quantitative
427429
...interpolate !== undefined && {interpolate},

src/scales/ordinal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function ScaleO(scale, channels, {
2828
if (typeof range === "function") range = range(domain);
2929
scale.range(range);
3030
}
31-
return {type, domain, range, scale, hint};
31+
return {type, domain, range, scale, hint, interval};
3232
}
3333

3434
export function ScaleOrdinal(key, channels, {

src/scales/quantitative.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export function ScaleQ(key, scale, channels, {
5757
unknown,
5858
round,
5959
scheme,
60+
interval,
6061
range = registry.get(key) === radius ? inferRadialRange(channels, domain) : registry.get(key) === length ? inferLengthRange(channels, domain) : registry.get(key) === opacity ? unit : undefined,
6162
interpolate = registry.get(key) === color ? (scheme == null && range !== undefined ? interpolateRgb : quantitativeScheme(scheme !== undefined ? scheme : type === "cyclical" ? "rainbow" : "turbo")) : round ? interpolateRound : interpolateNumber,
6263
reverse
@@ -105,7 +106,7 @@ export function ScaleQ(key, scale, channels, {
105106
if (nice) scale.nice(nice === true ? undefined : nice), domain = scale.domain();
106107
if (range !== undefined) scale.range(range);
107108
if (clamp) scale.clamp(clamp);
108-
return {type, domain, range, scale, interpolate};
109+
return {type, domain, range, scale, interpolate, interval};
109110
}
110111

111112
export function ScaleLinear(key, channels, options) {

test/scales/scales-test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,52 @@ it("plot(…).scale(name) reflects the given custom interpolator", async () => {
13921392
});
13931393
});
13941394

1395+
it("plot(…).scale(name).interval changes the domain and sets the transform option for ordinal scales", async () => {
1396+
const requests = [[2002,9],[2003,17],[2004,12],[2005,5],[2006,12],[2007,18],[2008,16],[2009,11],[2010,9],[2011,8],[2012,9],[2019,20]];
1397+
const plot = Plot.barY(requests, {x: "0", y: "1"}).plot({x: {interval: 1}});
1398+
scaleEqual(plot.scale("x"), {
1399+
align: 0.5,
1400+
bandwidth: 29,
1401+
domain: d3.range(2002, 2020),
1402+
interval: 1,
1403+
label: "0",
1404+
paddingInner: 0.1,
1405+
paddingOuter: 0.1,
1406+
range: [40, 620],
1407+
round: true,
1408+
step: 32,
1409+
type: "band"
1410+
});
1411+
});
1412+
1413+
it("plot(…).scale(name).interval reflects the interval option for quantitative scales", async () => {
1414+
const penguins = await d3.csv("data/penguins.csv", d3.autoType);
1415+
const plot = Plot.dotX(penguins, {x: "body_mass_g"}).plot({x: {interval: 50}});
1416+
scaleEqual(plot.scale("x"), {
1417+
clamp: false,
1418+
domain: [2700, 6300],
1419+
interpolate: d3.interpolateNumber,
1420+
interval: 50,
1421+
label: "body_mass_g →",
1422+
range: [20, 620],
1423+
type: "linear"
1424+
});
1425+
});
1426+
1427+
it("The interval option is reusable for ordinal scales", async () => {
1428+
const requests = [[2002,9],[2003.5,17],[2005.9,5]];
1429+
const plot1 = Plot.barY(requests, {x: "0", y: "1"}).plot({x: {interval: 1}, className: "a"});
1430+
const plot2 = Plot.barY(requests, {x: "0", y: "1"}).plot({x: plot1.scale("x"), className: "a"});
1431+
assert.strictEqual(plot1.innerHTML, plot2.innerHTML);
1432+
});
1433+
1434+
it("The interval option is reusable for quantitative scales", async () => {
1435+
const requests = [[2002,9],[2003.5,17],[2005.9,5]];
1436+
const plot1 = Plot.dot(requests, {x: "0", y: "1"}).plot({x: {interval: 1}, className: "a"});
1437+
const plot2 = Plot.dot(requests, {x: "0", y: "1"}).plot({x: plot1.scale("x"), className: "a"});
1438+
assert.strictEqual(plot1.innerHTML, plot2.innerHTML);
1439+
});
1440+
13951441
it("plot(…).scale('color') allows a range to be specified in conjunction with a scheme", async () => {
13961442
const gistemp = await d3.csv("data/gistemp.csv", d3.autoType);
13971443
const plot = Plot.dot(gistemp, {x: "Date", fill: "Anomaly"}).plot({color: {range: [0, 0.5], scheme: "cool"}});

0 commit comments

Comments
 (0)