Skip to content

Train graph ​

Stations down the side and time across: one line for each train, steep when it is fast. Good for timetables, and where trains meet or overtake.

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

In your framework

import { Chart } from 'nervure/react';

const spec = {
  type: 'marey',
  stations: [
    { name: 'Tunis', km: 0 },
    { name: 'Hammam-Lif', km: 17 },
    { name: 'Borj Cédria', km: 25 },
    { name: 'Grombalia', km: 40 },
    { name: 'Bou Argoub', km: 50 },
    { name: 'Bir Bou Rekba', km: 60 },
    { name: 'Enfidha', km: 100 },
    { name: 'Kalâa Kebira', km: 128 },
    { name: 'Sousse', km: 142 },
  ],
  trains: [
    { name: 'Express 1', kind: 'Express', times: ['06:05', null, null, null, null, null, '07:09', null, '07:37'] },
    { name: 'Local 11', kind: 'Local', times: ['06:19', '06:38', '06:48', '07:06', '07:18', '07:30', '08:13', '08:44', '09:00'] },
    { name: 'Express 3', kind: 'Express', times: ['07:03', null, null, '07:30', null, null, '08:09', null, '08:36'] },
    { name: 'Local 13', kind: 'Local', times: ['07:37', '07:56', '08:06', '08:24', '08:36', '08:48', '09:31', '10:02', '10:18'] },
    { name: 'Express 2', kind: 'Express', times: ['07:54', null, null, null, null, null, '06:50', null, '06:22'] },
    { name: 'Local 12', kind: 'Local', times: ['08:42', '08:23', '08:13', '07:56', '07:44', '07:32', '06:48', '06:17', '06:01'] },
  ],
};

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 marey takes:

/** Train graph: stations down the side, time across, one line for each train. */
interface MareySpec extends Base {
  type: 'marey';
  /** In order, with their distance from the first one in km. */
  stations: { name: string; km: number }[];
  /** Times at each station, in the order of `stations`: '07:42' or minutes from midnight; null: it does not stop. */
  trains: { name: string; times: (string | number | null)[]; kind?: string; color?: string }[];
  onItemClick?: (name: string) => void;
}

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