Skip to content

Commit 9081842

Browse files
committed
legends docs
1 parent aa329a0 commit 9081842

File tree

9 files changed

+362
-15
lines changed

9 files changed

+362
-15
lines changed

docs/api-reference/color/legend.md

Lines changed: 203 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
---
22
sidebar_position: 3
3-
sidebar_label: Legend
3+
sidebar_label: Legends
44
slug: legend
55
---
66

77
import Legend from '@site/src/components/Legend';
8+
import ColorSwatches from '@site/src/components/ColorSwatches';
9+
import IconsSwatches from '@site/src/components/IconsSwatches';
810

9-
# Legend
11+
# Legends
1012

11-
## React component
13+
## Numerical scale legends
14+
15+
### React component
1216

1317
To render the legend of a numerical color scale, you can use the `<Legend>` component as follow:
1418

@@ -49,7 +53,7 @@ interface LegendProps {
4953

5054
Note that the Legend will fill the width of its container. [Get in touch](mailto:[email protected]) if you have ideas on improvements or pragmatic options we may have missed.
5155

52-
## Vanilla javascript
56+
### Vanilla javascript
5357

5458
For non-react codebases, we provide a vanilla Javascript function that wraps this component and renders it into a container.
5559

@@ -76,9 +80,9 @@ smplr.Color.drawLegend({
7680
```
7781

7882
- `containerId` is the "id" of the html container where smplr.js should render the legend, something like "smplr-legend" that can be found in your html. Only ids are supported, not classes.
79-
- `...legendProps` represents all other options as per the [react component section](#react-component).
83+
- `...legendProps` represents all other options as per the previous section.
8084

81-
## Example legend
85+
### Example
8286

8387
<Legend />
8488

@@ -100,3 +104,196 @@ smplr.Color.drawLegend({
100104
},
101105
});
102106
```
107+
108+
## Categorical scale legends
109+
110+
### React component
111+
112+
To render the legend of a numerical color scale, you can use the `<ColorSwatches>` component as follow:
113+
114+
```tsx
115+
interface ColorSwatchesProps {
116+
swatches: {
117+
color: string
118+
label: string
119+
group?: string
120+
}[]
121+
size?: number
122+
correctColor?: boolean
123+
noLabels?: boolean
124+
}
125+
<ColorSwatches {...props: ColorSwatchesProps}>
126+
127+
// example usage with Color from smplr
128+
<smplr.Color.ColorSwatches
129+
swatches={[
130+
{
131+
color: 'red',
132+
label: 'Alert',
133+
},
134+
{
135+
color: 'orange',
136+
label: 'Warning',
137+
},
138+
{
139+
color: 'green',
140+
label: 'All ok',
141+
},
142+
]}
143+
/>
144+
```
145+
146+
- `swatches` defines the colors and labels used for the swatches. Note that an optional group can be provided to create multiple lines with a label ahead of each group. You can refer to the [`ragSwatches`](/api-reference/color/scales#ragscale) and [`categorySwatches`](/api-reference/color/scales#categoryscale) helpers to generate swatches for categorical scales generated with our functions.
147+
- `size` - _optional_ - is the size in pixels of each swatch. _Default value: 10._
148+
- `correctColor` - _optional_ - lets you choose if the colors of the legend should be corrected to match the ones from the viewer as per the explanation in the [color mapping section](./overview#color-mapping). We correct them by default. _Default value: true._
149+
- `noLabels` - _optional_ - set this to true to hide labels. _Default value: false._
150+
151+
152+
### Vanilla javascript
153+
154+
For non-react codebases, we provide a vanilla Javascript function that wraps this component and renders it into a container.
155+
156+
```ts
157+
smplr.Color.drawColorSwatches({
158+
containerId: string
159+
...swatchesProps: ColorSwatchesProps // see above
160+
})
161+
162+
// example usage
163+
smplr.Color.drawColorSwatches({
164+
containerId: 'smplr-legend',
165+
swatches: [
166+
{
167+
color: 'red',
168+
label: 'Alert',
169+
},
170+
{
171+
color: 'orange',
172+
label: 'Warning',
173+
},
174+
{
175+
color: 'green',
176+
label: 'All ok',
177+
},
178+
]
179+
})
180+
```
181+
182+
- `containerId` is the "id" of the html container where smplr.js should render the legend, something like "smplr-legend" that can be found in your html. Only ids are supported, not classes.
183+
- `...swatchesProps` represents all other options as per the previous section.
184+
185+
### Example
186+
187+
<ColorSwatches />
188+
189+
was rendered with the code below:
190+
191+
```ts
192+
smplr.Color.drawColorSwatches({
193+
containerId: 'smplr-legend',
194+
swatches: [
195+
{
196+
color: 'red',
197+
label: 'Alert',
198+
},
199+
{
200+
color: 'orange',
201+
label: 'Warning',
202+
},
203+
{
204+
color: 'green',
205+
label: 'All ok',
206+
},
207+
]
208+
});
209+
```
210+
211+
## Icons legends
212+
213+
### React component
214+
215+
To render the legend of a numerical color scale, you can use the `<IconsSwatches>` component as follow:
216+
217+
```tsx
218+
interface IconsSwatchesProps {
219+
icons: {
220+
url: string
221+
label: string
222+
group?: string
223+
}[]
224+
height?: number
225+
noLabels?: boolean
226+
}
227+
<IconsSwatches {...props: IconsSwatchesProps}>
228+
229+
// example usage with Color from smplr
230+
<smplr.Color.IconsSwatches
231+
icons={[
232+
{
233+
url: 'https://retail.smplrspace.io/img/electric.png',
234+
label: 'EV charging',
235+
},
236+
{
237+
url: 'https://retail.smplrspace.io/img/wheelchair.png',
238+
label: 'Reduced mobility',
239+
}
240+
]}
241+
/>
242+
```
243+
244+
- `icons` defines the icons and labels used for the swatches. Note that an optional group can be provided to create multiple lines with a label ahead of each group.
245+
- `height` - _optional_ - is the height in pixels of each swatch. _Default value: 16._
246+
- `noLabels` - _optional_ - set this to true to hide labels. _Default value: false._
247+
248+
249+
### Vanilla javascript
250+
251+
For non-react codebases, we provide a vanilla Javascript function that wraps this component and renders it into a container.
252+
253+
```ts
254+
smplr.Color.drawIconsSwatches({
255+
containerId: string
256+
...swatchesProps: IconsSwatchesProps // see above
257+
})
258+
259+
// example usage
260+
smplr.Color.drawIconsSwatches({
261+
containerId: 'smplr-legend',
262+
icons: [
263+
{
264+
url: 'https://retail.smplrspace.io/img/electric.png',
265+
label: 'EV charging',
266+
},
267+
{
268+
url: 'https://retail.smplrspace.io/img/wheelchair.png',
269+
label: 'Reduced mobility',
270+
}
271+
]
272+
})
273+
```
274+
275+
- `containerId` is the "id" of the html container where smplr.js should render the legend, something like "smplr-legend" that can be found in your html. Only ids are supported, not classes.
276+
- `...swatchesProps` represents all other options as per the previous section.
277+
278+
### Example
279+
280+
<IconsSwatches />
281+
282+
was rendered with the code below:
283+
284+
```ts
285+
smplr.Color.drawIconsSwatches({
286+
containerId: 'smplr-legend',
287+
icons: [
288+
{
289+
url: 'https://retail.smplrspace.io/img/electric.png',
290+
label: 'EV charging',
291+
},
292+
{
293+
url: 'https://retail.smplrspace.io/img/wheelchair.png',
294+
label: 'Reduced mobility',
295+
}
296+
],
297+
height: 20
298+
});
299+
```

docs/api-reference/color/scales.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Configure a numerical scale using our interactive [colors playground](https://co
6666
This function lets you quickly map a discrete number of named categories to colors. It comes with fallback color built-in, as well as type safe categories.
6767

6868
```ts
69-
smplr.Color.numericScale<C extends string>({
69+
smplr.Color.categoryScale<C extends string>({
7070
categories: Record<C, string>
7171
nodata?: string
7272
}): ((category: C) => string)
@@ -86,6 +86,28 @@ smplr.Color.categoryScale({
8686

8787
Typescript tip: You may disable category typesafety by passing a "loose" type hint to the function: `categoryScale<string>({...})`.
8888

89+
#### Easy swatches for your legend
90+
91+
When using a category scale, you can use the same arguments provided to create the scale to generate the corresponding swatches. This makes it easy to use a [legend for the scale](/api-reference/color/legend#categorical-scale-legends).
92+
93+
```ts
94+
smplr.Color.categorySwatches<C extends string>({
95+
// same as categoryScale
96+
categories: Record<C, string>
97+
nodata?: string
98+
// additional swatches config
99+
excludeNoData?: boolean
100+
noDataLabel?: string
101+
}): {
102+
color: string
103+
label: string
104+
}[]
105+
```
106+
107+
- `categories` and `nodata` - refer to `categoryScale` above.
108+
- `excludeNoData` - _optional_ - lets you opt out of having a swatch for the `nodata` color. _Default value: false._
109+
- `noDataLabel` - _optional_ - lets you customize the label used for the `nodata` color. _Default value: 'No data'._
110+
89111
### ragScale
90112

91113
This function is built on top of `categoryScale` and provide a shortcut for red/amber/green scenarios. Here, the scale comes with pre-defined and optimized RAG colors, that can be customized. And the values "red", "amber", and "green" are always valid for quick testing.
@@ -116,3 +138,30 @@ smplr.Color.ragScale({
116138
- `nodata` - _optional_ - is the color used when the value passed to the scale is not a known category. _Default value: #6a6c6c._
117139

118140
Typescript tip: You may disable category typesafety by passing a "loose" type hint to the function: `ragScale<string>({...})`.
141+
142+
#### Easy swatches for your legend
143+
144+
When using a RAG scale, you can use the same arguments provided to create the scale to generate the corresponding swatches. This makes it easy to use a [legend for the scale](/api-reference/color/legend#categorical-scale-legends).
145+
146+
```ts
147+
smplr.Color.ragSwatches<C extends string | 'red' | 'amber' | 'green'>({
148+
// same as ragScale
149+
categories?: Partial<Record<'red' | 'amber' | 'green', C>>
150+
colors?: {
151+
red?: string
152+
amber?: string
153+
green?: string
154+
}
155+
nodata?: string
156+
// additional swatches config
157+
excludeNoData?: boolean
158+
noDataLabel?: string
159+
}): {
160+
color: string
161+
label: string
162+
}[]
163+
```
164+
165+
- `categories`, `colors` and `nodata` - refer to `ragScale` above.
166+
- `excludeNoData` - _optional_ - lets you opt out of having a swatch for the `nodata` color. _Default value: false._
167+
- `noDataLabel` - _optional_ - lets you customize the label used for the `nodata` color. _Default value: 'No data'._

docs/api-reference/space/custom-ux.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ space.startViewer({
8181
autoRotate?: boolean,
8282
hideNavigationButtons?: boolean
8383
hideLevelPicker?: boolean
84+
legendPosition?: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
8485
}) => void
8586
```
8687

@@ -94,6 +95,7 @@ space.startViewer({
9495
- `autoRotate` - _optional_ - set this to true to have the viewer spin around the space automatically. You can also start, set the rotation speed, and stop the rotation as described [below](#auto-rotate-the-viewer). _Default value: false_
9596
- `hideNavigationButtons` - _optional_ - set this to true if you want the user to control the camera but want to remove the navigation buttons. Mouse, touch and keyboard inputs will work while the buttons are hidden. _Default value: false_
9697
- `hideLevelPicker` - _optional_ - set this to true if you want to remove the level picker from the viewer. Levels can still be controlled programmatically, so you could use your own buttons or logic. _Default value: false_
98+
- `legendPosition` - _optional_ - lets you choose where the legend (if any is configured in the data layers) would be rendered. _Default value: 'top-left'_
9799

98100
## Viewer controls
99101

docs/api-reference/space/data-layers.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,35 @@ space.addDataLayer({
1717
tooltipTemplate?: string,
1818
tooltipContainerStyle?: string,
1919
persistentTooltip?: boolean,
20+
legend?: LegendConfig, // see below
2021
onClick?: (dataElement: object, event: PointerEvent) => void,
2122
onHover?: (dataElement: object, event: LimitedPointerEvent) => void,
2223
onHoverOut?: (dataElement: object, event: LimitedPointerEvent) => void
2324
}) => DataLayerController
25+
26+
type LegendConfig =
27+
| {
28+
type: 'numeric'
29+
colorScale: (n: number | null | undefined) => string
30+
domain?: [number, number]
31+
ticks?: Record<number, number | string>
32+
}
33+
| {
34+
type: 'swatches'
35+
swatches: {
36+
color: string
37+
label: string
38+
group?: string
39+
}[]
40+
}
41+
| {
42+
type: 'icons'
43+
icons: {
44+
url: string
45+
label: string
46+
group?: string
47+
}[]
48+
}
2449
```
2550
2651
- `...layerDefinition` - refer to the [overview](./overview#data-layers) page.
@@ -35,6 +60,10 @@ space.addDataLayer({
3560
- Without this helper, we use `'-'` as a default value for all fields.
3661
- `tooltipContainerStyle` - _optional_ - lets you override the style of the tooltip container with inline CSS.
3762
- `persistentTooltip` - _optional_ - set this to `true` to turn tooltips into small cards that are all visible at once instead of on hover. Persistent tooltips are automatically positioned on the center of the data element they're attached to. They disappear when the camera is moving and reappear when it stops. They are only displayed for the top visible level. They only work for data elements with a non null or undefined `id`. _Default value: false_
63+
- `legend` - _optional_ - lets you configure a legend to be rendered automatically in a collapsible overlay on the viewer. The legend can be positioned using `legendPosition` in [viewer options](/api-reference/space/custom-ux#viewer-options).
64+
- For `numeric` legends, refer to options in [the legend section](/api-reference/color/legend#numerical-scale-legends).
65+
- For `swatches` legends, refer to options in [the legend section](/api-reference/color/legend#categorical-scale-legends).
66+
- For `icons` legends, refer to options in [the legend section](/api-reference/color/legend#icons-legends).
3867
- `onClick` - _optional_ - is taking the data element that was clicked as argument, as well as the Javascript [pointer event](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent) that triggered the click. It is called each time a click or tap event happens.
3968
- `onHover` - _optional_ - is taking the newly hovered data element as argument, as well as a limited (due to the rendering engine's internals) "pointer event" that triggered the handler. The limited event only includes the coordinates within the viewer of the pointer at the time when the event was triggered. The handler is called once when the pointer starts to hover a data element.
4069
- `onHoverOut` - _optional_ - is taking the previously hovered data element as argument, as well as the same limited "pointer event" as for `onHover`. The handler is called once when the pointer stops hovering a data element.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"@mantine/core": "^3.0.5",
3030
"@mantine/hooks": "^3.0.5",
3131
"@mdx-js/react": "^1.6.21",
32-
"@smplrspace/smplr-loader": "^2.14.2-beta.4",
32+
"@smplrspace/smplr-loader": "^2.25.2-beta.0",
3333
"@stackblitz/sdk": "^1.8.2",
3434
"@svgr/webpack": "^5.5.0",
3535
"chance": "^1.1.8",

0 commit comments

Comments
 (0)