|
1 | 1 | import type {ChannelValue, ChannelValueSpec} from "../channel.js";
|
2 | 2 | import type {Data, FrameAnchor, MarkOptions, RenderableMark} from "../mark.js";
|
3 | 3 |
|
| 4 | +/** The built-in vector shape implementations. */ |
4 | 5 | export type VectorShapeName = "arrow" | "spike";
|
5 | 6 |
|
6 |
| -/** |
7 |
| - * A custom vector shape implementation. |
8 |
| - */ |
| 7 | +/** A vector shape implementation. */ |
9 | 8 | export interface VectorShapeImplementation {
|
10 | 9 | draw(context: CanvasPath, length: number, radius: number): void;
|
11 | 10 | }
|
12 | 11 |
|
| 12 | +/** How to draw a vector. */ |
13 | 13 | export type VectorShape = VectorShapeName | VectorShapeImplementation;
|
14 | 14 |
|
| 15 | +/** Options for the vector mark. */ |
15 | 16 | export interface VectorOptions extends MarkOptions {
|
16 |
| - /** A channel for the horizontal position. */ |
| 17 | + /** |
| 18 | + * The horizontal position of the vector’s anchor point; an optional channel |
| 19 | + * bound to the *x* scale. Default depends on the **frameAnchor**. |
| 20 | + */ |
17 | 21 | x?: ChannelValueSpec;
|
18 |
| - /** A channel for the vertical position. */ |
| 22 | + |
| 23 | + /** |
| 24 | + * The vertical position of the vector’s anchor point; an optional channel bound to the |
| 25 | + * *y* scale. Default depends on the **frameAnchor**. |
| 26 | + */ |
19 | 27 | y?: ChannelValueSpec;
|
| 28 | + |
20 | 29 | /**
|
21 |
| - * The radius channel (in pixels) describes the mark’s secondary dimension |
22 |
| - * (typically the *arrow* head’s span, or the *spike*’s base). |
| 30 | + * The vector shape’s radius, such as half the width of the *arrow*’s head or |
| 31 | + * the *spike*’s base; a constant number in pixels. Defaults to 3.5 pixels. |
23 | 32 | */
|
24 |
| - r?: ChannelValueSpec; |
| 33 | + r?: number; |
| 34 | + |
25 | 35 | /**
|
26 |
| - * The length of the shape, typically associated with a linear *length* scale. |
| 36 | + * The vector’s length; either an optional channel bound to the *length* scale |
| 37 | + * or a constant number in pixels. Defaults to 12 pixels. |
27 | 38 | */
|
28 | 39 | length?: ChannelValueSpec;
|
29 |
| - /** Vector rotation, in degrees; with rotate = 0, the shape points up. */ |
| 40 | + |
| 41 | + /** |
| 42 | + * The vector’s orientation (rotation angle); either a constant number in |
| 43 | + * degrees clockwise, or an optional channel (with no associated scale). |
| 44 | + * Defaults to 0 degrees with the vector pointing up; positive angles proceed |
| 45 | + * clockwise. |
| 46 | + */ |
30 | 47 | rotate?: ChannelValue;
|
31 |
| - /** Vector shape, as a name (*arrow* or *spike*), or as a custom implementation. */ |
| 48 | + |
| 49 | + /** The shape of the vector; a constant. Defaults to *arrow*. */ |
32 | 50 | shape?: VectorShape;
|
33 |
| - /** Vector anchor; by default a vector is anchored at its base (*start*). */ |
| 51 | + |
| 52 | + /** |
| 53 | + * The vector’s position along its orientation relative to its anchor point; a |
| 54 | + * constant. Assuming a default **rotate** angle of 0°, one of: |
| 55 | + * |
| 56 | + * * *start* - from [*x*, *y*] to [*x*, *y* - *l*] |
| 57 | + * * *middle* (default) - from [*x*, *y* + *l* / 2] to [*x*, *y* - *l* / 2] |
| 58 | + * * *end* - from [*x*, *y* + *l*] to [*x*, *y*] |
| 59 | + * |
| 60 | + * where [*x*, *y*] is the vector’s anchor point and *l* is the vector’s |
| 61 | + * (possibly scaled) length in pixels. |
| 62 | + */ |
34 | 63 | anchor?: "start" | "middle" | "end";
|
| 64 | + |
35 | 65 | /**
|
36 |
| - * The frame anchor, for vectors without a position, or positioned on a single |
37 |
| - * (*x* or *y*) dimension in a two-dimensional frame. |
| 66 | + * The vector’s frame anchor, to default **x** and **y** relative to the |
| 67 | + * frame; a constant representing one of the frame corners (*top-left*, |
| 68 | + * *top-right*, *bottom-right*, *bottom-left*), sides (*top*, *right*, |
| 69 | + * *bottom*, *left*), or *middle* (default). Has no effect if both **x** and |
| 70 | + * **y** are specified. |
38 | 71 | */
|
39 | 72 | frameAnchor?: FrameAnchor;
|
40 | 73 | }
|
41 | 74 |
|
42 | 75 | /**
|
43 |
| - * A vector of the given **length**, drawn in the **shape** of an arrow from its |
44 |
| - * **anchor** at the given [**x**, **y**] position, and an orientation indicated |
45 |
| - * by **rotate**. To create a vector field representing a wind map: |
| 76 | + * Returns a new vector mark for the given *data* and *options*. For example, to |
| 77 | + * create a vector field from spatial samples of wind observations: |
46 | 78 | *
|
47 | 79 | * ```js
|
48 | 80 | * Plot.vector(wind, {x: "longitude", y: "latitude", length: "speed", rotate: "direction"})
|
49 | 81 | * ```
|
50 | 82 | *
|
51 |
| - * By default, accepts an array of coordinates pairs (tuples) as *data*. |
| 83 | + * If none of **frameAnchor**, **x**, and **y** are specified, then **x** and |
| 84 | + * **y** default to accessors assuming that *data* contains tuples [[*x₀*, |
| 85 | + * *y₀*], [*x₁*, *y₁*], [*x₂*, *y₂*], …] |
52 | 86 | */
|
53 | 87 | export function vector(data?: Data, options?: VectorOptions): Vector;
|
54 | 88 |
|
55 | 89 | /**
|
56 |
| - * The vector mark, but the **x** channel defaults to identity, positioning one |
57 |
| - * arrow for each horizontal position passed as *data*. |
| 90 | + * Like vector, but **x** instead defaults to the identity function and **y** |
| 91 | + * defaults to null, assuming that *data* is an array of numbers [*x₀*, *x₁*, |
| 92 | + * *x₂*, …]. |
58 | 93 | */
|
59 | 94 | export function vectorX(data?: Data, options?: VectorOptions): Vector;
|
60 | 95 |
|
61 | 96 | /**
|
62 |
| - * The vector mark, but the **y** channel defaults to identity, positioning one |
63 |
| - * arrow for each vertical position passed as *data*. |
| 97 | + * Like vector, but **y** instead defaults to the identity function and **x** |
| 98 | + * defaults to null, assuming that *data* is an array of numbers [*y₀*, *y₁*, |
| 99 | + * *y₂*, …]. |
64 | 100 | */
|
65 | 101 | export function vectorY(data?: Data, options?: VectorOptions): Vector;
|
66 | 102 |
|
67 | 103 | /**
|
68 |
| - * A variant of the vector mark where the **shape** defaults to *spike*, the |
69 |
| - * **stroke** defaults to *currentColor*, the **strokeWidth** defaults to 1, the |
70 |
| - * **fill** defaults to **stroke**, the **fillOpacity** defaults to 0.3, and the |
71 |
| - * **anchor** defaults to *start*. Typically used to create a spike map: |
| 104 | + * Like vector, but with default *options* suitable for drawing a spike map. For |
| 105 | + * example, to show city populations: |
72 | 106 | *
|
73 | 107 | * ```js
|
74 |
| - * Plot.spike(counties, {stroke: "red", length: "population"}) |
| 108 | + * Plot.spike(cities, {x: "longitude", y: "latitude", stroke: "red", length: "population"}) |
75 | 109 | * ```
|
76 | 110 | */
|
77 | 111 | export function spike(data?: Data, options?: VectorOptions): Vector;
|
78 | 112 |
|
79 |
| -/** The vector mark */ |
| 113 | +/** The vector mark. */ |
80 | 114 | export class Vector extends RenderableMark {}
|
0 commit comments