Overview of RacingBars library
RacingBars is an open-source, lightweight (~45kb gzipped) JavaScript library based on D3.js used to create bar chart races. It is compatible with JavaScript, TypeScript, React, Vue, and Svelte.
website·Indexed 32 days ago
https://racing-bars.hatemhosny.devA lightweight, open-source JavaScript library for creating highly configurable racing bar charts. It supports long and wide data formats, custom data transformations, and provides official wrapper components for React and Vue 3, as well as first-class TypeScript definitions.
Racing Bars is a lightweight (~45kb gzipped), open-source (MIT License) library for creating animated bar chart races. It is compatible with vanilla JavaScript, TypeScript, React, Vue, and Svelte. Key capabilities include:
TickDetails interface provides metadata about the current state of a chart tick. This object is passed to event callbacks to allow developers to react to the chart's progression, animation state, and date positioning.interface TickDetails {
allDates: string[]; // Array of all unique dates in the dataset, sorted ascending
date: string; // The current date as a 'YYYY-MM-DD' string
isFirstDate: boolean; // true if the current date is the first date in the dataset
isLastDate: boolean; // true if the current date is the last date in the dataset
isRunning: boolean; // true if the chart is currently animating
}showGroups is enabled, all bars within the same group share the same color. Note that if the dataset contains a very large number of names or groups, colors may become visually similar.const data: Data[] = [
{ date: '2017-01-01', name: 'Egypt', value: 96.44 },
{ date: '2017-01-01', name: 'Singapore', value: 5.61 },
];ApiCallback type defines the signature for callback functions used as chart event listeners. It accepts a single argument, tickDetails (of type TickDetails), which provides information about the current tick state of the chart. The function returns void.type ApiCallback = (tickDetails: TickDetails) => void;title, subTitle, DateCounter, and caption fields based on the current dataset and the date being displayed. This is achieved by utilizing the data function to inject values that change as the animation progresses.racing-bars package via npm to use it within a project that utilizes a module bundler. Import the race function to initialize the racing bars component by providing the data source, the target element selector, and an options object.npm install racing-barsimport { race } from "racing-bars";
race("/data.json", "#race", { /* options */ });fillDateGapsInterval to define the time interval and fillDateGapValue to specify how to fill the missing values (e.g., using 'interpolate').const options = {
fillDateGapsInterval: 'month',
fillDateGapsValue: 'interpolate',
valueDecimals: 2,
};loadData function to fetch the data first, transform it using standard JavaScript methods, and then pass the resulting array directly to the race function instead of a URL.import { loadData, race } from 'racing-bars';
const data = await loadData('/data/population.csv', 'csv');
const transformedData = data.map((d) => ({
...d,
icon: `https://flagsapi.com/${d.code}/flat/64.png`,
}));
const options = {
title: 'World Population',
};
race(transformedData, '#race', options);Options type to ensure your configuration object is type-safe.import { race, type Options } from 'racing-bars';
const options: Options = {
dataType: 'csv',
title: 'World Population',
};
race('/data/population.csv', '#race', options);