Skip to content

shadcn.io is an independent project built on shadcn/ui

Free

A bar chart with an active bar for Ark UI

Five browser bars, each in its own color, with Firefox drawn lighter and outlined by a dashed stroke. The highlight is a fixed index.

Five browser bars, each in its own color. Firefox is lighter, with a dashed outline in that same color. The highlight is a fixed index, not a hover.

Use it to point at one category while the rest stay visible. If the choice should follow a click, drive it from Tabs instead of a constant. In React, Ark UI does not choose Firefox.

Preview

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.
Use email insteadNo credit card

Anatomy

Chart anatomy: container, grid, axis, bar, tooltip, legend

Usage

<div className="w-full max-w-xl rounded-md border bg-background p-4">
  <ChartContainer config={chartConfig}>
    <BarChart accessibilityLayer data={chartData}>
      <CartesianGrid vertical={false} />
      <XAxis
        axisLine={false}
        dataKey="browser"
        tickFormatter={formatBrowser}
        tickLine={false}
        tickMargin={10}
      />
      <ChartTooltip content={renderTooltipContent} cursor={false} />
      <Bar dataKey="visitors" radius={8} shape={renderBar} strokeWidth={2} />
    </BarChart>
  </ChartContainer>
</div>

FAQ

How is the Firefox bar highlighted?

Bar sets shape to renderBar. That function compares props.index with ACTIVE_INDEX, which is 2, the Firefox row. A match renders Rectangle with fillOpacity 0.8, stroke from props.payload.fill, strokeDasharray 4, and strokeDashoffset 4.

How do I highlight a different bar?

Change ACTIVE_INDEX. The rows run Chrome, Safari, Firefox, Edge, Other, so 0 is Chrome. Any other index returns Rectangle with the bar props unchanged. Bar also sets radius to 8 and strokeWidth to 2, which is the outline width on the dashed bar.

Why is each bar a different color?

Each row sets fill to a variable such as var(--color-chrome). config maps chrome through other to --chart-1 through --chart-5 and to labels like Chrome. Bar has dataKey visitors and leaves fill to the row.

How do the ticks get names like Chrome?

XAxis uses dataKey browser and tickFormatter set to formatBrowser, which returns chartConfig[value].label. axisLine and tickLine are false, and tickMargin is 10.

What does the tooltip show on a bar?

ChartTooltipContent sets hideLabel, and cursor is false. The row name comes from the browser key in config. CartesianGrid sets vertical to false.

Does the keyboard reach these bars?

Yes. BarChart sets accessibilityLayer. The same config labels feed the axis ticks and the tooltip, so each browser is named in text as well as drawn in its own color.

API Reference

Chart

Sets config={chartConfig}, content={renderTooltipContent} and cursor={false}. The rest is on the Chart page.