Skip to content

Commit 3be866f

Browse files
authored
global zero option (#890)
* global zero option * tests for global scale options
1 parent 6202def commit 3be866f

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ Quantitative scales can be further customized with additional options:
199199
* *scale*.**zero** - if true, extend the domain to include zero if needed
200200
* *scale*.**percent** - if true, transform proportions in [0, 1] to percentages in [0, 100]
201201

202-
Clamping is typically used in conjunction with setting an explicit domain since if the domain is inferred, no values will be outside the domain. Clamping is useful for focusing on a subset of the data while ensuring that extreme values remain visible, but use caution: clamped values may need an annotation to avoid misinterpretation. Top-level **clamp** and **nice** options are supported as shorthand for setting *scale*.clamp and *scale*.nice on all scales.
202+
Clamping is typically used in conjunction with setting an explicit domain since if the domain is inferred, no values will be outside the domain. Clamping is useful for focusing on a subset of the data while ensuring that extreme values remain visible, but use caution: clamped values may need an annotation to avoid misinterpretation. Top-level **clamp**, **nice**, and **zero** options are supported as shorthand for setting the respective option on all scales.
203203

204204
The *scale*.**transform** option allows you to apply a function to all values before they are passed through the scale. This is convenient for transforming a scale’s data, say to convert to thousands or between temperature units.
205205

src/scales.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export function Scales(channelsByScale, {
1818
round,
1919
nice,
2020
clamp,
21+
zero,
2122
align,
2223
padding,
2324
...options
@@ -29,6 +30,7 @@ export function Scales(channelsByScale, {
2930
round: registry.get(key) === position ? round : undefined, // only for position
3031
nice,
3132
clamp,
33+
zero,
3234
align,
3335
padding,
3436
...scaleOptions

test/scales/scales-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,19 @@ it("plot(…).scale(name) promotes the given zero option to the domain", async (
339339
});
340340
});
341341

342+
it("plot(…).scale(name) promotes the given global zero option to the domain", async () => {
343+
const penguins = await d3.csv("data/penguins.csv", d3.autoType);
344+
const plot = Plot.dotX(penguins, {x: "body_mass_g"}).plot({zero: true});
345+
scaleEqual(plot.scale("x"), {
346+
type: "linear",
347+
domain: [0, 6300],
348+
range: [20, 620],
349+
interpolate: d3.interpolateNumber,
350+
clamp: false,
351+
label: "body_mass_g →"
352+
});
353+
});
354+
342355
it("plot(…).scale(name) handles the zero option correctly for descending domains", async () => {
343356
const penguins = await d3.csv("data/penguins.csv", d3.autoType);
344357
const plot = Plot.dotX(penguins, {x: "body_mass_g"}).plot({x: {zero: true, domain: [4000, 2000]}});
@@ -1197,6 +1210,7 @@ it("plot({inset, …}).scale('x').range respects the given scale-level inset", (
11971210
it("plot({clamp, …}).scale('x').clamp reflects the given clamp option", () => {
11981211
assert.strictEqual(Plot.dot([1, 2, 3], {x: d => d}).plot({x: {clamp: false}}).scale("x").clamp, false);
11991212
assert.strictEqual(Plot.dot([1, 2, 3], {x: d => d}).plot({x: {clamp: true}}).scale("x").clamp, true);
1213+
assert.strictEqual(Plot.dot([1, 2, 3], {x: d => d}).plot({clamp: true}).scale("x").clamp, true);
12001214
});
12011215

12021216
it("plot({align, …}).scale('x').align reflects the given align option for point scales", () => {
@@ -1344,6 +1358,7 @@ it("plot(…).scale(name).domain respects the given nice option", async () => {
13441358
assert.deepStrictEqual(Plot.dotX(penguins, {x: "body_mass_g"}).plot({x: {nice: true}}).scale("x").domain, [2500, 6500]);
13451359
assert.deepStrictEqual(Plot.dotX(penguins, {x: "body_mass_g"}).plot({x: {nice: 10}}).scale("x").domain, [2500, 6500]);
13461360
assert.deepStrictEqual(Plot.dotX(penguins, {x: "body_mass_g"}).plot({x: {nice: 5}}).scale("x").domain, [2000, 7000]);
1361+
assert.deepStrictEqual(Plot.dotX(penguins, {x: "body_mass_g"}).plot({nice: true}).scale("x").domain, [2500, 6500]);
13471362
});
13481363

13491364
it("plot(…).scale(name).domain nices an explicit domain, too", async () => {
@@ -1358,6 +1373,7 @@ it("plot(…).scale(name).interpolate reflects the round option for quantitative
13581373
const penguins = await d3.csv("data/penguins.csv", d3.autoType);
13591374
assert.strictEqual(Plot.dotX(penguins, {x: "body_mass_g"}).plot({x: {round: false}}).scale("x").interpolate, d3.interpolateNumber);
13601375
assert.strictEqual(Plot.dotX(penguins, {x: "body_mass_g"}).plot({x: {round: true}}).scale("x").interpolate, d3.interpolateRound);
1376+
assert.strictEqual(Plot.dotX(penguins, {x: "body_mass_g"}).plot({round: true}).scale("x").interpolate, d3.interpolateRound);
13611377
});
13621378

13631379
it("plot(…).scale(name).round reflects the round option for point scales", async () => {

0 commit comments

Comments
 (0)