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,
(d: DataRecord) => d.y1,
(d: DataRecord) => d.y2
]

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

Orientation

Grouped Bar supports horizontal and vertical orientation.

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

Bar Colors

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 index:

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...

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...

Bar Sizing

There are multiple configuration properties that contribute to the size of you bars in Grouped Bar. Some are on the group level while others on the individual.

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 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 vs. Bar Padding

Another way to control bar width is by adding padding to bar groups (groupPadding property) or individual bars (`barPadding). The value 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...
<VisGroupedBar barPadding={0.5} x={x} y={y}/>
Loading...

Minimum Bar Height

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...

Preventing Overlaps with dataStep

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...

Using 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}/>

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