diff --git a/docs/features/transforms.md b/docs/features/transforms.md index dac990022e..4c309c985c 100644 --- a/docs/features/transforms.md +++ b/docs/features/transforms.md @@ -181,7 +181,7 @@ The **transform** function is passed three arguments, *data*, *facets*, and *opt If the **transform** option is specified, it supersedes any basic transforms (*i.e.*, the **filter**, **sort** and **reverse** options are ignored). However, the **transform** option is rarely used directly; instead one of Plot’s built-in transforms are used, and these transforms automatically compose with the basic **filter**, **sort** and **reverse** transforms. -While transform functions often produce new *data* or *facets*, they may return the passed-in *data* and *facets* as-is, and often have a side-effect of constructing derived channels. For example, the count of elements in a [groupX transform](../transforms/group.md) might be returned as a new *y* channel. In this case, the transform is typically expressed as an options transform: a function that takes a mark *options* object and returns a new, transformed options object, where the returned options object implements the **transform** option. Transform functions should not mutate the input *data* or *facets*. Likewise options transforms should not mutate the input *options* object. +While transform functions often produce new *data* or *facets*, they may return the passed-in *data* and *facets* as-is, and often have a side effect of constructing derived channels. For example, the count of elements in a [groupX transform](../transforms/group.md) might be returned as a new *y* channel. In this case, the transform is typically expressed as an options transform: a function that takes a mark *options* object and returns a new, transformed options object, where the returned options object implements the **transform** option. Transform functions should not mutate the input *data* or *facets*. Likewise options transforms should not mutate the input *options* object. When implementing a custom transform for generic usage, keep in mind that it needs to be compatible with Plot’s [faceting system](./facets.md), which partitions the original dataset into discrete subsets. diff --git a/package.json b/package.json index 59da041c0d..b875fe73c4 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@observablehq/plot": "./src/index.js" }, "sideEffects": [ - "./src/plot.js" + "./src/index.js" ], "devDependencies": { "@observablehq/runtime": "^5.7.3", diff --git a/src/index.js b/src/index.js index 9fde7ce2d5..9d8c162260 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,11 @@ +import {Mark} from "./mark.js"; +import {plot} from "./plot.js"; + +// Note: this side effect avoids a circular dependency. +Mark.prototype.plot = function ({marks = [], ...options} = {}) { + return plot({...options, marks: [...marks, this]}); +}; + export {plot} from "./plot.js"; export {Mark, marks} from "./mark.js"; export {Area, area, areaX, areaY} from "./marks/area.js"; diff --git a/src/mark.js b/src/mark.js index 0e867689b5..768105025e 100644 --- a/src/mark.js +++ b/src/mark.js @@ -131,7 +131,7 @@ export class Mark { } export function marks(...marks) { - marks.plot = Mark.prototype.plot; // Note: depends on side-effect in plot! + marks.plot = Mark.prototype.plot; return marks; } diff --git a/src/plot.js b/src/plot.js index 628108bd65..f0829af9cc 100644 --- a/src/plot.js +++ b/src/plot.js @@ -368,13 +368,6 @@ function createFigcaption(document, caption) { return e; } -function plotThis({marks = [], ...options} = {}) { - return plot({...options, marks: [...marks, this]}); -} - -// Note: This side-effect avoids a circular dependency. -Mark.prototype.plot = plotThis; - function flatMarks(marks) { return marks .flat(Infinity) diff --git a/src/transforms/basic.d.ts b/src/transforms/basic.d.ts index fddc507801..d120ecf917 100644 --- a/src/transforms/basic.d.ts +++ b/src/transforms/basic.d.ts @@ -10,7 +10,7 @@ import type {ScaleFunctions} from "../scales.js"; * the data, *facets*, and the plot’s *options*. The transform function returns * new mark data and facets; the returned **data** defaults to the passed * *data*, and the returned **facets** defaults to the passed *facets*. The mark - * is the *this* context. Transform functions can also trigger side-effects, say + * is the *this* context. Transform functions can also trigger side effects, say * to populate lazily-derived columns; see also Plot.column. */ export type TransformFunction = (