Bullet Legend
Overview
BulletLegend is a stand-alone component that can be used alongside your visualization to label colored data.
Legend Items
BulletLegend requires the items property, an array of items that will be displayed in the legend.
Each item has type BulletLegendItemInterface:
interface BulletLegendItemInterface {
name: string | number;
color?: string;
shape?: BulletShape;
inactive?: boolean;
hidden?: boolean;
pointer?: boolean;
}
Note that the only required property, name corresponds to the legend items' label.
Here is an example of a basic configuration, where labels is an array of strings:
- React
- Angular
- Svelte
- Vue
- Solid
- TypeScript
import { VisBulletLegend } from '@unovis/react'
import { labels } from './data'
function Component(props) {
const items = labels.map(label => ({ name: label }))
return (
<VisBulletLegend items={items}/>
)
}
import { labels } from './data'
@Component({
template: '<vis-bullet-legend [items]="items"></vis-bullet-legend>'
})
export class Component {
items = labels.map(label => ({ name: label }))
}
<script lang='ts'>
import { VisBulletLegend } from '@unovis/svelte'
import { labels } from './data'
const items = labels.map(label => ({ name: label }))
</script>
<VisBulletLegend {items}/>
<script setup lang="ts">
import { VisBulletLegend } from '@unovis/vue'
import { labels } from './data'
const items = labels.map(label => ({ name: label }))
</script>
<template>
<VisBulletLegend :items="items" />
</template>
import { VisBulletLegend } from '@unovis/solid'
import { labels } from './data'
function Component(props) {
const items = labels.map(label => ({ name: label }))
return (
<VisBulletLegend items={items}/>
)
}
import { BulletLegend } from '@unovis/ts'
import { labels } from './data'
const bulletLegend = new BulletLegend(node, {
items: labels.map(label => ({ name: label }))
})
Color
By default, our color palette will be used to color each legend item, but you can provide your own colors in the legend item array.
const colors = ['red', 'blue', 'green']
const items = labels.map((label, i) => ({ name: label, color: colors[i] }))
or manually:
const items = [
{ name: 'A', color: 'red' },
{ name: 'B', color: 'blue' },
{ name: 'C', color: 'green' }
]
Either will produce the following result:
Multiple Colors Per Item 1.6
In case you want to have multiple colors in your legend, you can simply pass in an arry into color
const items = [
{
name: 'item 1',
color: ['#4cc9f0', '#4361ee', '#f72585', '#7209b7']
},
{
name: 'item 2',
color: ['#3a0ca3', '#f9c74f', '#f94144']
}
]
Shape
You can specify the shape of individual bullets with the shape property or with the bulletShape
component config property. This is useful when you want to have the legend with a line chart or shaped scatter plot.
The supported shapes are apart of the BulletShape enum.
import { BulletShape } from '@unovis/ts'
const items = [
{ name: 'Circle', shape: BulletShape.Circle },
{ name: 'Square', shape: BulletShape.Square },
{ name: 'Triangle', shape: BulletShape.Triangle }
{ name: 'Star', shape: BulletShape.Star }
]
All supported shapes:
BulletShape.Circleor "circle"BulletShape.Crossor "cross"BulletShape.Diamondor "diamond"BulletShape.Lineor "line"BulletShape.Squareor "square"BulletShape.Staror "star"BulletShape.Triangleor "triangle"BulletShape.Wyeor "wye"
Inactive Items
In some cases you may want to have some legend items look inactive, which reduces the opacity of the bullet. See how the initial legend looks when all of the items are inactive:
const items = labels.map(label => ({ name: label, inactive: true }))
Pointer
The pointer property in the BulletLegendItemInterface refers to the cursor CSS property.
Note that there is no specified default value unless onLegendItemCilck property is provided, in which case the
cursor will be pointer.
Orientation
The orientation property can be set to BulletLegendOrientation.Horizontal ('horizontal') or
BulletLegendOrientation.Horizontal ('vertical') to change the layout of the legend items.
Having a vertical legend is useful when you have a large number of items and the legend
container is scrollable.