On this page

Guides

Server Components

The charts work in the Next.js App Router and any other framework with React Server Components. The rule is small: a chart is a client component, so everything you give it has to be able to cross from the server to the browser. Plain data does, functions do not.

Rendering a chart from a server file #

The main entry tally-charts starts with 'use client'. Importing <Chart>, BarChart or ZoneMeter from it in a Server Component file is fine, because the entry already marks the boundary. The server renders the SVG into the HTML and the browser hydrates it for hover and keyboard. A file that only passes data and strings does not need 'use client' itself.

Median load time by page, rendered on the server
View of Median load time by page, rendered on the server
Median load time by page, rendered on the server
PageSeconds
Home1.8
Pricing2.4
Docs2.9
Checkout3.6
Search4.4

Import from tally-charts/core on the server #

Server code that needs a helper rather than a component should import from tally-charts/core. It has no React in it and is safe on the server: the formatters (fmt, createFormatter, createFormatters, createDateFormatter), thresholds, heatmapLevel, runwayForecast, toNumber, toTime and the shared types. The example above calls thresholds there to build the bins of its ZoneMeter. See Formatting and Thresholds for the helpers themselves.

What can cross the boundary #

Props must be serializable. Strings, numbers, booleans, arrays and plain objects are, so presets such as format="currency-compact", format={{ preset: 'number', digits: 1 }}, data and x="month" work from a server file. Function props do not: fill, tone, a function format, xAxisFormat, tooltip and the result of thresholds(), which carries methods. Pass those from a client component.

ZoneMeter has a way around it. Its zones also takes the plain part of a thresholds() result, so zones={{ bins: lcp.bins, inclusive: lcp.inclusive }} works from a Server Component, as in the example above.

A client boundary #

Put the chart that needs a function in its own file with 'use client' and import it from the server page. The page then passes only data.

margin-chart.tsx
// app/margins/margin-chart.tsx
'use client'

import { DotHistogram, thresholds } from 'tally-charts'

const bands = thresholds(
  [10, 25, 38],
  ['loss', 'warn', 'accent-soft', 'gain'],
  ['Critical', 'At risk', 'Below target', 'On target'],
)

export function MarginChart({ jobs }: { jobs: { id: string; margin: number }[] }) {
  return (
    <DotHistogram
      title="Jobs by margin"
      data={jobs}
      value="margin"
      toneBy={bands}
      format={(v) => `${v}%`}
    />
  )
}

// app/margins/page.tsx (a Server Component, no directive)
import { MarginChart } from './margin-chart'

export default async function Page() {
  const jobs = await loadJobs()
  return <MarginChart jobs={jobs} />
}

toneBy={bands} and the function format stay inside the client file, where they never have to be serialized. page.tsx loads the data on the server and hands the array over. For dates, give the chart ISO strings and keep the default zone of 'utc', so the server and the browser draw the same ticks; see Time scales.