React Sparklines Component
react-sparklines is a lightweight React component library designed for rendering small, expressive sparkline charts using SVG. It offers a declarative API to create various types of micro-charts, including lines, bars, and spots, along with statistical overlays like reference lines for mean, median, and normal bands. The current stable version is 1.7.0, with its last publication dating back several years, suggesting the library is in a maintenance phase rather than active development. Its primary appeal lies in providing simple, customizable visualizations for dashboards or other data-dense user interfaces where the emphasis is on quickly conveying trends without the complexity and overhead of full-featured charting libraries. It distinguishes itself by focusing purely on compact, trend-oriented data representations.
Common errors
-
Error: Objects are not valid as a React child (found: object with keys {x, y, value}). If you meant to render a collection of children, use an array instead.cause The `data` prop for Sparklines expects an array of numbers, not an array of objects or an object.fixEnsure the `data` prop is an array of primitive number values, e.g., `data={[1, 2, 3, 4]}`. -
TypeError: Cannot read properties of undefined (reading 'map') or 'map' is not a function
cause The `data` prop was either not provided or was provided with a non-array value (e.g., `null`, `undefined`, or an object).fixAlways provide an array of numbers to the `data` prop of the `Sparklines` component, e.g., `<Sparklines data={[10, 20, 15]} />`.
Warnings
- gotcha The package react-sparklines has not been updated in several years (last published 7 years ago for v1.7.0). This may lead to compatibility issues with newer versions of React (e.g., hooks, concurrent mode, strict mode) or modern development toolchains. It also implies a lack of ongoing security updates or feature enhancements.
- gotcha The `width` and `height` props define the SVG viewbox dimensions, not necessarily the rendered size. By default, the component is responsive and will scale to fit its parent container. To set absolute pixel dimensions, `svgWidth` and `svgHeight` should be used instead.
Install
-
npm install react-sparklines -
yarn add react-sparklines -
pnpm add react-sparklines
Imports
- Sparklines
import Sparklines from 'react-sparklines'; const { Sparklines } = require('react-sparklines');import { Sparklines } from 'react-sparklines'; - SparklinesLine
import SparklinesLine from 'react-sparklines/SparklinesLine';
import { Sparklines, SparklinesLine } from 'react-sparklines'; - SparklinesReferenceLine
const SparklinesReferenceLine = require('react-sparklines').SparklinesReferenceLine;import { SparklinesReferenceLine } from 'react-sparklines';
Quickstart
import React from 'react';
import ReactDOM from 'react-dom';
import { Sparklines, SparklinesLine, SparklinesSpots, SparklinesReferenceLine } from 'react-sparklines';
const sampleData = [5, 10, 5, 20, 8, 15, 25, 12, 18, 7, 30, 22];
function App() {
return (
<div>
<h2>Basic Sparkline</h2>
<Sparklines data={[5, 10, 5, 20]} width={100} height={20} margin={5}>
<SparklinesLine color="blue" />
</Sparklines>
<h2>Sparkline with Spots and Reference Line</h2>
<Sparklines data={sampleData} width={120} height={30} margin={5}>
<SparklinesLine style={{ fill: 'none', strokeWidth: 2 }} />
<SparklinesSpots size={2} style={{ fill: '#ff7f0e' }} />
<SparklinesReferenceLine type="mean" style={{ stroke: 'red', strokeDasharray: '2,2' }} />
</Sparklines>
</div>
);
}
ReactDOM.render(<App />, document.getElementById('root'));