Skip to main content

Grouped Bar

Basic configuration

The Grouped Bar component has been designed to work together with XY Container. The minimal Grouped Bar configuration looks like:

component.tsx
import { VisXYContainer, VisGroupedBar } from '@unovis/react'

function Component(props) {
const data: DataRecord[] = props.data
const x = (d: DataRecord) => d.x
const y = (d: DataRecord) => d.y

return (
<VisXYContainer data={data}>
<VisGroupedBar x={x} y={y}/>
</VisXYContainer>
)
}
Loading...

Multiple Bars

Grouped Bar can accept an array of y accessors to display values as a group of bars:

component.tsx
import { VisXYContainer, VisGroupedBar } from '@unovis/react'

function Component(props) {
const data: DataRecord[] = props.data
const x = (d: DataRecord) => d.x
const y = [
(d: DataRecord) => d.y,
(d: DataRecord) => d.y1,
(d: DataRecord) => d.y2
]

return (
<VisXYContainer data={data}>
<VisGroupedBar x={x} y={y}/>
</VisXYContainer>
)
}
Loading...

Group Width

By default, the width of the bars is calculated automatically based on their count. But you can also strictly set the bar's width in pixels using the barWidth property:

<VisGroupedBar groupWidth={50} x={x} y={y}/>
Loading...

Limiting Dynamic Group Bar Width

When you don't know the number of bars in advance, and you're relying on automatic bar width calculation, you might want to limit the maximum bar width to prevent the bars from being too wide when there are just a few of them. That can be achieved by setting the groupMaxWidth property.

<VisGroupedBar x={x} y={y}/>
Loading...
<VisGroupedBar x={x} y={y} groupMaxWidth={25}/>
Loading...

Group Padding

Another way to control width in a BarGroup component is by changing the groupPadding argument, which specifies how much of the available sector should be empty, in the range of [0,1).

<VisGroupedBar groupPadding={0.5} x={x} y={y}/>
Loading...

Bar Padding

Or for individual bars within each bar group, the barPadding attribute is available:

<VisGroupedBar barPadding={0.5} x={x} y={y}/>
Loading...

Bar Height Limit

When you have highly scattered data with very low and high values, the bars corresponding to the lower values can be so small, so they become invisible. If you want to prevent that you can set the minimum bar height to 1 pixel using the barMinHeight boolean property.

<VisGroupedBar barMinHeight={1} x={x} y={y}/>
Loading...

Dealing With Inconsistent data

When your data has gaps, it's impossible to do calculate of the bar width automatically. The visualization will still try to do that, but most likely the result will be wrong, and you'll see wide overlapping bars. However, you can help the calculation by setting your data step implicitly using the dataStep property. Consider the following example, with data mainly clumped in the domain 0 < x < 1:

<VisGroupedBar x={x} y={y}/>
Loading...
<VisGroupedBar x={x} y={y} dataStep={0.1}/>
Loading...

Rounded Corners

You can apply rounded corners to the top bar in your Grouped Bar component using the roundedCorners property, which accepts either a number (in pixels) or boolean argument.

<VisGroupedBar roundedCorners={true} x={x} y={y}/>
Loading...
<VisGroupedBar roundedCorners={5} x={x} y={y}/>
Loading...

Bar Color

Set the color of the bar by assigning the color property to a hex string and/or by assigning the color property to a function evaluated per each bar. In this example, each bar's color is assigned based on its value:

component.tsx
function Component(props) {
const data: DataRecord[] = props.data
const x = (d: DataRecord) => d.x
const y = [
(d: DataRecord) => d.y,
(d: DataRecord) => d.y1,
(d: DataRecord) => d.y2
]
const color = (d: DataRecord, i: number) => ['#04c0c7','#5144d3','#da348f'][i]

return (
<VisGroupedBar x={x} y={y} color={color}/>
)
}
Loading...

Orientation

Grouped Bar supports horizontal and vertical orientation.

<VisGroupedBar orientation="horizontal" x={x} y={y}/>
Loading...

Ordinal Data

Read our guide about using ordinal/categorical values with XY Components here

Events

import { GroupedBar } from '@unovis/ts`
...
events = {
[GroupedBar.selectors.bar]: {
click: (d: DataRecord) => {},
...
},
[GroupedBar.selectors.barGroup]: {
mouseover: (d: DataRecord[]) => {},
...
},
}
<VisGroupedBar x={x} y={y} events={events}/>

Additional Styling: CSS Variables

The Grouped Bar component supports additional styling via CSS variables that you can define for your visualization container. For example:

styles.css
.custom-vis {
--vis-grouped-bar-stroke-width: 2px;
--vis-grouped-bar-stroke-color: #000;
--vis-grouped-bar-cursor: crosshair;
}
Loading...
Supported CSS variables and their default values
--vis-grouped-bar-cursor: default;
--vis-grouped-bar-fill-color: var(--vis-color-main);
--vis-grouped-bar-stroke-color: none;
--vis-grouped-bar-stroke-width: 0px;
--vis-grouped-bar-hover-stroke-width: 1px;
--vis-grouped-bar-hover-stroke-color: none;

Component Props

NameTypeDescription
* required property