Skip to main content
Version: 1.2.0

Legends

Legends are representations of the data entries in a dataset, legends are mostly used to represent data keys of a dataset plotted in a chart.

Live Editor

Usage

1 . Import Legends component after installation.

import { Legends } from "@k2/ui";

2 . Add data to Legends component.

<Legends data={LegendData} />

Examples

Basic Legends: Demo

<Legends
data={[
{
name: "legend 1",
color: "red",
enabled: true,
},
{
name: "legend 2",
color: "green",
enabled: false,
},
{
name: "legend 3",
color: "blue",
enabled: true,
},
{
name: "legend 4",
color: "pink",
enabled: true,
},
{
name: "legend 5",
color: "orange",
enabled: true,
},
{
name: "legend 6",
color: "black",
enabled: true,
},
]}
/>

Legends without symbols: Demo

<Legends
data={[
{
name: "legend 1",
color: "red",
enabled: true,
},
{
name: "legend 2",
color: "green",
enabled: false,
},
{
name: "legend 3",
color: "blue",
enabled: true,
},
{
name: "legend 4",
color: "pink",
enabled: true,
},
{
name: "legend 5",
color: "orange",
enabled: true,
},
{
name: "legend 6",
color: "black",
enabled: true,
},
]}
symbol={false}
/>

Legends with square symbol: Demo

<Legends
data={[
{
name: "legend 1",
color: "red",
enabled: true,
},
{
name: "legend 2",
color: "green",
enabled: false,
},
{
name: "legend 3",
color: "blue",
enabled: true,
},
{
name: "legend 4",
color: "pink",
enabled: true,
},
{
name: "legend 5",
color: "orange",
enabled: true,
},
{
name: "legend 6",
color: "black",
enabled: true,
},
]}
symbolShape="square"
/>

Click here for more variations of legends.

Props

PropertyTypeRequiredDefault valueDescription
dataArray<LegendData>yesNoneData for Legends
onClick(legend: LegendData) => void;noNoneClick handler of Legends
symbolboolean | JSX.Element | HTMLElementnotrueSymbol for Legends
overflow"auto" | "scroll" | "hidden" | "none"no"auto"To handle overflow of legends
direction"row" | "column"no"row"Direction of legends
symbolShape"circle" | "square"no"circle"Shape of Symbol
colorstringno"#ccc"Color of Symbol
classNamestringnoNoneClass applied to the root element of Legends
classesClassesnoNoneClass applied to the root element of Legends
styleCSSPropertiesnoNoneStyles applied to the root element of Legends

Note: K2 Components that are using Legends will handle data and onClick event internally. data and onClick are required only when using Legends independently or with some component out of k2.

Custom Types

LegendData

Here is the format of the data accepted by a Legends component.

type Legend = {
name: string;
color?: string;
symbol?: boolean | JSX.Element | HTMLElement;
enabled: boolean;
}

Classes

Classes enable customization and styling of Legends elements throught their classes.

ClassNameTypeDefault valueDescription
rootstringNoneApplied to root element of Legends.
itemstringNoneApplied to item of Legends.
symbolstringNoneApplied to symbol of Legends.
valuestringNoneApplied to value of Legends.

Please see Class Specificity for correctly defining it.