Integrated charts

SvGrid can chart its own data with no external charting library. Two pieces:

Feed it api.getDisplayedRows() and the chart reflects the grid's current, filtered, sorted data - the "chart from the grid" enterprise feature.

The grid's filtered and sorted rows flow through rowsToChartSpec into SvGridChart, which re-renders whenever the grid's filters or sorting change.

The examples on this page import from @svgrid/grid:

<script lang="ts">
  import { SvGrid, SvGridChart } from '@svgrid/grid'
</script>
<script lang="ts">
  import { SvGrid, SvGridChart, rowsToChartSpec, type ChartSpec, type GridColumns, type SvGridApi } from '@svgrid/grid'

  type Row = { id: number; region: string; product: string; revenue: number }
  const rows: Row[] = [
    { id: 1, region: 'EMEA', product: 'Grid', revenue: 1200 },
    { id: 2, region: 'EMEA', product: 'Charts', revenue: 800 },
    { id: 3, region: 'EMEA', product: 'Studio', revenue: 450 },
    { id: 4, region: 'APAC', product: 'Grid', revenue: 900 },
    { id: 5, region: 'APAC', product: 'Charts', revenue: 300 },
    { id: 6, region: 'Americas', product: 'Grid', revenue: 1500 },
    { id: 7, region: 'Americas', product: 'Charts', revenue: 700 },
    { id: 8, region: 'Americas', product: 'Studio', revenue: 620 },
  ]
  const columns: GridColumns<Row> = [
    { field: 'region', header: 'Region', width: 120 },
    { field: 'product', header: 'Product', width: 120 },
    { field: 'revenue', header: 'Revenue', width: 110, cellDataType: 'number' },
  ]
  let api: SvGridApi<{}, Row> | null = null
  let displayed = $state<Row[]>(rows)
  // The chart draws whatever the grid shows: filter or sort the grid and the bars follow.
  const sync = () => (displayed = (api?.getDisplayedRows() as Row[]) ?? rows)
  const spec = $derived<ChartSpec>({
    ...rowsToChartSpec(displayed, { type: 'bar', category: 'region', value: 'revenue', reduce: 'sum', height: 220 }),
    valueFormat: 'currency',
  })
</script>

<SvGrid data={rows} {columns} sortable filterable showFilterRow containerHeight={260}
  onApiReady={(a) => { api = a; sync() }}
  onFiltersChange={sync} onSortingChange={sync} />

<SvGridChart {spec} legend={false} />

Pages

More examples

Axes, titles and styling

Open the live example: Axes, titles and styling (Charts)

Synchronized charts with zoom, pan and presets

Open the live example: Synchronized charts with zoom, pan and presets (Charts)

Financial workbench

Open the live example: Financial workbench (Charts)

See also

Live examples

  • Axes, titles and styling - The chart hands the decisions back: a numeric x axis that spreads payload sizes the way the numbers do, pinned domains with a fixed tick interval and a formatter per axis, grid lines on or off, a shaded budget band, a vertical reference line, per-point markers and colours, a stepped dashed series on the right axis, a title / subtitle / caption, a legend on any side, and a tooltip snippet in single-series mode. Below it, 50,000 monitoring readings decimated to one point per pixel with LTTB or min / max - toggle it off to see the cost.
  • Synchronized charts with zoom, pan and presets - Two years of daily prices and volumes as two charts that move as one: hover either for a shared crosshair, Ctrl + wheel or pinch to zoom around the pointer, Shift + drag or the hand button to pan, 1W / 1M / 3M / 6M / YTD / 1Y / All presets, and a grid that shows exactly the rows in the window. Right-click for a context menu with export, zoom and series items plus your own. Below: bars that grow in and slide on every data change with multi-selection, and a sunburst that drills down on click with a breadcrumb back up.
  • Financial workbench - A year of daily prices as a trading terminal, free in the grid package: rows to candles with rowsToOhlcSpec, a daily / weekly toggle through resampleOhlc, Bollinger bands or an EMA on the price, volume, RSI, MACD, stochastic, ATR and OBV panes stacked under it with one x axis and one crosshair, a last-price pill, earnings and dividend flags with tooltips, drawing tools (trend line, ray, Fibonacci, rectangle, arrow, note) stored as data points, range presets, Ctrl + wheel zoom, PDF and print. Below, the same rows in the grid panel with Candlestick picked, indicator chips, the chart builder and the link toggle.

Related articles