Command palette

@yasinsensoy/web

Web · Components

Chart

Chart: bar, line, area, pie, donut and scatter.

19 exports

Live example

Preview

Use the arrow keys to move between values.
Monthly revenue
ayRevenueExpenses
Jan$42,000$36,000
Feb$50,094$36,865
Mar$56,509$36,442
Apr$60,131$35,224
May$60,785$33,903
Jun$59,292$33,193
Jul$57,196$33,660
Aug$56,245$35,581
Sep$57,800$38,878
Oct$62,380$43,135
Nov$69,488$47,702
Dec$77,777$51,852

Code

import { Chart } from "@yasinsensoy/web";

<Chart
  data={aylik}
  x="ay"
  series={[{ key: "income", label: "Revenue" }, { key: "expense", label: "Expenses" }]}
  format="currency" zoom
  label="Monthly revenue"
/>

Events

  1. Events appear here when you interact with the preview.

Zoom: 100,000 points, range navigator and the selected range

All data
Use the arrow keys to move between values.

Zooming on categories: 36 months, the last 12 at first

Use the arrow keys to move between values.
Monthly revenue and expenses
ayRevenueExpenses
Jan 2442K36K
Feb 2448.4K36.2K
Mar 2453.1K35K
Apr 2455K33.1K
May 2454K31.1K
Jun 2450.8K29.7K
Jul 2447K29.5K
Aug 2444.3K30.7K
Sep 2444.2K33.3K
Oct 2447.1K36.8K
Nov 2452.5K40.7K
Dec 2459.1K44.2K
Jan 2565.1K46.6K
Feb 2569K47.6K
Mar 2570K47.1K
Apr 2568.1K45.6K
May 2564.5K43.5K
Jun 2560.9K41.7K
Jul 2558.9K40.7K
Aug 2559.7K41.1K
Sep 2563.5K43K
Oct 2569.5K46K
Nov 2576.1K49.8K
Dec 2581.6K53.6K
Jan 2684.6K56.7K
Feb 2684.6K58.5K
Mar 2682K58.8K
Apr 2678.2K57.9K
May 2675K56K
Jun 2673.8K54K
Jul 2675.6K52.4K
Aug 2680.2K52K
Sep 2686.6K53.1K
Oct 2693K55.5K
Nov 2697.8K58.9K
Dec 2699.9K62.8K

Inside a card: title, summary and bars + a line (target) together

Revenue and target

2026, monthly
18% up
Use the arrow keys to move between values.
Revenue and target
ayRevenueTarget
Jan$42,000$50,000
Feb$50,094$52,500
Mar$56,509$55,000
Apr$60,131$57,500
May$60,785$60,000
Jun$59,292$62,500
Jul$57,196$65,000
Aug$56,245$67,500
Sep$57,800$70,000
Oct$62,380$72,500
Nov$69,488$75,000
Dec$77,777$77,500

Sparkline: stat boxes (server component)

Visitors
48,2 B
Conversion
%3,4
Orders
1.284

Horizontal bars, long names and value labels

Use the arrow keys to move between values.
Sales by city
citySales
İstanbul1,840
Ankara920
İzmir760
Bursa410
Antalya380

Negative values: profit / loss

Use the arrow keys to move between values.
Net profit
ayNet
Jan0
Feb10.1K
Mar10.9K
Apr1.7K
May-9.1K
Jun-11.5K
Jul-3.4K
Aug7.9K

Import

import { Chart, Sparkline } from "@yasinsensoy/web";

Chart

componentClient component

Chart: bar, line, area, pie, donut and scatter. Works with its own SVG renderer; no external library. Colors come from the theme's --ui-chart-1 … --ui-chart-8 variables and change automatically in the dark theme.

  • Easy to use: give the data and fields, the rest is computed: axis range, round ticks, abbreviated numbers (12.5K), a time axis (day/month/year), thinning of overlapping labels.
  • Tooltip: on lines a vertical cursor snaps to the nearest x and lists all series; a band for bars, a slice for pies, the nearest point for scatter.
  • Legend: appears automatically with multiple series; a click hides a series, hover brings it forward. Colors are tied to series; hiding one doesn't change the others' colors.
  • Mobile: sliding your finger sideways on the chart makes the tooltip follow the values; vertical swipes scroll the page. Tapping elsewhere closes the tooltip.
  • Accessibility: the chart is keyboard focusable; arrow keys move between values and the value is read to the screen reader. The data is also in a hidden table for screen readers.
  • Performance: a single <path> per series; a line with 100,000 points is reduced to the min/max of each pixel column (peaks aren't lost). Hover redraws only the cursor layer.
  • Next.js: a client component; on the server a fixed-height area and an accessible table are drawn, and the chart is painted once the width is measured (no layout shift).
Chart({ data, type, x, series, label, height: heightProp, stacked, horizontal, curve, dots, legend, tooltip, renderTooltip, grid, xAxis, yAxis, xLabel, format, xFormat, yMin, yMax, labels, references, colors, barSize, center, maxSlices, loading, empty, animate, onPointClick, zoom, range, defaultRange, onRangeChange, locale: localeProp, currency: currencyProp, classNames, className, ...props }: ChartProps<T>): JSX.Element

Examples

const sales = [
  { month: "Jan", revenue: 12400, cost: 8100 },
  { month: "Feb", revenue: 15100, cost: 9300 },
];

<Chart data={sales} x="month" series={["revenue", "cost"]} format="currency" />

<Chart type="line" data={daily} x="date" label="Visitors"
  series={[{ key: "visits", label: "Visits" }, { key: "lastYear", label: "Last year", dashed: true }]}
  references={[{ value: 5000, label: "Target", tone: "success" }]} />

<Chart type="donut" data={browsers} x="name" series="share" format="percent" />

Sparkline

componentServer component

A small chart showing a trend next to text or a number (no axes, no tooltip). A server component; needs no JavaScript and its width fits the container. Use Chart for interaction and axes.

Sparkline({ data, type, color, height, endDot, label, className, style, ...props }: SparklineProps): JSX.Element

Examples

<div className="flex items-end gap-3">
  <span className="font-semibold text-2xl">$48.2K</span>
  <Sparkline data={[12, 14, 13, 18, 21, 19, 24]} type="area" color="success" className="w-24" />
</div>

ChartSeries

interface

Detailed definition of a Chart series.

NameTypeDefaultDescription
keyKey<T> | (string & {})Field the value is read from (e.g. "revenue"). The series id; must be unique.
label?stringkeyName in the legend and tooltip.
color?ChartColorits order in the paletteColor. See ChartColor.
type?ChartSeriesTypethe chart's `typeDrawing type of this series; to show bars and lines together (e.g. revenue as bars, target as a line).
value?(row: T) => number | null | undefinedReads the value from a function instead of a field (e.g. (r) => r.revenue - r.cost).
format?ChartFormatthe chart's `formatValue format of this series.
curve?ChartCurvethe chart's `curveLine style.
dashed?booleanDashed line (e.g. forecast, last year).
dots?booleantrue` with few pointsShow points on the line.
hidden?booleanThe series starts hidden; can be shown from the legend.

ChartPoint

interface

A data point; given in click events and the tooltip.

NameTypeDescription
rowTThe row itself.
indexnumberIndex of the row in data.
seriesstringkey of the series.
valuenumber | nullValue (null = empty).

A row in the tooltip. See ChartProps.renderTooltip.

NameTypeDescription
keystringkey of the series.
labelstringName of the series.
colorstringColor of the series (CSS).
valuenumber | nullValue (null = empty).
formattedstringFormatted value.
share?numberShare in a percent stack or a pie (0–1).

Info given to the ChartProps.renderTooltip function.

NameTypeDescription
titlestringTitle: the formatted x value; the slice name in a pie.
rowT | undefinedThe row (undefined for the "Other" slice).
indexnumberIndex of the row in data (-1 for the "Other" slice).
itemsChartTooltipItem[]Values of the series.

Value of the ChartRange ends: an x value (category name, number or date).

type ChartRangeValue = string | number | Date

Visible range: [start, end] x values.

  • Time axis: a Date, an ISO string ("2026-03-01") or ms. Events give a Date.
  • Number axis: numbers.
  • Category (bar): x values of the categories at the ends; both included (e.g. ["Mar", "Jun"]).
type ChartRange = readonly [ChartRangeValue, ChartRangeValue]

Detailed settings of ChartProps.zoom.

NameTypeDefaultDescription
brush?booleanfalseRange navigator at the bottom: a small drawing of all the data. The visible window is dragged to scroll and narrowed with the handles at its edges; dragging on empty space selects a new range and a click goes to that point.
brushHeight?number48Height of the range navigator (px).
wheel?boolean | "ctrl""ctrl"Zooming with the mouse wheel. - "ctrl": while Ctrl (⌘ on Mac) is held; the wheel alone scrolls the page and a short hint appears. Two-finger pinch on a touchpad also works this way. - true: always (for full-screen panels). - false: off. Horizontal scrolling (touchpad or Shift + wheel) pans the range while zoomed.
select?booleantrueZooming by dragging to select a range with the mouse. Shift + drag (or the middle button) pans.
toolbar?booleantrueZoom out / zoom in / reset buttons at the top right. Always visible while zoomed, otherwise on hover.

Classes that can be given to the inner parts of Chart.

NameTypeDescription
plot?stringDrawing area (a fixed-height box).
legend?stringLegend list.
tooltip?stringTooltip box.
toolbar?stringZoom buttons.
brush?stringRange navigator.

ChartProps

interface

Props of the Chart component.

Extends: Omit<ComponentProps<"figure">, "children" | "onSelect">

NameTypeDefaultDescription
datareadonly T[]Rows. For large data keep the array stable with useMemo.
type?ChartType"bar"Chart type. - "bar": bars; horizontal with horizontal, stacked with stacked. - "line": lines; time series and trends. - "area": filled lines; share distribution with stacked. - "pie", "donut": parts of a whole; x is the slice name, series the value. - "scatter": scatter; x and the series value are numbers.
xKey<T> | ((row: T) => unknown)Field of the x value (category, date or number). The slice name in a pie. Dates (Date or "2026-10-29") are drawn as a time axis.
seriesKey<T> | readonly (Key<T> | ChartSeries<T>)[]Values to draw: a single field ("revenue"), fields (["revenue", "cost"]) or detailed definitions (see ChartSeries).
label?stringScreen reader name of the chart (e.g. "Monthly revenue"). Use Card for a visible title.
height?number300Height of the drawing area (px). The width fits the container.
stacked?boolean | "percent"Stacks bar and area series; a 100% stack with "percent".
horizontal?booleanHorizontal bars (for long category names).
curve?ChartCurve"smooth"Line style: "smooth", "linear", "step".
dots?booleantrue` with few pointsShow points on lines.
legend?boolean | "top" | "bottom""bottom"` with multiple series and for piesLegend (series names). A click hides/shows a series, hover brings it forward.
tooltip?booleantrueTooltip showing values on hover, touch and keyboard.
renderTooltip?(info: ChartTooltipInfo<T>) => ReactNodeDraw the tooltip content yourself.
grid?booleantrueHorizontal grid lines.
xAxis?booleantruex axis labels (category names for horizontal bars).
yAxis?booleantrueValue axis labels.
xLabel?stringName of the x value; used in the header of the screen reader table (e.g. "Month").
format?ChartFormat"number"Value format (axis, tooltip, labels). See ChartFormat.
xFormat?ChartXFormatFormat of the x values. See ChartXFormat.
yMin?numberLower bound of the value axis. From the data when omitted (from 0 for bars).
yMax?numberUpper bound of the value axis.
labels?booleanValue labels: at bar ends, at the line end, percentages on pie slices. Only those that fit are written.
references?readonly ChartReference[]Reference lines (target, average). See ChartReference.
colors?readonly ChartColor[]Colors used in order; by series (slice in a pie) order.
barSize?number32Maximum bar thickness (px).
center?ReactNode | falseCenter of a donut chart. Shows the total when omitted; hovering a slice shows that slice. Empty with false.
maxSlices?number8Maximum slices in a pie; the rest are collected into "Other".
loading?booleanData is loading. With data the chart stays faded; without, a skeleton is drawn.
empty?ReactNodemessages.chartEmptyContent shown without data.
animate?booleantrueIntro and data change animations. Off with the reduced motion preference.
onPointClick?(point: ChartPoint<T>) => voidWhen a value is clicked (tapped).
zoom?boolean | ChartZoomOptionsZoom and pan (on the x axis; the value axis fits the visible data automatically). Works for bar, line, area and scatter. - Mouse: drag to select a range; Ctrl/⌘ + wheel zooms at the pointer; Shift + drag or horizontal scrolling pans; double click resets. - Touch: pinch and pan with two fingers; double tap resets. One finger shows values as before, and vertical swipes scroll the page. - Keyboard: + / - zoom, 0 resets, Shift + arrow pans; when arrows move outside the visible window the range follows the value. - Range navigator: at the bottom with { brush: true } (see ChartZoomOptions). Smooth even at 100,000 points: while zoomed only the visible window is drawn and downsampling is done for that window (detail appears).
range?ChartRange | nullVisible range (controlled). null is all data. Give it with onRangeChange. See ChartRange.
defaultRange?ChartRangeInitial visible range (uncontrolled).
onRangeChange?(range: ChartRange | null) => voidWhen the visible range changes; null when back to all. May be called every frame while zooming; defer expensive work.
locale?string{@link UIProvider} `localeNumber and date locale.
currency?string{@link UIProvider} `currencyCurrency.
classNames?ChartClassNamesClasses added to the inner parts.

Format of chart values (y axis, tooltip, labels).

  • "number": Locale-grouped number (12,480). On the axis, values above 10,000 are abbreviated (12.5K).
  • "compact": Abbreviated everywhere (1.2M).
  • "percent": A ratio as a percentage (0.25 → 25%).
  • "currency": Money ($1,250.00); the unit comes from the UIProvider currency setting or the chart's currency prop.
  • Intl.NumberFormatOptions: Full control (e.g. { style: "unit", unit: "kilogram" }).
  • Function: Your own format (e.g. (v) => ${v} pcs``).
type ChartFormat = "number" | "compact" | "percent" | "currency" | Intl.NumberFormatOptions | ((value: number) => string)

Format of chart x values (axis and tooltip title).

  • "date": Day (Oct 29, 2026), "datetime": day and time, "time": time, "month": month and year, "year": year.
  • Intl.DateTimeFormatOptions: Full control.
  • Function: Your own format; receives the raw x value.

When omitted, dates are formatted automatically by time range and other values are written as is.

type ChartXFormat = "date" | "datetime" | "time" | "month" | "year" | Intl.DateTimeFormatOptions | ((value: unknown) => string)

Chart type. See ChartProps.type.

type ChartType = "bar" | "line" | "area" | "pie" | "donut" | "scatter"

How a series is drawn. In bar and line charts it may differ per series.

type ChartSeriesType = "bar" | "line" | "area"

Color: a tone ("success"), a palette index ("chart-3") or any CSS color ("#7c3aed", "var(--brand)"). When omitted, series take --ui-chart-1 … --ui-chart-8 in order.

type ChartColor = Tone | `chart-${1 | 2 | 3 | 4 | 5 | 6 | 7 | 8}` | (string & {})

A horizontal or vertical reference line (target, average, limit).

NameTypeDefaultDescription
valuenumberValue of the line (on the y axis; on the value axis for horizontal bars).
label?stringShort text next to the line (e.g. "Target").
tone?Tone"neutral"Color.

Shape of the line between points.

type ChartCurve = "smooth" | "linear" | "step"

Props of the Sparkline component.

Extends: Omit<ComponentProps<"svg">, "children" | "color">

NameTypeDefaultDescription
datareadonly (number | null | undefined)[]Values; null is a gap.
type?"line" | "area" | "bar""line"Drawing type.
color?ChartColor"chart-1"Color. See ChartColor.
height?number32Height (px). The width fits the container.
endDot?booleantrueHighlight the last value with a dot (line and area).
label?stringScreen reader text (e.g. "Last 12 months: rising"). Hidden when omitted.