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 x={x} y={y} orientation="horizontal"/>
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...

Custom Bar Styles

When color alone isn't enough, the barStyle accessor lets you style individual bars. It's called for every bar with the datum and the bar's index within the group, and the returned object is applied to the bar as inline styles. Property names can be camelCase or kebab-case, and CSS custom properties (--*) are supported. A common use case is rendering projected or estimated values differently from actuals:

const barStyle = (d: DataRecord, i: number) => d.x > 5
? { strokeDasharray: '4 3', strokeWidth: 1, fillOpacity: 0.2 }
: undefined
Loading...

A few things to keep in mind:

  • The properties the component manages itself — fill, cursor and mask — take precedence over their regular sources (like the color accessor) and stay animated; all other properties are applied instantly.
  • Properties that are no longer returned for a bar are removed on the next render, so conditional styles clean up after themselves.
  • Since inline styles bypass the CSS cascade, use CSS variable references (e.g. stroke: 'var(--my-color)') for values that should respond to theming.

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 x={x} y={y} roundedCorners={true}/>
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 x={x} y={y} groupWidth={50}/>
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 x={x} y={y} groupPadding={0.5}/>
Loading...
<VisGroupedBar x={x} y={y} barPadding={0.5}/>
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 x={x} y={y} barMinHeight={1}/>
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