Skip to content

Bar chart race ​

Bars that overtake one another year after year. Good for how a ranking changed over time.

The smallest code that works. change the code, then press Run

In your framework

import { Chart } from 'nervure/react';

const spec = {
  type: 'barrace',
  top: 6,
  steps: ['2017', '2018', '2019', '2020', '2021', '2022', '2023', '2024', '2025', '2026'],
  series: [
    { name: 'Kairouan', values: [19.2, 36, 21.2, 26.6, 11.1, 9.5, 2, 2, 11.1, 2] },
    { name: 'Gabes', values: [41.2, 40.5, 53, 43.5, 40.9, 43.1, 57.3, 48.9, 52.1, 48.2] },
    { name: 'Sousse', values: [31.8, 25.7, 25.2, 42, 55.7, 51.7, 40.5, 50, 47.3, 47.5] },
    { name: 'Mahdia', values: [36.4, 54.7, 50.9, 42.8, 35.2, 20.5, 14.2, 2, 2, 10.4] },
    { name: 'Djerba', values: [43.8, 53.2, 40.9, 32, 35.8, 45.8, 53.1, 66.6, 64, 77.1] },
    { name: 'Sfax', values: [48.9, 61.3, 56.6, 51.9, 69, 63.8, 74.3, 80.9, 95, 84] },
    { name: 'Bizerte', values: [37.7, 41.6, 58.8, 69.7, 58.8, 70.1, 71.7, 64.7, 72.8, 72.6] },
    { name: 'Tunis', values: [48.1, 45.8, 31.2, 25.1, 41.3, 52.1, 50.7, 61.9, 63.2, 49.9] },
    { name: 'Nabeul', values: [28.6, 30, 31.8, 36.3, 31.6, 34.4, 37.4, 46.6, 62.8, 58.3] },
  ],
};

export default function Example() {
  return <Chart spec={spec} style={{ height: 340 }} />;
}

Or import this chart alone: what each import weighs.

Options

Beside the fields shared by every chart (title, format, heading, export…), type barrace takes:

/** Bars that overtake one another, step after step. */
interface BarRaceSpec extends Base {
  type: 'barrace';
  /** Name of each step: years, rounds… */
  steps: string[];
  series: { name: string; values: number[]; image?: string; color?: string }[];
  /** How many bars: a top 5, a top 10. */
  top?: number;
  /** Milliseconds for each step. */
  pace?: number;
}

Style it

  • Colours: theme.palette, chart.setColor(name, colour), or --nervure-palette in your CSS. More
  • Marks: [data-series="name"], parts: [data-part="axes"], legend, series. What you change is kept in exports.
  • Movement: animation: false draws at once. More
  • Files: chart.download('png'), svg, csv. More

Close to it