Skip to content
Charts

Bar Chart

Recharts 3 bar chart with rounded bars, optional stacking, and a single-bar accent highlight.

MotionRecharts grow animation; disabled under reduced motion

Install

Install Kineto and follow the theme setup once in your app.

bash
npm install kineto
tsx
import { BarChart } from "kineto/bar-chart";

Preview

Compute activity (hrs)

tsx
"use client";

import * as React from "react";
import { useReducedMotion } from "motion/react";
import {
  Bar,
  BarChart as RechartsBarChart,
  CartesianGrid,
  Cell,
  Legend,
  ResponsiveContainer,
  Tooltip,
  XAxis,
  YAxis,
} from "recharts";

import { cn } from "@/lib/cn";
import { ChartTooltip } from "@/components/charts/chart-tooltip";

const DEFAULT_COLORS = ["--chart-1", "--chart-2", "--chart-3", "--chart-4", "--chart-5"];
const color = (v: string) => `hsl(var(${v}))`;

export interface BarChartProps extends React.HTMLAttributes<HTMLDivElement> {
  data: Array<Record<string, string | number>>;
  index: string;
  categories: string[];
  colors?: string[];
  height?: number;
  valueFormat?: (value: number) => string;
  showGrid?: boolean;
  showLegend?: boolean;
  showYAxis?: boolean;
  stacked?: boolean;
  /** When a single category is plotted, emphasize this bar in the accent color. */
  highlightIndex?: number;
}

export const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>(
  (
    {
      className,
      data,
      index,
      categories,
      colors = DEFAULT_COLORS,
      height = 240,
      valueFormat = (n) => n.toLocaleString(),
      showGrid = true,
      showLegend = false,
      showYAxis = false,
      stacked = false,
      highlightIndex,
      ...props
    },
    ref
  ) => {
    const reduced = useReducedMotion();
    const singleHighlight =
      categories.length === 1 && typeof highlightIndex === "number";

    return (
      <div ref={ref} className={cn("w-full", className)} style={{ height }} {...props}>
        <ResponsiveContainer width="100%" height="100%">
          <RechartsBarChart data={data} margin={{ top: 8, right: 8, bottom: 0, left: 0 }}>
            {showGrid ? (
              <CartesianGrid vertical={false} stroke="hsl(var(--border))" strokeDasharray="3 6" />
            ) : null}
            <XAxis
              dataKey={index}
              tickLine={false}
              axisLine={false}
              tick={{ fill: "hsl(var(--muted-foreground))", fontSize: 12 }}
              dy={8}
            />
            {showYAxis ? (
              <YAxis
                tickLine={false}
                axisLine={false}
                width={40}
                tick={{ fill: "hsl(var(--muted-foreground))", fontSize: 12 }}
                tickFormatter={(v) => valueFormat(Number(v))}
              />
            ) : null}
            <Tooltip
              cursor={{ fill: "hsl(var(--muted) / 0.5)" }}
              content={<ChartTooltip valueFormat={valueFormat} />}
            />
            {showLegend ? <Legend iconType="circle" /> : null}

            {singleHighlight ? (
              <Bar
                dataKey={categories[0] as string}
                radius={[6, 6, 0, 0]}
                isAnimationActive={!reduced}
                animationDuration={700}
                maxBarSize={48}
              >
                {data.map((_, i) => (
                  <Cell
                    key={i}
                    fill={i === highlightIndex ? color("--chart-1") : color("--chart-2")}
                    fillOpacity={i === highlightIndex ? 1 : 0.4}
                  />
                ))}
              </Bar>
            ) : (
              categories.map((cat, i) => (
                <Bar
                  key={cat}
                  dataKey={cat}
                  stackId={stacked ? "stack" : undefined}
                  fill={color(colors[i % colors.length] ?? "--chart-1")}
                  radius={stacked ? 0 : [6, 6, 0, 0]}
                  isAnimationActive={!reduced}
                  animationDuration={700}
                  maxBarSize={48}
                />
              ))
            )}
          </RechartsBarChart>
        </ResponsiveContainer>
      </div>
    );
  }
);
BarChart.displayName = "BarChart";

src/components/charts/bar-chart.tsx

Props

PropTypeDefaultDescription
data / index / categories——Same data API as the area chart.
highlightIndexnumber—Accent a single bar (one category).
stackedbooleanfalseStack multiple categories.