Skip to content

Commit e2f7e8e

Browse files
committed
legends docs
1 parent 675fc6b commit e2f7e8e

File tree

2 files changed

+69
-25
lines changed

2 files changed

+69
-25
lines changed

src/legends.d.ts

Lines changed: 67 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
11
import type {ScaleName, ScaleOptions} from "./scales.js";
22

3-
/** The supported legend types. */
4-
export type LegendType = "ramp" | "swatches";
5-
63
/** Options for generating a scale legend. */
74
export interface LegendOptions {
85
/**
9-
* The desired legend type; currently supported only for a discrete *color*
10-
* scale of type *ordinal*, *quantile*, *quantize*, or *threshold*. One of:
6+
* The desired legend type; one of:
7+
*
8+
* - *ramp* - place labels underneath with a connecting line, and no wrapping
9+
* - *swatches* - place labels to the right, and allow wrapping
1110
*
12-
* * *ramp* - place labels underneath with a connecting line, and no wrapping
13-
* * *swatches* - place labels to the right, and allow wrapping
11+
* The legend type can currently only be configured for a discrete *color*
12+
* scale of type *ordinal*, *quantile*, *quantize*, or *threshold*; for other
13+
* *color* scale types, or for *opacity* or *symbol* scales, the legend type
14+
* cannot be changed.
1415
*/
15-
legend?: LegendType;
16+
legend?: "ramp" | "swatches";
1617

1718
/**
1819
* How to format tick values sampled from the scale’s domain. This may be a
1920
* function, which will be passed the tick value *t* and zero-based index *i*
2021
* and must return the corresponding string. If the domain is numbers, the
21-
* tick format may also be expressed as a [d3-format
22-
* string](https://github.com/d3/d3-format/blob/main/README.md#locale_format);
23-
* or if the domain is dates, the tick format may also be expressed as a
24-
* [d3-time-format
25-
* string](https://github.com/d3/d3-time-format/blob/main/README.md#locale_format).
22+
* tick format may also be expressed as a [d3-format string][1]; or if the
23+
* domain is dates, the tick format may also be expressed as a [d3-time-format
24+
* string][2].
25+
*
26+
* [1]: https://github.com/d3/d3-format/blob/main/README.md#locale_format
27+
* [2]: https://github.com/d3/d3-time-format/blob/main/README.md#locale_format
2628
*/
2729
tickFormat?: ScaleOptions["tickFormat"];
2830

2931
/**
30-
* The [CSS
31-
* font-variant](https://developer.mozilla.org/en-US/docs/Web/CSS/font-variant)
32-
* for tick labels. For non-ordinal scales, or ordinal scales without an
33-
* interval, this defaults to *tabular-nums*.
32+
* The [CSS font-variant][1] for tick labels. For non-ordinal scales, or
33+
* ordinal scales without an interval, this defaults to *tabular-nums*.
34+
*
35+
* [1]: https://developer.mozilla.org/en-US/docs/Web/CSS/font-variant
3436
*/
3537
fontVariant?: ScaleOptions["fontVariant"];
3638

@@ -43,35 +45,77 @@ export interface LegendOptions {
4345
*/
4446
className?: string | null;
4547

46-
/** The constant color for *opacity* scales. Defaults to black. */
48+
/** The constant color the ramp; defaults to black. For *ramp* *opacity* legends only. */
4749
color?: string;
48-
49-
// symbol options
50+
/** The desired fill color of symbols; use *color* for a redundant encoding. For *symbol* legends only. */
5051
fill?: string;
52+
/** The desired fill opacity of symbols. For *symbol* legends only. */
5153
fillOpacity?: number;
54+
/** The desired stroke color of symbols; use *color* for a redundant encoding. For *symbol* legends only. */
5255
stroke?: string;
56+
/** The desired stroke opacity of symbols. For *symbol* legends only. */
5357
strokeOpacity?: number;
58+
/** The desired stroke width of symbols. For *symbol* legends only. */
5459
strokeWidth?: number;
60+
/** The desired radius of symbols in pixels. For *symbol* legends only. */
5561
r?: number;
5662

57-
// dimensions
63+
/**
64+
* The width of the legend in pixels. For *ramp* legends, defaults to 240; for
65+
* *swatch* legends, defaults to undefined, allowing the swatches to wrap
66+
* based on content flow.
67+
*/
5868
width?: number;
69+
70+
/**
71+
* The height of the legend in pixels; defaults to 44 plus **tickSize**. For
72+
* *ramp* legends only.
73+
*/
5974
height?: number;
75+
76+
/** The top margin in pixels; defaults to 18. For *ramp* legends only. */
6077
marginTop?: number;
78+
/** The right margin in pixels; defaults to 0. For *ramp* legends only. */
6179
marginRight?: number;
80+
/** The bottom margin in pixels; defaults to 16 plus **tickSize**. For *ramp* legends only. */
6281
marginBottom?: number;
82+
/** The left margin in pixels; defaults to 0. For *ramp* legends only. */
6383
marginLeft?: number;
6484

65-
// ramp options
85+
/** A textual label to place above the legend. For *ramp* legends only. */
6686
label?: string | null;
87+
88+
/**
89+
* The desired approximate number of axis ticks, or an explicit array of tick
90+
* values, or an interval such as *day* or *month*. For *ramp* legends only.
91+
*/
6792
ticks?: ScaleOptions["ticks"];
93+
94+
/**
95+
* The length of axis tick marks in pixels; negative values extend in the
96+
* opposite direction. For *ramp* legends only.
97+
*/
6898
tickSize?: ScaleOptions["tickSize"];
99+
100+
/**
101+
* If true, round the output value to the nearest integer (pixel); useful for
102+
* crisp edges when rendering. For *ramp* legends only.
103+
*/
69104
round?: ScaleOptions["round"];
70105

71-
// swatches options
106+
/**
107+
* The [CSS columns property][1], for a multi-column layout. For *swatches*
108+
* legends only.
109+
*
110+
* [1]: https://developer.mozilla.org/en-US/docs/Web/CSS/columns
111+
*/
72112
columns?: string;
113+
114+
/** The swatch width and height in pixels; defaults to 15; For *swatches* legends only. */
73115
swatchSize?: number;
116+
/** The swatch width in pixels; defaults to **swatchSize**; For *swatches* legends only. */
74117
swatchWidth?: number;
118+
/** The swatch height in pixels; defaults to **swatchSize**; For *swatches* legends only. */
75119
swatchHeight?: number;
76120
}
77121

src/scales.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {InsetOptions} from "./inset.js";
22
import type {NiceInterval, RangeInterval} from "./interval.js";
3-
import type {LegendType} from "./legends.js";
3+
import type {LegendOptions} from "./legends.js";
44
import type {AxisAnchor} from "./marks/axis.js";
55

66
/**
@@ -521,7 +521,7 @@ export interface ScaleOptions extends ScaleDefaults {
521521
*
522522
* For *color*, *opacity*, and *symbol* scales only. See also *plot*.legend.
523523
*/
524-
legend?: LegendType | boolean | null;
524+
legend?: LegendOptions["legend"] | boolean | null;
525525

526526
/**
527527
* The desired approximate number of axis ticks, or an explicit array of tick

0 commit comments

Comments
 (0)