React Chart - Getting Started

NOTE
Before you start the tutorial, ensure DevExtreme is installed in your application.

The DevExtreme Chart is an interactive UI component that visualizes data using multiple series types: lines, bars, bubbles, candlesticks, and more.

This tutorial explains how to add a Chart to a page and configure component core settings.

Embedded applications are not available for the selected DevExtreme version (v25.1).
We recommend that you upgrade to the most recent version of DevExtreme to access the latest features, bug fixes, and security updates.

Each section in this tutorial covers a single configuration step. You can find the complete source code in the following GitHub repository:

View on GitHub

Create a Chart

Add DevExtreme to your React application and use the following code snippet to create a Chart:

App.tsx
  • import React from 'react';
  • import 'devextreme/dist/css/dx.fluent.blue.light.css';
  • import Chart from 'devextreme-react/chart';
  •  
  • function App() {
  • return (
  • <Chart id="chart">
  • {/* Configuration goes here */}
  • </Chart>
  • );
  • }
  •  
  • export default App;

Display Chart Data

To bind a DevExtreme Chart to data, specify the dataSource property. Ensure that the data source contains items with at least two fields: an argument and a value. The Chart looks for fields named arg and val. If field names are different, assign these names to series.argumentField and series.valueField properties:

App.tsx
  • const chartData = [
  • { month: 'January', targetSpend: 12000 },
  • { month: 'February', targetSpend: 15500 },
  • { month: 'March', targetSpend: 13500 },
  • { month: 'April', targetSpend: 16000 },
  • { month: 'May', targetSpend: 17000 },
  • { month: 'June', targetSpend: 20000 },
  • { month: 'July', targetSpend: 14500 },
  • { month: 'August', targetSpend: 21000 },
  • { month: 'September', targetSpend: 19000 },
  • { month: 'October', targetSpend: 22000 },
  • { month: 'November', targetSpend: 16000 },
  • { month: 'December', targetSpend: 25000 },
  • ];
  •  
  • function App(): JSX.Element {
  • return (
  • <>
  • <Chart dataSource={chartData} >
  • <Series
  • argumentField='month'
  • valueField='targetSpend'
  • />
  • </Chart>
  • </>
  • );
  • }
  •  
  • export default App;

Configure Chart Series

To configure how a Chart displays data, specify series object properties. This example implements the following properties: type, name, and color.

App.tsx
  • function App(): JSX.Element {
  • return (
  • <>
  • <Chart dataSource={chartData} >
  • <Series
  • type='bar'
  • argumentField='month'
  • valueField='targetSpend'
  • name='Budget'
  • color='#5996ff'
  • />
  • </Chart>
  • </>
  • );
  • }
  •  
  • export default App;

Specify Multiple Series

To configure Chart with multiple series, update your data source with additional value fields and specify additional <Series> selectors. This example configures two series to display a spline and a bar chart:

App.tsx
  • const chartData = [
  • { month: 'January', targetSpend: 12000, actualSpend: 9500 },
  • { month: 'February', targetSpend: 15500, actualSpend: 16500 },
  • { month: 'March', targetSpend: 13500, actualSpend: 12000 },
  • { month: 'April', targetSpend: 16000, actualSpend: 14000 },
  • { month: 'May', targetSpend: 17000, actualSpend: 18500 },
  • { month: 'June', targetSpend: 20000, actualSpend: 17500 },
  • { month: 'July', targetSpend: 14500, actualSpend: 11000 },
  • { month: 'August', targetSpend: 21000, actualSpend: 22500 },
  • { month: 'September', targetSpend: 19000, actualSpend: 18000 },
  • { month: 'October', targetSpend: 22000, actualSpend: 25000 },
  • { month: 'November', targetSpend: 16000, actualSpend: 14500 },
  • { month: 'December', targetSpend: 25000, actualSpend: 27000 },
  • ];
  •  
  • function App(): JSX.Element {
  • return (
  • <>
  • <Chart dataSource={chartData} >
  • <Series
  • type='bar'
  • argumentField='month'
  • valueField='targetSpend'
  • name='Budget'
  • color='#5996ff'
  • />
  • <Series
  • type='spline'
  • argumentField='month'
  • valueField='actualSpend'
  • name='Amount Spent'
  • color='#cb4bfa'
  • />
  • </Chart>
  • </>
  • );
  • }
  •  
  • export default App;

Add and Customize Tooltips

To add tooltips to a Chart, assign true to the tooltip.enabled property. Default tooltips display values when users hover over points. To implement custom tooltips, define the customizeTooltip function. This example uses customizeTooltip to display the difference between current and average actualSpend values (calculated in the calculateAverageSpend() method):

App.tsx
  • import { formatNumber } from 'devextreme/localization';
  •  
  • function calculateAverageSpend(): number {
  • const sum = chartData.reduce((accumulator, { actualSpend }) => accumulator + actualSpend, 0);
  •  
  • return sum / chartData.length;
  • }
  •  
  • const averageSpend = calculateAverageSpend();
  •  
  • function customizeTooltip(data: { value: number; seriesName: string }): { text: string } {
  • if (data.seriesName === 'Budget') {
  • return { text: formatNumber(data.value, 'currency') };
  • }
  • const isValueAboveAverage = data.value > calculateAverageSpend();
  • if (isValueAboveAverage) {
  • return { text: `${formatNumber(data.value, 'currency')}\n${formatNumber(data.value - averageSpend, 'currency')} above average spending.` };
  • }
  •  
  • return { text: `${formatNumber(data.value, 'currency')}\n${formatNumber(averageSpend - data.value, 'currency')} below average spending.` };
  • }
  •  
  • function App(): JSX.Element {
  • return (
  • <>
  • <Chart dataSource={chartData} >
  • <Tooltip
  • enabled={true}
  • customizeTooltip={customizeTooltip}
  • />
  • </Chart>
  • </>
  • );
  • }
  •  
  • export default App;

Specify a Custom Gradient Background

You can customize the appearance of Chart elements. To configure a background gradient, set commonPaneSettings.backgroundColor to a ChartsColor object. In this object, assign the registerGradient return value to fillId:

App.tsx
  • import { registerGradient } from 'devextreme-react/common/charts';
  •  
  • const chartGradient = registerGradient('linear', {
  • colors: [{
  • offset: '20%',
  • color: '#dee4ff',
  • }, {
  • offset: '90%',
  • color: '#ffdeff',
  • }],
  • });
  •  
  • function App(): JSX.Element {
  • return (
  • <>
  • <Chart dataSource={chartData} >
  • <CommonPaneSettings>
  • <BackgroundColor
  • base='#ffffff'
  • fillId={chartGradient}
  • >
  • </BackgroundColor>
  • </CommonPaneSettings>
  • </Chart>
  • </>
  • );
  • }
  •  
  • export default App;

Add a Constant Line

DevExtreme Chart can display horizontal or vertical constant lines as points of reference. The following example uses the valueAxis.constantLines property to display a horizontal constant line that indicates the yearly spending average (averageSpend):

App.tsx
  • function App(): JSX.Element {
  • return (
  • <>
  • <Chart dataSource={chartData} >
  • <ValueAxis>
  • <ConstantLine
  • value={averageSpend}
  • color='#0000c7'
  • >
  • <Label text='Yearly Spend Average' />
  • </ConstantLine>
  • </ValueAxis>
  • </Chart>
  • </>
  • );
  • }
  •  
  • export default App;