Bar Chart

Series as bars: side by side, stacked, below 0; each corner's radius of your choice. Each series' name and color come from the chart's config; the keyboard moves through the categories, and the tooltip says their values.

ChartsExperimental

Installation

It has its own entry point, @semantic-components/ui/charts, built on d3-scale d3-shape — an optional peer of ui: install it where you use the component.

ts
import { ScChart, ScChartPlot, ScChartBar } from '@semantic-components/ui/charts';

Usage

import { DecimalPipe } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import {  ScChart,  ScChartBar,  ScChartCategoryAxis,  type ScChartConfig,  ScChartCursor,  ScChartGrid,  ScChartPlot,  ScChartTooltip,  ScChartTooltipIndicator,  ScChartTooltipItem,  ScChartTooltipName,  ScChartTooltipPortal,  ScChartTooltipProvider,  ScChartTooltipValue,} from '@semantic-components/ui/charts';
@Component({  selector: 'app-bar-chart-usage-demo',  imports: [    ScChart,    ScChartPlot,    ScChartGrid,    ScChartCategoryAxis,    ScChartCursor,    ScChartBar,    ScChartTooltip,    ScChartTooltipPortal,    ScChartTooltipProvider,    DecimalPipe,    ScChartTooltipIndicator,    ScChartTooltipItem,    ScChartTooltipName,    ScChartTooltipValue,  ],  template: `    <div scChart [config]="config" [data]="data">      <svg        scChartPlot        categoryKey="month"        aria-label="Desktop visitors, January to June"      >        <g scChartGrid />        <g scChartCategoryAxis [tickFormatter]="short" />        <g scChartCursor />        <g scChartBar key="desktop" />      </svg>      <div scChartTooltipProvider>        <ng-template scChartTooltipPortal>          <div scChartTooltip #tooltip="scChartTooltip">            @for (item of tooltip.payload(); track item.dataKey) {              <div scChartTooltipItem [item]="item">                <span scChartTooltipIndicator></span>                <span scChartTooltipName>{{ item.name }}</span>                <span scChartTooltipValue>{{ item.value | number }}</span>              </div>            }          </div>        </ng-template>      </div>    </div>  `,  host: { class: 'block w-full max-w-xl' },  encapsulation: ViewEncapsulation.None,})export class BarChartUsageDemo {  private readonly data = [    { month: 'January', desktop: 186, mobile: 80 },    { month: 'February', desktop: 305, mobile: 200 },    { month: 'March', desktop: 237, mobile: 120 },    { month: 'April', desktop: 73, mobile: 190 },    { month: 'May', desktop: 209, mobile: 130 },    { month: 'June', desktop: 214, mobile: 140 },  ];  private readonly config: ScChartConfig = {    desktop: { label: 'Desktop', color: 'var(--chart-1)' },    mobile: { label: 'Mobile', color: 'var(--chart-2)' },  };  /** January → Jan. */  private readonly short = (month: string) => month.slice(0, 3);}

Anatomy

A chart (its rows and config), its plot with the series, and the tooltip and legend you write from their state.

import { ScChart, ScChartPlot, ScChartCategoryAxis, ScChartCursor, ScChartGrid, ScChartValueAxis, ScChartBar, ScChartTooltip, ScChartTooltipIndicator, ScChartTooltipItem, ScChartTooltipName, ScChartTooltipPortal, ScChartTooltipProvider, ScChartTooltipValue, ScChartLegend, ScChartLegendIndicator, ScChartLegendItem } from '@semantic-components/ui/charts';
<div scChart [config]="…" [data]="…">  <svg scChartPlot categoryKey="…" aria-label="…">    <g scChartGrid />    <g scChartCategoryAxis />    <g scChartValueAxis />    <g scChartCursor />    <g scChartBar key="…" />  </svg>  <div scChartTooltipProvider>    <ng-template scChartTooltipPortal>      <div scChartTooltip #tooltip="scChartTooltip">        <div scChartTooltipLabel>…</div>        <div scChartTooltipItem [item]="…">          <span scChartTooltipIndicator></span>          <span scChartTooltipName>…</span>          <span scChartTooltipValue>…</span>        </div>      </div>    </ng-template>  </div>  <div scChartLegend #legend="scChartLegend">    <div scChartLegendItem [item]="…">      <span scChartLegendIndicator></span>    </div>  </div></div>

Accessibility

  • The plot is one tab stop, a group named by its aria-label: the arrow keys, Home and End move through its categories; Escape closes the tooltip.
  • As the keyboard moves, screen readers hear what the tooltip says, through the CDK's LiveAnnouncer (politely).
  • Its axes, grid and cursor are hidden from assistive technology: the tooltip says what they show.
  • Right to left, it can be mirrored (the app's charts.followDirection, or the axes' reversed): the arrows then go the way it is drawn.
  • With reduced motion (prefers-reduced-motion), it appears and changes at once.

Examples

Bar, two series

Desktop
Mobile
import { DecimalPipe } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import {  ScChart,  ScChartBar,  ScChartCategoryAxis,  type ScChartConfig,  ScChartCursor,  ScChartGrid,  ScChartLegend,

Bar, stacked

Desktop
Mobile
import { DecimalPipe } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import {  ScChart,  ScChartBar,  ScChartCategoryAxis,  type ScChartConfig,  ScChartCursor,  ScChartGrid,  ScChartLegend,

Bar, negative values

import { DecimalPipe } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import {  ScChart,  ScChartBar,  ScChartCategoryAxis,  type ScChartConfig,  ScChartCursor,  ScChartGrid,  ScChartPlot,

Horizontal

import { DecimalPipe } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import {  ScChart,  ScChartBar,  ScChartCategoryAxis,  type ScChartConfig,  ScChartCursor,  ScChartPlot,  ScChartTooltip,

With labels

import { DecimalPipe } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import {  ScChart,  ScChartBar,  ScChartCategoryAxis,  type ScChartConfig,  ScChartCursor,  ScChartGrid,  ScChartLabels,

Horizontal, its name inside

import { DecimalPipe } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import {  ScChart,  ScChartBar,  type ScChartConfig,  ScChartCursor,  ScChartLabels,  ScChartPlot,  ScChartTooltip,

RTL

سطح المكتب
الجوال
import { Dir } from '@angular/cdk/bidi';import { Component, LOCALE_ID, ViewEncapsulation } from '@angular/core';import {  ScChart,  ScChartBar,  ScChartCategoryAxis,  type ScChartConfig,  ScChartCursor,  ScChartGrid,  ScChartLegend,

API reference

The series' inputs. The chart, its plot, axes, tooltip and legend are on the Chart page.

InputTypeDefaultDescription
keystring (required)— the series' key in the rows
stackstring | nullnull bars of a stack on one another
cornersall | end | base | noneall the rounded ones — the bar's end's two (a column's top), its base's two; how round is the style's (see [Corners](#corners))
colorBykey | categorykey its key's colour, or each bar its row's category's (--color-<category>, from the config) — the tooltip and the legend then name the categories
fillstring'' instead of its color, url(#gradient)
backgroundboolean | stringfalse A track behind each bar, its slot's whole range of values (Recharts' background): true for the muted one, or your classes (fill-secondary); its corners the bars'.
barSizenumberthe slot's its width in pixels, centered in its slot (Recharts' barSize)
maxBarSizenumber— its widest, in pixels (Recharts' maxBarSize)
isAnimationActivebooleantrue its bars grow from their base when it appears (Recharts' entrance)
animationBeginnumber0 the wait before they grow, in ms
animationDurationnumber400 how long they grow, in ms
animationEasingease, ease-in, ease-out, ease-in-out, linearease
animationMatchBy'index' (by position, stretched), 'append' (the first with the first) or (row, index) => key'append' how its shapes before and after a change are paired (Recharts')
classstring'' Merged over its own classes.