Legend
Legend is a utility Plugin that works with all charts. It automatically reads the series of the current data and generates the corresponding legend. Legend items are clickable to toggle each series' visibility, notifying other Plugins to update accordingly through events.
Legend has a single Layer (also named Legend) that is shown by default, so new Legend() is usually all you need.
Usage
Add Legend to the plugins array alongside your chart Plugin. Here is a complete example of GridPlot with Legend:
import { OrbCharts } from '@orbcharts/core'
import type { RawData } from '@orbcharts/core'
import { GridPlot, Legend } from '@orbcharts/plugin-basic'
const data: RawData = [
{ series: 'A', category: 'category1', value: 30 },
{ series: 'A', category: 'category2', value: 20 },
{ series: 'B', category: 'category1', value: 70 },
{ series: 'B', category: 'category2', value: 80 },
]
const chart = new OrbCharts(document.querySelector('#chart')!, {
data,
plugins: [
new GridPlot({ Bar: {}, CategoryAxis: {}, ValueAxis: {} }),
new Legend({ Legend: { placement: 'top' } }),
],
})
// chart.destroy()Tip: The chart fills its container by default, so set a width and height on the
#chartcontainer. See Core Concepts.
A bare new Legend() shows the legend with its defaults (placement: 'bottom').
new Legend() // placed at the bottom by default
new Legend({ Legend: { placement: 'top' } }) // placed at the topTip: The legend generates items automatically from the
seriesof the current data; clicking an item toggles that series' visibility and notifies the other Plugins in the same chart.
Tip: Layer visibility and the
show/hide/showOnly/togglemethods are common to all Plugins. See Plugin API for the full explanation.
Plugin parameters
These parameters affect the whole Legend's behavior, listed alongside Layer names in the same params object:
| Parameter | Type | Default | Description |
|---|---|---|---|
sort | ((a, b) => number) | null | null | Sort function for legend items; null means no sorting |
datasetIndex | number | 0 | Which dataset to use when data is multi-dataset |
styles | GraphicStyles | padding is { 60, 60, 60, 60 } | Shared visual styles (see Plugin API) |
visibleFilter | (datum: ModelDatumSeries) => boolean | null | () => true | Filter which series to show (see Plugin API) |
Note:
stylesandvisibleFilterare shared by all Plugins; their structure and usage are documented centrally in Plugin API. Only Legend's default difference is noted here (styles.paddingdefaults to{ top: 60, right: 60, bottom: 60, left: 60 }).
Layer parameters
Legend has a single Layer, also named Legend:
About
ColorType: color-related parameters useColorType('data','primary', etc.), mapping to the colors defined in the Theme. See Theme for the full explanation.
Legend
| Parameter | Type | Default | Description |
|---|---|---|---|
placement | see below | 'bottom' | Legend position |
padding | number | 5 | Legend container padding |
backgroundColorType | ColorType | 'none' | Background fill |
strokeColorType | ColorType | 'none' | Background stroke color |
gap | number | 10 | Spacing between items |
listRectWidth | number | 14 | Color swatch width |
listRectHeight | number | 14 | Color swatch height |
listRectRadius | number | 0 | Color swatch corner radius |
textColorType | ColorType | 'primary' | Text color |
placement options: 'top', 'top-start', 'top-end', 'bottom', 'bottom-start', 'bottom-end', 'left', 'left-start', 'left-end', 'right', 'right-start', 'right-end'.
Full TypeScript interface
interface LegendPluginParams {
styles: GraphicStyles
visibleFilter: (datum: ModelDatumSeries) => boolean | null
sort: ((a, b) => number) | null
datasetIndex: number
}
interface LegendAllLayerParams {
Legend: LegendLayerParams
}
// The constructor accepts a DeepPartial of both (Layer names alongside Plugin params)
new Legend(params?: DeepPartial<LegendPluginParams & LegendAllLayerParams>)Related
- GridPlot — a chart Plugin to pair with Legend
- Tooltip — another utility Plugin
- Plugin API — shared parameters and operations