Skip to content

Commit 609e3b2

Browse files
committed
rename to quantize
1 parent 120b1a7 commit 609e3b2

File tree

9 files changed

+50
-41
lines changed

9 files changed

+50
-41
lines changed

docs/.vitepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export default defineConfig({
111111
{text: "Interval", link: "/transforms/interval"},
112112
{text: "Map", link: "/transforms/map"},
113113
{text: "Normalize", link: "/transforms/normalize"},
114+
{text: "Quantize", link: "/transforms/quantize"},
114115
{text: "Select", link: "/transforms/select"},
115116
{text: "Sort", link: "/transforms/sort"},
116117
{text: "Stack", link: "/transforms/stack"},

docs/transforms/group.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ onMounted(() => {
1616
# Group transform
1717

1818
:::tip
19-
The group transform is for aggregating ordinal or nominal data. For quantitative or temporal data, use the [bin transform](./bin.md), or use the [interval transform](./interval.md) to make continuous data ordinal.
19+
The group transform is for aggregating ordinal or nominal data. For quantitative or temporal data, use the [bin transform](./bin.md), or use the [quantize transform](./quantize.md) to make continuous data ordinal.
2020
:::
2121

2222
The **group transform** groups ordinal or nominal data—discrete values such as name, type, or category. You can then compute summary statistics for each group, such as a count, sum, or proportion. The group transform is most often used to make bar charts with the [bar mark](../marks/bar.md).
@@ -310,14 +310,14 @@ Plot.plot({
310310
Although barX applies an implicit stackX transform, [textX](../marks/text.md) does not; this example uses an explicit stackX transform in both cases for clarity, and to pass the additional **order** and **reverse** options to place the largest sport on the left. The [filter transform](./filter.md) is applied after the stack transform to hide the labels on the smallest sports where the bars are too thin.
311311
:::
312312

313-
While you should generally use the [bin transform](./bin.md) for quantitative or temporal data, you can use the [group transform](./group.md) if you also use the [interval transform](./interval.md) to make the data ordinal.
313+
While you should generally use the [bin transform](./bin.md) for quantitative or temporal data, you can use the [group transform](./group.md) if you also use the [quantize transform](./quantize.md) to make the data ordinal.
314314

315315
:::plot defer
316316
```js
317317
Plot.plot({
318318
x: {tickFormat: "%Y"},
319319
marks: [
320-
Plot.barY(olympians, Plot.groupX({y: "count"}, Plot.intervalX(d3.utcYear.every(5), {x: "date_of_birth"})))
320+
Plot.barY(olympians, Plot.groupX({y: "count"}, Plot.quantizeX(d3.utcYear.every(5), {x: "date_of_birth"})))
321321
]
322322
})
323323
```

docs/transforms/quantize.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Quantize transform
2+
3+
:::tip
4+
There’s also an [**interval** scale option](../features/scales.md#scale-transforms) for quantizing continuous data.
5+
:::
6+
7+
TODO Describe the **quantize transform**.

src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ export * from "./transforms/centroid.js";
4545
export * from "./transforms/dodge.js";
4646
export * from "./transforms/group.js";
4747
export * from "./transforms/hexbin.js";
48-
export * from "./transforms/interval.js";
4948
export * from "./transforms/map.js";
5049
export * from "./transforms/normalize.js";
50+
export * from "./transforms/quantize.js";
5151
export * from "./transforms/select.js";
5252
export * from "./transforms/stack.js";
5353
export * from "./transforms/tree.js";

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ export {centroid, geoCentroid} from "./transforms/centroid.js";
3333
export {dodgeX, dodgeY} from "./transforms/dodge.js";
3434
export {group, groupX, groupY, groupZ} from "./transforms/group.js";
3535
export {hexbin} from "./transforms/hexbin.js";
36-
export {intervalX, intervalY, intervalMap} from "./transforms/interval.js";
3736
export {normalize, normalizeX, normalizeY} from "./transforms/normalize.js";
3837
export {map, mapX, mapY} from "./transforms/map.js";
3938
export {window, windowX, windowY} from "./transforms/window.js";
39+
export {quantizeX, quantizeY, quantizeMap} from "./transforms/quantize.js";
4040
export {select, selectFirst, selectLast, selectMaxX, selectMaxY, selectMinX, selectMinY} from "./transforms/select.js";
4141
export {stackX, stackX1, stackX2, stackY, stackY1, stackY2} from "./transforms/stack.js";
4242
export {treeNode, treeLink} from "./transforms/tree.js";

src/transforms/interval.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
11
import {isTemporal, labelof, map, maybeInterval, maybeValue, valueof} from "../options.js";
22
import {maybeInsetX, maybeInsetY} from "./inset.js";
3-
import {mapX, mapY} from "./map.js";
4-
5-
export function intervalX(interval, options) {
6-
if (arguments.length === 1) ({interval, ...options} = interval);
7-
return mapX(intervalMap(interval), options);
8-
}
9-
10-
export function intervalY(interval, options) {
11-
if (arguments.length === 1) ({interval, ...options} = interval);
12-
return mapY(intervalMap(interval), options);
13-
}
14-
15-
export function intervalMap(interval, type) {
16-
interval = maybeInterval(interval, type);
17-
return {
18-
mapIndex(I, S, T) {
19-
T.interval = interval; // hint for scales
20-
for (let i = 0, n = I.length; i < n; ++i) {
21-
T[I[i]] = interval.floor(S[I[i]]);
22-
}
23-
}
24-
};
25-
}
263

274
// The interval may be specified either as x: {value, interval} or as {x,
285
// interval}. The former can be used to specify separate intervals for x and y,

src/transforms/interval.d.ts renamed to src/transforms/quantize.d.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type {Interval} from "../interval.js";
22
import type {Transformed} from "./basic.js";
33
import type {Map} from "./map.js";
44

5-
/** Options for the interval transform. */
6-
export interface IntervalOptions {
5+
/** Options for the quantize transform. */
6+
export interface QuantizeOptions {
77
/**
88
* How to quantize the continuous data; one of:
99
*
@@ -14,7 +14,7 @@ export interface IntervalOptions {
1414
* For example, for integer bins:
1515
*
1616
* ```js
17-
* Plot.barY(numbers, Plot.groupX({y: "count"}, Plot.intervalX(1)))
17+
* Plot.barY(numbers, Plot.groupX({y: "count"}, Plot.quantizeX(1)))
1818
* ```
1919
*/
2020
interval?: Interval;
@@ -24,23 +24,23 @@ export interface IntervalOptions {
2424
* Derives new **x**, **x1**, and **x2** channels for each corresponding input
2525
* channel by quantizing to the given *interval*.
2626
*/
27-
export function intervalX<T>(interval?: Interval, options?: T): Transformed<T>;
28-
export function intervalX<T>(options?: T & IntervalOptions): Transformed<T>;
27+
export function quantizeX<T>(interval?: Interval, options?: T): Transformed<T>;
28+
export function quantizeX<T>(options?: T & QuantizeOptions): Transformed<T>;
2929

3030
/**
3131
* Derives new **y**, **y1**, and **y2** channels for each corresponding input
3232
* channel by quantizing to the given *interval*.
3333
*/
34-
export function intervalY<T>(interval?: Interval, options?: T): Transformed<T>;
35-
export function intervalY<T>(options?: T & IntervalOptions): Transformed<T>;
34+
export function quantizeY<T>(interval?: Interval, options?: T): Transformed<T>;
35+
export function quantizeY<T>(options?: T & QuantizeOptions): Transformed<T>;
3636

3737
/**
3838
* Given an *interval*, returns a corresponding map implementation for use with
39-
* the map transform, allowing the normalization of arbitrary channels instead
40-
* of only **x** and **y**. For example, to interval the **stroke** channel:
39+
* the map transform, allowing the quantization of arbitrary channels instead of
40+
* only **x** and **y**. For example, to quantize the **stroke** channel:
4141
*
4242
* ```js
43-
* Plot.map({stroke: Plot.intervalMap(10)}, {x: "Date", stroke: "Close", stroke: "Symbol"})
43+
* Plot.map({stroke: Plot.quantizeMap(10)}, {x: "Date", stroke: "Close", stroke: "Symbol"})
4444
* ```
4545
*/
46-
export function intervalMap(interval: Interval): Map;
46+
export function quantizeMap(interval: Interval): Map;

src/transforms/quantize.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {maybeInterval} from "../options.js";
2+
import {mapX, mapY} from "./map.js";
3+
4+
export function quantizeX(interval, options) {
5+
if (arguments.length === 1) ({interval, ...options} = interval);
6+
return mapX(quantizeMap(interval), options);
7+
}
8+
9+
export function quantizeY(interval, options) {
10+
if (arguments.length === 1) ({interval, ...options} = interval);
11+
return mapY(quantizeMap(interval), options);
12+
}
13+
14+
export function quantizeMap(interval, type) {
15+
interval = maybeInterval(interval, type);
16+
return {
17+
mapIndex(I, S, T) {
18+
T.interval = interval; // hint for scales
19+
for (let i = 0, n = I.length; i < n; ++i) {
20+
T[I[i]] = interval.floor(S[I[i]]);
21+
}
22+
}
23+
};
24+
}

test/plots/group-interval.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export async function groupIntervalYear() {
55
const olympians = await d3.csv<any>("data/athletes.csv", d3.autoType);
66
return Plot.plot({
77
x: {tickFormat: "%Y"},
8-
marks: [Plot.barY(olympians, Plot.groupX({y: "count"}, Plot.intervalX(d3.utcYear.every(5), {x: "date_of_birth"})))]
8+
marks: [Plot.barY(olympians, Plot.groupX({y: "count"}, Plot.quantizeX(d3.utcYear.every(5), {x: "date_of_birth"})))]
99
});
1010
}
1111

@@ -17,6 +17,6 @@ export async function groupIntervalYearSparse() {
1717
});
1818
return Plot.plot({
1919
x: {tickFormat: "%Y"},
20-
marks: [Plot.barY(olympians, Plot.groupX({y: "count"}, Plot.intervalX(d3.utcYear.every(5), {x: "date_of_birth"})))]
20+
marks: [Plot.barY(olympians, Plot.groupX({y: "count"}, Plot.quantizeX(d3.utcYear.every(5), {x: "date_of_birth"})))]
2121
});
2222
}

0 commit comments

Comments
 (0)