We use cookies to ensure you get the best experience on our website. Read our privacy policy

Skip to main content

Directory

Bar Chart Component

The Bar Chart component provides an intuitive way to display data comparisons. It features smooth animations, responsive design, and comprehensive accessibility support.

  • Check Circle

    Responsive Design: Automatically adjusts to container size

  • Check Circle

    Interactive Elements: Hover states, tooltips, and click events

  • Check Circle

    Multiple Datasets: Support for comparing multiple data series

  • Check Circle

    Customizable: Flexible styling, colors, and configuration options

  • Check Circle

    Accessibility: ARIA support and screen reader compatibility

Create a simple bar chart with a single dataset. The color accepts a CSS color value or a variable.

Business Sales

Sales by day

Sales
<BarChart
data={{
datasets: [{
label: 'Sales', values: [10, 25, 30, 55, 85],
color: '--color-primary',
hoverColor: '--color-secondary',
tooltip: 'Sales',
opacity: 0.5
}],
labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
}}
title="Business Sales"
subtitle="Sales by day"
/>

Compare multiple data series in one chart:

Sales 2023
Sales 2024
<BarChart
data={{
datasets: [
{ label: 'Sales 2023', values: [30, 45, 60], color: '#4CAF50' },
{ label: 'Sales 2024', values: [45, 55, 70], color: '#2196F3' }
],
labels: ['Q1', 'Q2', 'Q3']
}}
/>

Display bars horizontally for different visualization needs:

Series 1
<BarChart
data={{
datasets: [
{ label: 'Series 1', values: [30, 45, 60], color: '--color-tertiary' }
],
labels: ['Category A', 'Category B', 'Category C']
}}
options={{
orientation: 'horizontal',
showLines: true
}}
/>

Each dataset in the datasets array supports these properties:

Dataset Interface
interface Dataset {
label: string; // Name of the data series
values: number[]; // Array of numeric values
color?: string; // Bar color (CSS color value or variable)
hoverColor?: string; // Color when hovering
opacity?: number; // Bar opacity (0-1)
tooltip?: string; // Custom tooltip text
}`

Customize the chart behavior and appearance:

BarChartOptions
interface BarChartOptions {
orientation?: 'horizontal' | 'vertical';
showLines?: boolean;
valueMin?: number;
valueMax?: number;
valueStep?: number;
}

Try out different chart configurations:

Series 1
Series 2
Number of Series
Values per Series
Chart Orientation
Y-Axis Maximum
Y-Axis Minimum
Y-Axis Step

The chart emits custom events for interaction:

Bar Click Event
// Emitted when a bar is clicked
CustomEvent<{
label?: string; // X-axis label
value: number; // Bar value
index: number; // Data index
}>;
Chart Update Event
// Emitted when chart data changes
CustomEvent<{
data: BarChartData;
options?: BarChartOptions;
}>;
Prop Type Default Description
data BarChartData Required Chart data including datasets and labels
title string Chart title
subtitle string Chart subtitle
height number 400 Chart height
options BarChartOptions Chart configuration options
animate boolean true Enable/disable animations
class string Additional CSS classes
Function Arguments Description
set: data BarChartData Chart data including datasets and labels
set: title string Chart title
set: subtitle string Chart subtitle
set: options BarChartOptions Chart configuration options
set: valueFormat function Format the value axis tick labels

The valueFormat function is used to format the value axis tick labels. It takes a number and returns a string.

import { ready } from '@/components/NST-components/base';
import type { BarChartElement } from '@/components/NST-components/UI/charts/bar/data';
const valueFormatChart = await ready(
document.querySelector<BarChartElement>('#value-format-chart')
);
if (valueFormatChart) {
valueFormatChart.valueFormat = (value: number) => `$${value.toFixed(2)}`;
}
Series 1

The Bar Chart component includes several accessibility enhancements:

  • Check Circle ARIA labels for data points
  • Check Circle Keyboard navigation support
  • Check Circle Screen reader descriptions
  • Check Circle High contrast support through theming

Limited Time Launch Sale

Create stunning data visualizations with our Bar Chart component. Get started with the AstroJS starter template now!

GET 60% OFF!
North Star Themes Logo

Subscribe to our newsletter

Get the latest AstroJS tips and tricks right to your inbox

Email: [email protected]

© 2025 North Star Themes

Web Kit Provided By North Star Themes