You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/features/marks.md
+139
Original file line number
Diff line number
Diff line change
@@ -547,6 +547,8 @@ All marks support the following [transform](./transforms.md) options:
547
547
548
548
The **sort** option, when not specified as a channel value (such as a field name or an accessor function), can also be used to [impute ordinal scale domains](./scales.md#sort-mark-option).
549
549
550
+
The **render** option allows to override or extend the default mark’s [rendering](#rendering) method.
551
+
550
552
### Insets
551
553
552
554
Rect-like marks support insets: a positive inset moves the respective side in (towards the opposing side), whereas a negative inset moves the respective side out (away from the opposing side). Insets are specified in pixels using the following options:
@@ -588,3 +590,140 @@ Plot.marks(
588
590
```
589
591
590
592
A convenience method for composing a mark from a series of other marks. Returns an array of marks that implements the *mark*.plot function. See the [box mark](../marks/box.md) implementation for an example.
593
+
594
+
## Rendering
595
+
596
+
A mark’s render method is called for each facet, unless its data for that facet is empty. The render method is responsible for drawing the mark by producing an SVG element.
597
+
598
+
:::warning
599
+
We do not recommend using this low-level interface when a more high-level option exists such as a [data transform](https://observablehq.com/plot/features/transforms). It is meant for use by extension developers more than by users.
600
+
:::
601
+
602
+
The mark’s render function is called with the following five arguments:
603
+
604
+
**index*: the index of the facet
605
+
**scales*: the scale functions and descriptors
606
+
**values*: the scaled and raw channels
607
+
**dimensions*: the dimensions of the facet
608
+
**context*: the context
609
+
610
+
The function is expected to return a single SVG node, or null or undefined if no output is desired for the current facet. Typically, it returns a [G element](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/g), with a child node (say, a [circle element](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/circle)) for each valid data point.
611
+
612
+
You can extend or replace this method by specifying a render transform with the mark’s **render** option. The render transform will be called with the five arguments described above and a sixth argument:
613
+
614
+
**next*: the next render method in the chain
615
+
616
+
The *index* is an array of indices in the channels, that represent the points to be drawn in the current facet. The *scales* object contains the scale functions, indexed by name, and an additional scales property with the scales descriptors, also indexed by name.
617
+
618
+
For example, the following code will log the color associated with the Torgersen category ("#e15759") and the [instantiated color scale object](./plots.md#plot_scale), and will not render anything to the chart.
The *values* object contains the scaled channels, indexed by name, and an additional channels property with the unscaled channels, also indexed by name. For example:
will output the following three lines to the console, with each line containing the index of the first penguin of the current facet, its fill color, and the underlying (unscaled) category:
648
+
649
+
```js
650
+
0'#e15759''Torgersen'
651
+
152'#f28e2c''Dream'
652
+
220'#4e79a7''Biscoe'
653
+
```
654
+
655
+
The *dimensions* object contains the marginTop, marginRight, marginLeft,marginBottom, and width and height of the chart. For example, to draw an ellipse that extends to the edges:
656
+
657
+
```js
658
+
Plot.plot({
659
+
marks: [
660
+
function (index, scales, values, dimensions, context) {
* document - the [document object](https://developer.mozilla.org/en-US/docs/Web/API/Document)
675
+
* ownerSVGElement - the chart’s bare svg element
676
+
* className - the [class name](./plots.md#other-options) of the chart (*e.g.*, "plot-d6a7b5")
677
+
* projection - the [projection](./projections.md) stream, if any
678
+
679
+
:::tip
680
+
When you write a plugin, using *context*.document allows your code to run in different contexts such as a server-side rendering environment.
681
+
:::
682
+
683
+
Render transforms are called with a sixth argument, *next*, a function that can be called to continue the render chain. For example, if you wish to animate a mark to fade in, you can render it as usual, immediately set its opacity to 0, then bring it to life with D3:
Note that Plot’s marks usually set the attributes of the nodes. As styles have precedence over attributes, the output can be customized with [CSS](https://developer.mozilla.org/en-US/docs/Web/CSS).
705
+
:::
706
+
707
+
Here is another example, where we render the dots one by one:
0 commit comments