Skip to content

Commit 2545560

Browse files
Filmbostock
andauthored
Plot.scale(x) exposes bandwidth and step when applicable (#743)
* Plot.scale(x) exposes bandwidth and step when applicable closes #741 * Update src/scales.js Co-authored-by: Mike Bostock <[email protected]> * fix tests * Update README.md Co-authored-by: Mike Bostock <[email protected]>
1 parent 1c27551 commit 2545560

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ const plot2 = Plot.plot({…, color: plot1.scale("color")});
231231

232232
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.
233233

234-
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.
234+
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.
235235

236236
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*).
237237

src/scales.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ function exposeScale({
413413
// band, point
414414
...scale.align && {align: scale.align(), round: scale.round()},
415415
...scale.padding && (scale.paddingInner ? {paddingInner: scale.paddingInner(), paddingOuter: scale.paddingOuter()} : {padding: scale.padding()}),
416+
...scale.bandwidth && {bandwidth: scale.bandwidth(), step: scale.step()},
416417

417418
// utilities
418419
apply: t => scale(t),

test/scales/scales-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ it("plot(…).scale('x') can return a point scale", () => {
101101
range: [20, 620],
102102
padding: 0.5,
103103
align: 0.5,
104+
bandwidth: 0,
105+
step: 300,
104106
round: true
105107
});
106108
});
@@ -113,6 +115,8 @@ it("plot(…).scale('x') can return a point scale, respecting the specified alig
113115
range: [20, 620],
114116
padding: -0.2,
115117
align: 1,
118+
bandwidth: 0,
119+
step: 600,
116120
round: true
117121
});
118122
});
@@ -125,6 +129,8 @@ it("plot(…).scale('x') can promote a reversed point scale to a point scale wit
125129
range: [20, 620],
126130
padding: 0.5,
127131
align: 0.5,
132+
bandwidth: 0,
133+
step: 300,
128134
round: true
129135
});
130136
});
@@ -138,6 +144,8 @@ it("plot(…).scale('x') can return a band scale", () => {
138144
paddingInner: 0.1,
139145
paddingOuter: 0.1,
140146
align: 0.5,
147+
bandwidth: 257,
148+
step: 285,
141149
round: true
142150
});
143151
});
@@ -151,6 +159,8 @@ it("plot(…).scale('x') can return an explicit band scale", () => {
151159
paddingInner: 0.1,
152160
paddingOuter: 0.1,
153161
align: 0.5,
162+
bandwidth: 257,
163+
step: 285,
154164
round: true
155165
});
156166
});
@@ -164,6 +174,8 @@ it("plot(…).scale('x') can promote a reversed band scale to a band scale with
164174
paddingInner: 0.1,
165175
paddingOuter: 0.1,
166176
align: 0.5,
177+
bandwidth: 257,
178+
step: 285,
167179
round: true
168180
});
169181
});
@@ -188,6 +200,8 @@ it("plot(…).scale('y') can return a band scale", () => {
188200
paddingInner: 0.1,
189201
paddingOuter: 0.1,
190202
align: 0.5,
203+
bandwidth: 25,
204+
step: 28,
191205
round: true
192206
});
193207
});
@@ -201,6 +215,8 @@ it("plot(…).scale('y') can return a band scale, respecting the specified align
201215
paddingInner: 0.1,
202216
paddingOuter: -0.2,
203217
align: 1,
218+
bandwidth: 36,
219+
step: 40,
204220
round: true
205221
});
206222
});
@@ -227,6 +243,8 @@ it("plot(…).scale('fx') can return a band scale", () => {
227243
align: 0.5,
228244
paddingInner: 0.1,
229245
paddingOuter: 0,
246+
bandwidth: 275,
247+
step: 305,
230248
round: true
231249
});
232250
});
@@ -241,6 +259,8 @@ it("plot(…).scale('fy') can return a band scale", () => {
241259
align: 0.5,
242260
paddingInner: 0.1,
243261
paddingOuter: 0,
262+
bandwidth: 170,
263+
step: 189,
244264
round: true
245265
});
246266
});

0 commit comments

Comments
 (0)