Skip to content

Commit 4e66e13

Browse files
authored
add Plot.shuffle (#564)
* add Plot.shuffle * add seed option for deterministic shuffle
1 parent af35f23 commit 4e66e13

File tree

6 files changed

+11002
-1
lines changed

6 files changed

+11002
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,14 @@ Plot.sort("body_mass_g", options) // show data in ascending body mass order
10471047

10481048
Sorts the data by the specified *order*, which can be an accessor function, a comparator function, or a channel value definition such as a field name.
10491049

1050+
### Plot.shuffle(*options*)
1051+
1052+
```js
1053+
Plot.shuffle(options) // show data in random order
1054+
```
1055+
1056+
Shuffles the data randomly. If a *seed* option is specified, a linear congruential generator with the given seed is used to generate random numbers deterministically; otherwise, Math.random is used.
1057+
10501058
### Plot.reverse(*options*)
10511059

10521060
```js

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export {Text, text, textX, textY} from "./marks/text.js";
1313
export {TickX, TickY, tickX, tickY} from "./marks/tick.js";
1414
export {filter} from "./transforms/filter.js";
1515
export {reverse} from "./transforms/reverse.js";
16-
export {sort} from "./transforms/sort.js";
16+
export {sort, shuffle} from "./transforms/sort.js";
1717
export {bin, binX, binY} from "./transforms/bin.js";
1818
export {group, groupX, groupY, groupZ} from "./transforms/group.js";
1919
export {normalize, normalizeX, normalizeY} from "./transforms/normalize.js";

src/transforms/sort.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import {randomLcg} from "d3";
12
import {ascendingDefined} from "../defined.js";
23
import {valueof} from "../mark.js";
34
import {basic} from "./basic.js";
45

6+
export function shuffle({seed, ...options} = {}) {
7+
return basic(options, sortValue(seed == null ? Math.random : randomLcg(seed)));
8+
}
9+
510
export function sort(value, options) {
611
return basic(options, sortTransform(value));
712
}

0 commit comments

Comments
 (0)