How can I help you?
Getting started with React Sparkline component
2 Mar 202616 minutes to read
This section describes the steps to create a simple Sparkline and demonstrates the basic usage of the Sparkline component.
Dependencies
Below is the list of minimum dependencies required to use the Sparkline component.
|-- @syncfusion/ej2-react-charts
|-- @syncfusion/ej2-data
|-- @syncfusion/ej2-svg-base
|-- @syncfusion/ej2-react-base
|-- @syncfusion/ej2-pdf-export
|-- @syncfusion/ej2-file-utils
|-- @syncfusion/ej2-compression
Once you install @syncfusion/ej2-react-charts, the other required dependencies will be installed automatically along with the main package.
Installation and configuration
Create a React application
To easily set up a React application, use the Vite CLI (npm create vite), which provides a faster development environment, smaller bundle sizes, and optimized builds compared to traditional tools like create-react-app. For detailed steps, refer to the Vite installation instructions. Vite sets up your environment using JavaScript and optimizes your application for production.
Note: To create a React application using
create-react-appinstead, refer to this documentation for more details.
To create a new React application, run the following command.
npm create vite@latest my-appThis command will prompt you for a few settings for the new project, such as selecting a framework and a variant.

To set up a React application in TypeScript environment, run the following command.
npm create vite@latest my-app -- --template react-ts
cd my-app
npm run devTo set up a React application in JavaScript environment, run the following command.
npm create vite@latest my-app -- --template react
cd my-app
npm run devInstall Syncfusion® Sparkline package
All the available Essential® JS 2 packages are published in the npmjs.com public registry.
To install the Syncfusion® Sparkline package, use the following command
npm install @syncfusion/ej2-react-charts --saveThe –save will instruct NPM to include the Sparkline package inside of the dependencies section of the package.json.
Add Sparkline to the project
Add the Sparkline component to src/App.tsx using the following code.
import { SparklineComponent } from '@syncfusion/ej2-react-charts';
import * as React from 'react';
function App() {
return (<SparklineComponent></SparklineComponent>);
}
export default App;import { SparklineComponent } from '@syncfusion/ej2-react-charts';
import * as React from 'react';
function App() {
return (<SparklineComponent></SparklineComponent>);
}
export default App;Now run the npm run dev command in the console to start the development server. This command compiles your code and serves the application locally, opening it in the browser.
npm run dev
Since the data source has not been specified to the Sparkline, no shapes will be rendered. Only an empty SVG element is appended to the Sparkline container.
Module injection
Sparkline components are segregated into individual feature-wise modules. In order to use a particular feature, you need to inject its feature service in the Sparkline component. Please find relevant feature service name and description as follows.
-
SparklineTooltip- Inject this module in toservicesto use tooltip feature.
Import the above-mentioned module from the chart package and inject them into the services section of the Sparkline component as follows.
import { SparklineComponent, Inject, SparklineTooltip } from '@syncfusion/ej2-react-charts';
import * as React from "react";
function App() {
return (<SparklineComponent>
<Inject services={[SparklineTooltip]} />
</SparklineComponent>);
}
export default App;import { SparklineComponent, Inject, SparklineTooltip } from '@syncfusion/ej2-react-charts';
import * as React from "react";
function App() {
return (<SparklineComponent>
<Inject services={[SparklineTooltip]} />
</SparklineComponent>);
}
export default App;Bind data source to Sparkline
The dataSource property enables data binding for the Sparkline. It accepts a collection of values as input, such as a list of objects.
import * as React from 'react';
import * as ReactDOM from "react-dom";
import { SparklineComponent } from '@syncfusion/ej2-react-charts';
function App() {
return (<SparklineComponent id='sparkline' height='100px' width='70%' dataSource={[
{ xval: '2005', yval: 20090440 },
{ xval: '2006', yval: 20264080 },
{ xval: '2007', yval: 20434180 },
{ xval: '2008', yval: 21007310 },
{ xval: '2009', yval: 21262640 },
{ xval: '2010', yval: 21515750 },
{ xval: '2011', yval: 21766710 },
{ xval: '2012', yval: 22015580 },
{ xval: '2013', yval: 22262500 },
{ xval: '2014', yval: 22507620 }
]} xName='xval' yName='yval' type='Line'>
</SparklineComponent>);
}
export default App;
const root = ReactDOM.createRoot(document.getElementById('sparkline'));
root.render(<App />);import * as React from 'react';
import * as ReactDOM from "react-dom";
import { SparklineComponent } from '@syncfusion/ej2-react-charts';
function App() {
return (<SparklineComponent id='sparkline' height='100px' width='70%' dataSource={[
{ xval: '2005', yval: 20090440 },
{ xval: '2006', yval: 20264080 },
{ xval: '2007', yval: 20434180 },
{ xval: '2008', yval: 21007310 },
{ xval: '2009', yval: 21262640 },
{ xval: '2010', yval: 21515750 },
{ xval: '2011', yval: 21766710 },
{ xval: '2012', yval: 22015580 },
{ xval: '2013', yval: 22262500 },
{ xval: '2014', yval: 22507620 }
]} xName='xval' yName='yval' type='Line'>
</SparklineComponent>);
}
export default App;
const root = ReactDOM.createRoot(document.getElementById('sparkline'));
root.render(<App />);Change the type of Sparkline
The Sparkline type can be configured using the type property, which supports Line, Column, WinLoss, Pie, and Area. Here, the Area type is applied.
import * as React from 'react';
import * as ReactDOM from "react-dom";
import { SparklineComponent } from '@syncfusion/ej2-react-charts';
function App() {
return (<SparklineComponent id='sparkline' height='100px' width='70%' dataSource={[
{ xval: '2005', yval: 20090440 },
{ xval: '2006', yval: 20264080 },
{ xval: '2007', yval: 20434180 },
{ xval: '2008', yval: 21007310 },
{ xval: '2009', yval: 21262640 },
{ xval: '2010', yval: 21515750 },
{ xval: '2011', yval: 21766710 },
{ xval: '2012', yval: 22015580 },
{ xval: '2013', yval: 22262500 },
{ xval: '2014', yval: 22507620 }
]} xName='xval' yName='yval' type='Area'>
</SparklineComponent>);
}
export default App;
const root = ReactDOM.createRoot(document.getElementById('sparkline'));
root.render(<App />);import * as React from 'react';
import * as ReactDOM from "react-dom";
import { SparklineComponent } from '@syncfusion/ej2-react-charts';
function App() {
return (<SparklineComponent id='sparkline' height='100px' width='70%' dataSource={[
{ xval: '2005', yval: 20090440 },
{ xval: '2006', yval: 20264080 },
{ xval: '2007', yval: 20434180 },
{ xval: '2008', yval: 21007310 },
{ xval: '2009', yval: 21262640 },
{ xval: '2010', yval: 21515750 },
{ xval: '2011', yval: 21766710 },
{ xval: '2012', yval: 22015580 },
{ xval: '2013', yval: 22262500 },
{ xval: '2014', yval: 22507620 }
]} xName='xval' yName='yval' type='Area'>
</SparklineComponent>);
}
export default App;
const root = ReactDOM.createRoot(document.getElementById('sparkline'));
root.render(<App />);Enable tooltip for Sparkline
The Sparkline provides additional information through a tooltip that appears when the mouse pointer hovers over the chart. You can enable tooltip by setting the visible property to true in tooltipSettings and injecting SparklineTooltip module into the services.
import * as React from 'react';
import * as ReactDOM from "react-dom";
import { SparklineComponent, Inject, SparklineTooltip } from '@syncfusion/ej2-react-charts';
function App() {
return (<SparklineComponent id='sparkline' height='100px' width='70%' tooltipSettings={{
visible: true, format: '${xval} : ${yval}'
}} dataSource={[
{ xval: '2005', yval: 20090440 },
{ xval: '2006', yval: 20264080 },
{ xval: '2007', yval: 20434180 },
{ xval: '2008', yval: 21007310 },
{ xval: '2009', yval: 21262640 },
{ xval: '2010', yval: 21515750 },
{ xval: '2011', yval: 21766710 },
{ xval: '2012', yval: 22015580 },
{ xval: '2013', yval: 22262500 },
{ xval: '2014', yval: 22507620 }
]} xName='xval' yName='yval' type='Area'>
<Inject services={[SparklineTooltip]} />
</SparklineComponent>);
}
export default App;
const root = ReactDOM.createRoot(document.getElementById('sparkline'));
root.render(<App />);import * as React from 'react';
import * as ReactDOM from "react-dom";
import { SparklineComponent, Inject, SparklineTooltip } from '@syncfusion/ej2-react-charts';
function App() {
return (<SparklineComponent id='sparkline' height='100px' width='70%' tooltipSettings={{
visible: true, format: '${xval} : ${yval}'
}} dataSource={[
{ xval: '2005', yval: 20090440 },
{ xval: '2006', yval: 20264080 },
{ xval: '2007', yval: 20434180 },
{ xval: '2008', yval: 21007310 },
{ xval: '2009', yval: 21262640 },
{ xval: '2010', yval: 21515750 },
{ xval: '2011', yval: 21766710 },
{ xval: '2012', yval: 22015580 },
{ xval: '2013', yval: 22262500 },
{ xval: '2014', yval: 22507620 }
]} xName='xval' yName='yval' type='Area'>
<Inject services={[SparklineTooltip]} />
</SparklineComponent>);
}
export default App;
const root = ReactDOM.createRoot(document.getElementById('sparkline'));
root.render(<App />);