Skip to content

Commit 8dab9dc

Browse files
committed
expose Plot.transform and Plot.column
1 parent e0bfe9c commit 8dab9dc

File tree

5 files changed

+2133
-6
lines changed

5 files changed

+2133
-6
lines changed

README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,7 @@ Plot’s transforms provide a convenient mechanism for transforming data as part
13311331
* **filter** - filters data according to the specified accessor or values
13321332
* **sort** - sorts data according to the specified comparator, accessor, or values
13331333
* **reverse** - reverses the sorted (or if not sorted, the input) data order
1334+
* **transform** - a function that returns transformed *data* and *facets*
13341335
13351336
For example, to draw bars only for letters that commonly form vowels:
13361337
@@ -1346,10 +1347,6 @@ Plot.barY(alphabet.filter(d => /[aeiou]/i.test(d.letter)), {x: "letter", y: "fre
13461347
13471348
Together the **sort** and **reverse** transforms allow control over *z*-order, which can be important when addressing overplotting. If the sort option is a function but does not take exactly one argument, it is assumed to be a [comparator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description); otherwise, the sort option is interpreted as a channel value definition and thus may be either as a column name, accessor function, or array of values.
13481349
1349-
For greater control, you can also implement a custom transform function:
1350-
1351-
* **transform** - a function that returns transformed *data* and *index*
1352-
13531350
The basic transforms are composable: the *filter* transform is applied first, then *sort*, then *reverse*. If a custom *transform* option is specified directly, it supersedes any basic transforms (*i.e.*, the *filter*, *sort* and *reverse* options are ignored). However, the *transform* option is rarely used directly; instead an option transform is used. These option transforms automatically compose with the basic *filter*, *sort* and *reverse* transforms.
13541351
13551352
Plot’s option transforms, listed below, do more than populate the **transform** function: they derive new mark options and channels. These transforms take a mark’s *options* object (and possibly transform-specific options as the first argument) and return a new, transformed, *options*. Option transforms are composable: you can pass an *options* objects through more than one transform before passing it to a mark. You can also reuse the same transformed *options* on multiple marks.
@@ -1974,6 +1971,26 @@ If *marker* is true, it defaults to *circle*. If *marker* is a function, it will
19741971
19751972
The primary color of a marker is inherited from the *stroke* of the associated mark. The *arrow* marker is [automatically oriented](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/orient) such that it points in the tangential direction of the path at the position the marker is placed. The *circle* markers are centered around the given vertex. Note that lines whose curve is not *linear* (the default), markers are not necessarily drawn at the data positions given by *x* and *y*; marker placement is determined by the (possibly Bézier) path segments generated by the curve. To ensure that symbols are drawn at a given *x* and *y* position, consider using a [dot](#dot).
19761973
1974+
### Custom transforms
1975+
1976+
For greater control, you can also implement a custom transform function, by defining the following option:
1977+
1978+
* **transform** - a function that returns transformed *data* and *facets*
1979+
1980+
The arguments of the *transform* function are *data* and *facets*. The incoming *facets* is an array of facets, each facet being an array of indices into the incoming *data* array. The transform must return an object with two properties *{data, facets}* with the same structure. (The new data and facets can, of course, be different from the incoming values.)
1981+
1982+
A custom transform might also generate new channels (for example, the count of elements in a groupX transform might be returned as a new channel *y*). The following helpers are useful to build new custom transforms:
1983+
1984+
#### Plot.transform(*options*, *transform*)
1985+
1986+
Returns an options object with a new transform that corresponds to the specified *transform* function composed with any preceding transform.
1987+
1988+
#### Plot.column(*x*)
1989+
1990+
Returns [*X*, *setX*], a channel and a setter. *X* can be returned immediately as a new channel, and *setX* can be called to fill *X* when the transform is applied to the data. The resulting channel *X* is an object with a *transform* method that returns the materialized column of values instantiated by *setX*.
1991+
1992+
If *x* is a string or has a label, a label is automatically created on the resulting channel.
1993+
19771994
## Formats
19781995
19791996
These helper functions are provided for use as a *scale*.tickFormat [axis option](#position-options), as the text option for [Plot.text](#plottextdata-options), or for general use. See also [d3-format](https://github.com/d3/d3-format), [d3-time-format](https://github.com/d3/d3-time-format), and JavaScript’s built-in [date formatting](https://observablehq.com/@mbostock/date-formatting) and [number formatting](https://observablehq.com/@mbostock/number-formatting).

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export {RuleX, RuleY, ruleX, ruleY} from "./marks/rule.js";
1414
export {Text, text, textX, textY} from "./marks/text.js";
1515
export {TickX, TickY, tickX, tickY} from "./marks/tick.js";
1616
export {Vector, vector, vectorX, vectorY} from "./marks/vector.js";
17-
export {valueof} from "./options.js";
18-
export {filter, reverse, sort, shuffle} from "./transforms/basic.js";
17+
export {valueof, lazyChannel as column} from "./options.js";
18+
export {filter, reverse, sort, shuffle, basic as transform} from "./transforms/basic.js";
1919
export {bin, binX, binY} from "./transforms/bin.js";
2020
export {group, groupX, groupY, groupZ} from "./transforms/group.js";
2121
export {normalize, normalizeX, normalizeY} from "./transforms/normalize.js";

0 commit comments

Comments
 (0)