Bar Segment

One bar split into parts, showing how each part adds up to a total.

500K
200K
100K
100K
Google
Bing
Direct
Yandex

Usage

Code
import { BarSegment, useChart } from '@leverui/components';
Code
function TrafficMix() {
  const chart = useChart({
    data: [
      { name: 'Direct', value: 60, color: 'teal.solid' },
      { name: 'Referral', value: 40, color: 'blue.solid' },
    ],
  });

  return (
    <BarSegment.Root chart={chart}>
      <BarSegment.Content>
        <BarSegment.Value />
        <BarSegment.Bar tooltip>
          <BarSegment.Reference value={50} label="Target" />
        </BarSegment.Bar>
        <BarSegment.Label />
      </BarSegment.Content>
      <BarSegment.Legend showPercent />
    </BarSegment.Root>
  );
}

Examples

Reference

Mark a 200,000 target across the source bar.

500K
200K
100K
80K
Target
Google
Bing
Direct
Yandex

Legend

Add colored names and percentages below the source bar.

500K
200K
100K
100K
Google56%
Bing22%
Direct11%
Yandex11%

Bar Size

Reduce the bar height for the language collection.

Ruby35%
JavaScript23%
SolidJS17%
HTML13%
CSS12%

Tooltip

Reveal name and value tooltips on hover or focus.

Google56%
Bing22%
Direct11%
Yandex11%

Props

Root

PropDefaultTypeDescription
chart—UseChartReturn<BarSegmentData>Value returned by useChart. Required.
barSize2.5remstringBar height.

Only Root is required.

barSize and the gap props take a spacing token such as "10" (2.5rem) or a CSS length such as "3rem".

Bar

PropDefaultTypeDescription
tooltipfalseboolean | functionShow a segment's name and value on hover and focus.
gap0.25remstringSpace between segments.

A tooltip function gets { payload }, the item from your data, and renders your own tooltip.

Reference

PropDefaultTypeDescription
value—numberMarker position on the bar. Required.
label—JSX.ElementText next to the marker.

Put it inside Bar. value is measured against the total of your data: with a total of 100, a value of 50 sits halfway along the bar.

Legend

PropDefaultTypeDescription
showValuefalsebooleanShow each value after its name.
showPercentfalsebooleanShow each share as a whole percentage.
valueFormatter—(value: number) => stringFormat each value when showValue is on.
gap1remstringSpace between legend items.
textStylesm'xs' | 'sm'Legend text size.

Tooltip

PropDefaultTypeDescription
payload—BarSegmentDataItem the tooltip belongs to. Required.

Bar adds this part for you when tooltip is on.

Content, Value, and Label have no props of their own. Value prints each number and Label each name, in line with its segment. Other attributes and event handlers go to each part's outer element.

Content closes the tooltip when the pointer leaves it. Your own onMouseLeave handler runs after that.

Data fields

FieldTypeDescription
namestringSegment name. Required.
valuenumberSegment size. Required.
colorstringColor token such as teal.solid, or a CSS color. Required.

Pass sort to useChart to order the segments.

Keyboard

Each segment takes focus when tooltip is on. A screen reader reads its name and value.

KeyAction
TabNext segment. Shows its tooltip.

Styling

Code
<BarSegment.Root chart={chart} class="traffic-mix">
  ...
</BarSegment.Root>
Code
.traffic-mix {
  --leverui-bar-segment-size: 3rem;
  --leverui-bar-segment-bar-gap: 0.5rem;
}

.traffic-mix .leverui-bar-segment__segment {
  border-radius: 0.75rem;
}

Variables

VariableDefaultDescription
--leverui-bar-segment-size2.5remBar height.
--leverui-bar-segment-root-gap1remSpace between the content and the legend.
--leverui-bar-segment-content-gap0.25remSpace between the values, the bar, and the names.
--leverui-bar-segment-row-gap0.5remSpace between items in the value and name rows.
--leverui-bar-segment-bar-gap0.25remSpace between segments.
--leverui-bar-segment-legend-gap1remSpace between legend items.

barSize sets the size variable on one chart. gap on Bar and Legend sets the gap variable for that part.

Classes

Parts, each prefixed with .leverui-bar-segment__: root, content, value, valueItem, bar, segment, reference, referenceLine, referenceLabel, label, labelItem, legend, legendItem, legendSwatch, legendText, legendPercent, tooltip, tooltipSwatch.