React Sparklines (TypeScript Rewrite)

1.3.0 · active · verified Sun Apr 19

The `react-sparklines` package (maintained by acodesmith) is a modern, TypeScript-rewritten version of the original unmaintained `react-sparklines` library. It provides a suite of React components for rendering small, expressive sparklines, including various visual elements like lines, bars, spots, reference lines, and normal bands. As of version 1.3.0, this fork actively uses modern React patterns such as functional components, and ships with comprehensive TypeScript types, offering a smaller, faster, and more actively maintained alternative to its predecessor. Its key differentiators include explicit TypeScript support, adherence to contemporary React development practices, and a focus on performance and bundle size. The release cadence is driven by ongoing modernization and bug fixes, providing a stable, up-to-date solution for compact data visualization in React applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to render a basic sparkline with a line, spots, and a mean reference line using sample data, styled for clarity within a React functional component.

import React from 'react';
import { Sparklines, SparklinesLine, SparklinesSpots, SparklinesReferenceLine } from 'react-sparklines';

// A simple functional component demonstrating sparklines
const MySparklineChart: React.FC = () => {
  const sampleData = [5, 10, 5, 20, 8, 15, 12, 18, 7, 13, 10, 22];
  const meanValue = sampleData.reduce((a, b) => a + b, 0) / sampleData.length;

  return (
    <div style={{ width: '200px', height: '50px', border: '1px solid #ccc', padding: '10px', margin: '20px' }}>
      <h3 style={{ fontSize: '1em', marginBottom: '5px' }}>Sales Trend</h3>
      <Sparklines data={sampleData} width={180} height={40} margin={5}>
        <SparklinesLine color="#1a73e8" style={{ fill: 'none', strokeWidth: 2 }} />
        <SparklinesSpots style={{ fill: '#1a73e8' }} size={2} />
        <SparklinesReferenceLine type="mean" style={{ stroke: 'red', strokeDasharray: '2, 2' }} />
      </Sparklines>
      <p style={{ fontSize: '0.8em', color: '#555', marginTop: '5px' }}>Mean: {meanValue.toFixed(2)}</p>
    </div>
  );
};

export default MySparklineChart;

view raw JSON →