Skip to Content
TelamonJS

BarChart

Bar chart component for comparing values across categories

Bar chart component built on Recharts for comparing values across categories, with vertical (default) or horizontal bars.

Built on Recharts

All chart components are built on Recharts with pre-configured theming and responsive behavior. Recharts ships with @telamonjs/ui — no separate install.


Basic Usage

import { BarChart } from '@telamonjs/ui'

const data = [
  { name: 'Electronics', sales: 4000 },
  { name: 'Clothing', sales: 3000 },
  { name: 'Home', sales: 2000 },
  { name: 'Sports', sales: 2780 },
]

export default function SalesByCategory() {
  return <BarChart data={data} dataKeys={['sales']} />
}

The category axis reads the `name` field

BarChart takes flat dataKeys: string[], one bar series per key. The category axis is fixed to the name field of each data point.


Props

Prop

Type


Examples

Grouped Series

const data = [
  { name: 'Q1', online: 4000, stores: 2400 },
  { name: 'Q2', online: 3000, stores: 1398 },
  { name: 'Q3', online: 5000, stores: 3200 },
]

<BarChart
  data={data}
  dataKeys={['online', 'stores']}
  colors={['#3b82f6', '#10b981']}
/>

Horizontal Bars

<BarChart
  data={topProducts}
  dataKeys={['sales']}
  layout='vertical'
  height={400}
/>

Horizontal bars (layout='vertical') work well for long category labels and rankings. The rounded corners follow the layout: each bar is rounded at its end (the top of a vertical bar, the right of a horizontal one).

Counts and Long Labels

<BarChart
  data={ticketsByTeam}
  dataKeys={['open']}
  layout='vertical'
  allowDecimals={false}
  categoryAxisWidth={160}
/>

allowDecimals={false} keeps the value axis on whole numbers (no 0.5 ticks for a count); categoryAxisWidth widens the label column so long names are not clipped.

With React Query

'use client'

import { BarChart } from '@telamonjs/ui'
import { useQuery } from '@tanstack/react-query'
import { metricsApi } from '@/lib/api/metrics-client'

export function SalesByCategory() {
  const { data = [] } = useQuery({
    queryKey: ['sales-by-category'],
    queryFn: metricsApi.salesByCategory,
  })

  return <BarChart data={data} dataKeys={['total']} />
}

Lazy Variant

BarChartLazy has the same props but code-splits the Recharts bundle:

import { BarChartLazy } from '@telamonjs/ui'

<BarChartLazy data={data} dataKeys={['sales']} />

Best Practices

  1. Name the category field name - Map your API data if it uses another key
  2. Sort deliberately - Rankings descending, time buckets chronologically
  3. Few series - 2-3 grouped series max; more becomes unreadable
  4. Horizontal for long labels - Switch layout instead of rotating labels


Status: ✅ Stable

On this page