React Sparklines is a React component library for creating small, inline sparkline charts. Sparklines are word-sized graphics that can be embedded in text, tables, or dashboards to show data trends at a glance. This guide walks through advanced usage of React Sparklines, including custom styling, interactive features, and dashboard implementations.
Live demos and docs: borisyankov.github.io/react-sparklines/
Prerequisites
Before you begin, make sure you have:
- Node.js version 14.0 or higher installed
- npm, yarn, or pnpm package manager
- A React project (version 16.8 or higher) or create-react-app setup
- Basic knowledge of React hooks (useState, useEffect)
- Familiarity with JavaScript/TypeScript
- Understanding of data visualization concepts
Installation
Install React Sparklines using your preferred package manager:
npm install react-sparklines
Or with yarn:
yarn add react-sparklines
Or with pnpm:
pnpm add react-sparklines
After installation, your package.json should include:
{
"dependencies": {
"react-sparklines": "^1.7.0",
"react": "^18.0.0",
"react-dom": "^18.0.0"
}
}
Project Setup
React Sparklines requires minimal setup. Import the components and you're ready to use them.
First Example / Basic Usage
Let's create a simple sparkline. Create a new file src/SparklineExample.jsx:
// src/SparklineExample.jsx
import React from 'react';
import { Sparklines, SparklinesLine } from 'react-sparklines';
function SparklineExample() {
const data = [35, 28, 34, 32, 40, 32, 38, 35, 42, 38, 40, 45];
return (
<div style={{ padding: '20px', maxWidth: '800px', margin: '0 auto' }}>
<h2>Basic Sparkline Example</h2>
<Sparklines data={data} width={200} height={50}>
<SparklinesLine color="#1f77b4" />
</Sparklines>
</div>
);
}
export default SparklineExample;
Update your App.jsx:
// src/App.jsx
import React from 'react';
import SparklineExample from './SparklineExample';
import './App.css';
function App() {
return (
<div className="App">
<SparklineExample />
</div>
);
}
export default App;
This creates a basic sparkline chart displaying data trends.
Understanding the Basics
React Sparklines provides several components:
- Sparklines: Main container component
- SparklinesLine: Line chart sparkline
- SparklinesBars: Bar chart sparkline
- SparklinesSpots: Highlight specific points
- SparklinesReferenceLine: Add reference lines
- SparklinesNormalBand: Show normal range
Key concepts for advanced usage:
- Data array: Array of numeric values
- Width and height: Chart dimensions
- Color customization: Customize line and fill colors
- Interactive features: Tooltips and hover effects
- Multiple series: Combine multiple sparklines
Here's an example with multiple sparkline types:
// src/AdvancedSparklineExample.jsx
import React from 'react';
import {
Sparklines,
SparklinesLine,
SparklinesBars,
SparklinesSpots,
SparklinesReferenceLine
} from 'react-sparklines';
function AdvancedSparklineExample() {
const data1 = [35, 28, 34, 32, 40, 32, 38, 35, 42, 38];
const data2 = [120, 132, 101, 134, 90, 110, 125, 115, 130, 120];
const data3 = [20, 25, 18, 22, 28, 24, 26, 23, 27, 25];
return (
<div style={{ padding: '20px', maxWidth: '1200px', margin: '0 auto' }}>
<h2>Advanced Sparkline Example</h2>
<div style={{ display: 'flex', flexDirection: 'column', gap: '30px' }}>
<div>
<h3>Line Sparkline with Spots</h3>
<Sparklines data={data1} width={300} height={60}>
<SparklinesLine color="#1f77b4" />
<SparklinesSpots />
</Sparklines>
</div>
<div>
<h3>Bar Sparkline</h3>
<Sparklines data={data2} width={300} height={60}>
<SparklinesBars color="#ff7f0e" />
</Sparklines>
</div>
<div>
<h3>Line with Reference Line</h3>
<Sparklines data={data3} width={300} height={60}>
<SparklinesLine color="#2ca02c" />
<SparklinesReferenceLine type="mean" />
</Sparklines>
</div>
</div>
</div>
);
}
export default AdvancedSparklineExample;
Practical Example / Building Something Real
Let's build a comprehensive dashboard with sparklines:
// src/DashboardSparklines.jsx
import React, { useState, useEffect } from 'react';
import {
Sparklines,
SparklinesLine,
SparklinesSpots,
SparklinesReferenceLine
} from 'react-sparklines';
function DashboardSparklines() {
const [salesData, setSalesData] = useState([35, 28, 34, 32, 40, 32]);
const [revenueData, setRevenueData] = useState([12000, 15000, 18000, 20000, 22000, 25000]);
const [usersData, setUsersData] = useState([1200, 1350, 1500, 1650, 1800, 1950]);
useEffect(() => {
const interval = setInterval(() => {
setSalesData(prev => [...prev.slice(1), Math.floor(Math.random() * 50) + 20]);
setRevenueData(prev => [...prev.slice(1), Math.floor(Math.random() * 10000) + 15000]);
setUsersData(prev => [...prev.slice(1), Math.floor(Math.random() * 1000) + 1000]);
}, 2000);
return () => clearInterval(interval);
}, []);
const metrics = [
{
title: 'Sales',
value: salesData[salesData.length - 1],
data: salesData,
color: '#1f77b4',
unit: 'units'
},
{
title: 'Revenue',
value: revenueData[revenueData.length - 1],
data: revenueData,
color: '#ff7f0e',
unit: '$'
},
{
title: 'Users',
value: usersData[usersData.length - 1],
data: usersData,
color: '#2ca02c',
unit: ''
}
];
return (
<div style={{ padding: '20px', maxWidth: '1200px', margin: '0 auto' }}>
<h1>Real-time Metrics Dashboard</h1>
<div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: '20px' }}>
{metrics.map((metric, index) => (
<div
key={index}
style={{
padding: '20px',
backgroundColor: '#f8f9fa',
border: '1px solid #ddd',
borderRadius: '8px'
}}
>
<h3 style={{ margin: '0 0 10px 0' }}>{metric.title}</h3>
<div style={{ fontSize: '32px', fontWeight: 'bold', marginBottom: '10px' }}>
{metric.unit}{metric.value.toLocaleString()}
</div>
<Sparklines data={metric.data} width={200} height={60}>
<SparklinesLine color={metric.color} />
<SparklinesSpots />
<SparklinesReferenceLine type="mean" />
</Sparklines>
</div>
))}
</div>
</div>
);
}
export default DashboardSparklines;
Now create a table with inline sparklines:
// src/TableWithSparklines.jsx
import React from 'react';
import {
Sparklines,
SparklinesLine,
SparklinesSpots
} from 'react-sparklines';
function TableWithSparklines() {
const products = [
{
name: 'Product A',
sales: 1200,
trend: [100, 110, 105, 115, 120, 125, 130, 128, 132, 130]
},
{
name: 'Product B',
sales: 980,
trend: [90, 95, 100, 98, 102, 105, 108, 110, 112, 110]
},
{
name: 'Product C',
sales: 1500,
trend: [140, 145, 150, 148, 152, 155, 158, 160, 162, 160]
},
{
name: 'Product D',
sales: 850,
trend: [80, 85, 90, 88, 92, 95, 98, 100, 102, 100]
}
];
return (
<div style={{ padding: '20px', maxWidth: '1000px', margin: '0 auto' }}>
<h2>Product Sales with Trends</h2>
<table style={{ width: '100%', borderCollapse: 'collapse' }}>
<thead>
<tr style={{ borderBottom: '2px solid #ddd' }}>
<th style={{ padding: '12px', textAlign: 'left' }}>Product</th>
<th style={{ padding: '12px', textAlign: 'right' }}>Sales</th>
<th style={{ padding: '12px', textAlign: 'center' }}>Trend</th>
</tr>
</thead>
<tbody>
{products.map((product, index) => (
<tr key={index} style={{ borderBottom: '1px solid #ddd' }}>
<td style={{ padding: '12px' }}>{product.name}</td>
<td style={{ padding: '12px', textAlign: 'right' }}>
${product.sales.toLocaleString()}
</td>
<td style={{ padding: '12px', textAlign: 'center' }}>
<Sparklines data={product.trend} width={150} height={40}>
<SparklinesLine color="#1f77b4" />
<SparklinesSpots />
</Sparklines>
</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
export default TableWithSparklines;
Update your App.jsx:
// src/App.jsx
import React from 'react';
import DashboardSparklines from './DashboardSparklines';
import TableWithSparklines from './TableWithSparklines';
import './App.css';
function App() {
return (
<div className="App">
<DashboardSparklines />
<TableWithSparklines />
</div>
);
}
export default App;
This example demonstrates:
- Real-time updating sparklines
- Dashboard metrics
- Table integration
- Multiple sparkline types
- Custom styling
- Interactive features
Common Issues / Troubleshooting
Sparkline not displaying: Make sure you're importing components correctly from
'react-sparklines'. Check that the data array contains numeric values.Data format error: Ensure data is an array of numbers. Non-numeric values will cause rendering issues.
Styling issues: Customize colors through component props. Use
colorprop for line and bar colors.Size not working: Set
widthandheightprops on theSparklinescomponent. These control the chart dimensions.Spots not showing: Use
SparklinesSpotscomponent insideSparklines. Spots highlight min/max values by default.Performance: For frequently updating sparklines, consider data windowing (keeping only recent N values) to maintain performance.
Next Steps
Now that you have an advanced understanding of React Sparklines:
- Explore all available components
- Learn about advanced styling options
- Implement custom tooltips
- Create animated sparklines
- Integrate with data APIs
- Learn about other mini chart libraries
- Check the official repository: https://github.com/borisyankov/react-sparklines
Summary
You've successfully integrated React Sparklines into your React application with advanced features including real-time dashboards, table integrations, and interactive mini charts. React Sparklines provides a lightweight solution for creating word-sized graphics that show data trends at a glance.
SEO Keywords
react-sparklines
React Sparklines
react-sparklines tutorial
React mini charts
react-sparklines installation
React inline charts
react-sparklines example
React sparkline component
react-sparklines setup
React data trends
react-sparklines customization
React dashboard sparklines
react-sparklines table
React mini visualization
react-sparklines getting started
Top comments (0)