Skip to Content
Cristal

Data

Chart

Columns, bars, lines, areas or donut, readable on hover, with the keyboard and as a table.

<cr-chart>CrChart

Change over time: columns, lines, areas

labels gives the categories (months, for example) and series one or more series { name, values }, with one value per category. format formats the values with Intl.NumberFormat options: { style: 'currency', currency: 'USD' }, { style: 'percent' }… Axis ticks switch to compact notation ("$20K") from 10,000.

'use client';

import { useState } from 'react';
import { CrChart, CrSegmented } from '@cristal-ui/react';

const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const sales = [12400, 15800, 14100, 18900, 21300, 19700, 24800, 22100, 26500, 25200, 29800, 34100];
const returns = [900, 1100, 1300, 1000, 1500, 1200, 1700, 1400, 1600, 2100, 1800, 2400];

export default function ChartSales() {
  const [type, setType] = useState('column');

  return (
    <div style={{ display: 'grid', gap: 16, width: '100%' }}>
      <CrSegmented label="Chart type" value={type} onCrChange={(event) => setType(event.detail)}>
        <option value="column">Columns</option>
        <option value="line">Lines</option>
        <option value="area">Areas</option>
      </CrSegmented>
      <CrChart
        label="Sales and returns"
        type={type as 'column' | 'line' | 'area'}
        labels={months}
        series={[
          { name: 'Sales', values: sales },
          { name: 'Returns', values: returns },
        ]}
        format={{ style: 'currency', currency: 'USD', maximumFractionDigits: 0 }}
      />
    </div>
  );
}
  • column (default): grouped columns, one colour per series.
  • line: one line per series; on hover, a vertical hairline and one dot per series.
  • area: the line, with a tinted area below it.

A single series has no legend (the title is enough); its last value is written at the end of the line. From two series on, a legend names them.

Ranking and parts: bars, donut

'use client';

import { useState } from 'react';
import { CrChart, CrSegmented } from '@cristal-ui/react';

const channels = ['Web', 'Store', 'App', 'Wholesale', 'Phone', 'Trade shows', 'Marketplace'];
const orders = [1840, 1210, 760, 420, 180, 90, 60];

export default function ChartParts() {
  const [type, setType] = useState('bar');

  return (
    <div style={{ display: 'grid', gap: 16, width: '100%' }}>
      <CrSegmented label="Chart type" value={type} onCrChange={(event) => setType(event.detail)}>
        <option value="bar">Bars</option>
        <option value="donut">Donut</option>
      </CrSegmented>
      <CrChart label="Orders by channel" type={type as 'bar' | 'donut'} labels={channels} series={[{ name: 'Orders', values: orders }]} />
    </div>
  );
}
  • bar: horizontal bars, to compare categories with long names; the value is written at the tip of each bar.
  • donut: parts of a whole, with the total in the centre; on hover, the centre shows the part and its percentage. It uses the first series. Beyond 6 parts, the smallest are grouped into "Other". To compare close values, prefer bars.

Size and colours

  • The chart takes the full width of its container and is 240 pixels high; change the height with --cr-chart-height. In a fixed-height container (a dashboard card), give it height: 100%: the drawing takes all the free space. Bars take the height of their rows.
  • hide-label (hideLabel in React) visually hides the title when the card around the chart already shows it; it stays the name read by screen readers.
  • Series take, in order, the colours --cr-color-chart-1 to --cr-color-chart-8, with dedicated shades in the dark theme. This order was checked so that two neighbouring series stay distinct for colour-blind people: keep it, and beyond 8 series, group or split the chart (the extra series are not drawn).

labels, series and format are properties: in HTML, set them in JavaScript (chart.series = [...]), not as attributes.

If the user asked their system to reduce motion, columns, bars and lines appear without growing or drawing.

Accessibility

  • "Show data" opens a real table with every value: the equivalent of the drawing for screen readers, and for anyone who wants the exact numbers.
  • The drawing area can be reached with Tab; arrow keys (and Home, End) go through the categories: the tooltip shows as on hover and the value is announced, for example "Mar: Sales $14,100, Returns $1,300". Escape closes the tooltip.
  • Colour is never the only information: legend, written values, tooltip and table name every series. In high contrast mode, marks take the text colour.
  • Texts (ticks, values, legend) keep the text colour, never the series colour, to keep their contrast.

API

Properties

PropertyTypeDefaultDescription
formatNumberFormatOptions—Mise en forme des valeurs (Intl.NumberFormat) : { style: 'currency', currency: 'EUR' }, { style: 'percent' }…
hideLabelbooleanfalseMasque visuellement le titre, quand la carte qui contient le graphique l'affiche déjà.
label (obligatoire)string—Titre du graphique, aussi son nom pour les lecteurs d'écran (obligatoire).
labelsstring[][]Les catégories, dans l'ordre : mois, produits…
seriesChartSeries[][]Les séries : { name, values }, une valeur par catégorie ; 8 au plus. Le donut n'utilise que la première.
type"area" | "bar" | "column" | "donut" | "line"'column'column (colonnes, évolution), bar (barres horizontales, classement), line, area (courbe et surface) ou donut (parts d'un tout).

CSS parts

To style it from outside: cr-chart::part(name) { … }

PartDescription
dataLe tableau des données.
plotLa zone du dessin.

On this page