diff --git a/README.md b/README.md index 0d747958ea..e7ceb7be0f 100644 --- a/README.md +++ b/README.md @@ -231,7 +231,7 @@ const plot2 = Plot.plot({…, color: plot1.scale("color")}); The returned scale object represents the actual (or “materialized”) values encountered in the plot, including the domain, range, interpolate function, *etc.* The scale’s label, if any, is also returned; however, note that other axis properties are not currently exposed. -For convenience, an apply method is exposed, which returns the scale’s output for any given input. When applicable, an invert method is exposed, which returns the corresponding input from the scale’s domain for any given output. +For convenience, an apply method is exposed, which returns the scale’s output for any given input. When applicable, an invert method is exposed, which returns the corresponding input from the scale’s domain for any given output. Point and band scales also expose their materialized bandwidth and step. The scale object is undefined if the associated plot has no scale with the given *name*, and throws an error if the *name* is invalid (*i.e.*, not one of the known scale names: *x*, *y*, *fx*, *fy*, *r*, *color*, or *opacity*). diff --git a/src/scales.js b/src/scales.js index fae86361cf..9dffed4168 100644 --- a/src/scales.js +++ b/src/scales.js @@ -413,6 +413,7 @@ function exposeScale({ // band, point ...scale.align && {align: scale.align(), round: scale.round()}, ...scale.padding && (scale.paddingInner ? {paddingInner: scale.paddingInner(), paddingOuter: scale.paddingOuter()} : {padding: scale.padding()}), + ...scale.bandwidth && {bandwidth: scale.bandwidth(), step: scale.step()}, // utilities apply: t => scale(t), diff --git a/test/scales/scales-test.js b/test/scales/scales-test.js index 600035d048..6165f4c895 100644 --- a/test/scales/scales-test.js +++ b/test/scales/scales-test.js @@ -101,6 +101,8 @@ it("plot(…).scale('x') can return a point scale", () => { range: [20, 620], padding: 0.5, align: 0.5, + bandwidth: 0, + step: 300, round: true }); }); @@ -113,6 +115,8 @@ it("plot(…).scale('x') can return a point scale, respecting the specified alig range: [20, 620], padding: -0.2, align: 1, + bandwidth: 0, + step: 600, round: true }); }); @@ -125,6 +129,8 @@ it("plot(…).scale('x') can promote a reversed point scale to a point scale wit range: [20, 620], padding: 0.5, align: 0.5, + bandwidth: 0, + step: 300, round: true }); }); @@ -138,6 +144,8 @@ it("plot(…).scale('x') can return a band scale", () => { paddingInner: 0.1, paddingOuter: 0.1, align: 0.5, + bandwidth: 257, + step: 285, round: true }); }); @@ -151,6 +159,8 @@ it("plot(…).scale('x') can return an explicit band scale", () => { paddingInner: 0.1, paddingOuter: 0.1, align: 0.5, + bandwidth: 257, + step: 285, round: true }); }); @@ -164,6 +174,8 @@ it("plot(…).scale('x') can promote a reversed band scale to a band scale with paddingInner: 0.1, paddingOuter: 0.1, align: 0.5, + bandwidth: 257, + step: 285, round: true }); }); @@ -188,6 +200,8 @@ it("plot(…).scale('y') can return a band scale", () => { paddingInner: 0.1, paddingOuter: 0.1, align: 0.5, + bandwidth: 25, + step: 28, round: true }); }); @@ -201,6 +215,8 @@ it("plot(…).scale('y') can return a band scale, respecting the specified align paddingInner: 0.1, paddingOuter: -0.2, align: 1, + bandwidth: 36, + step: 40, round: true }); }); @@ -227,6 +243,8 @@ it("plot(…).scale('fx') can return a band scale", () => { align: 0.5, paddingInner: 0.1, paddingOuter: 0, + bandwidth: 275, + step: 305, round: true }); }); @@ -241,6 +259,8 @@ it("plot(…).scale('fy') can return a band scale", () => { align: 0.5, paddingInner: 0.1, paddingOuter: 0, + bandwidth: 170, + step: 189, round: true }); });