JavaScript/jQuery 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 jQuery application and use the following code snippet to create a Chart:

index.js
index.html
  • $(function() {
  • $("#chart").dxChart({
  • // Configuration goes here
  • });
  • });
  • <html>
  • <head>
  • <!-- ... -->
  • <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
  •  
  • <link rel="stylesheet" href="https://cdn3.devexpress.com/jslib/25.1.15/css/dx.light.css">
  • <link rel="stylesheet" href="index.css">
  •  
  • <script type="text/javascript" src="https://cdn3.devexpress.com/jslib/25.1.15/js/dx.all.js"></script>
  • <script type="text/javascript" src="index.js"></script>
  • </head>
  • <body class="dx-viewport">
  • <div id="chart"></div>
  • </body>
  • </html>

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:

index.js
  • $(() => {
  • $('#chart').dxChart({
  • dataSource: chartData,
  • series: [{
  • argumentField: 'month',
  • valueField: 'targetSpend',
  • }],
  • });
  • });
  •  
  • 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 },
  • ];

Configure Chart Series

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

index.js
  • $(() => {
  • $('#chart').dxChart({
  • dataSource: chartData,
  • series: [{
  • type: 'bar',
  • argumentField: 'month',
  • valueField: 'targetSpend',
  • name: 'Budget',
  • color: '#5996ff',
  • }],
  • });
  • });

Specify Multiple Series

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

index.js
  • $(() => {
  • $('#chart').dxChart({
  • dataSource: chartData,
  • series: [{
  • type: 'bar',
  • argumentField: 'month',
  • valueField: 'targetSpend',
  • name: 'Budget',
  • color: '#5996ff',
  • }, {
  • type: 'spline',
  • argumentField: 'month',
  • valueField: 'actualSpend',
  • name: 'Amount Spent',
  • color: '#cb4bfa',
  • }],
  • });
  • });
  •  
  • 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 },
  • ];

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):

index.js
  • const formatNumber = DevExpress.localization.formatNumber;
  •  
  • $(() => {
  • $('#chart').dxChart({
  • dataSource: chartData,
  • tooltip: {
  • enabled: true,
  • customizeTooltip,
  • },
  • });
  • });
  •  
  • const averageSpend = calculateAverageSpend();
  •  
  • function calculateAverageSpend() {
  • const sum = chartData.reduce((accumulator, { actualSpend }) => accumulator + actualSpend, 0);
  •  
  • return sum / chartData.length;
  • }
  •  
  • function customizeTooltip(data) {
  • if (data.seriesName === 'Budget') {
  • return { text: formatNumber(data.value, 'currency') };
  • }
  • const isValueAboveAverage = data.value > averageSpend;
  • 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.` };
  • }

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:

index.js
  • const registerGradient = DevExpress.common.charts.registerGradient;
  •  
  • $(() => {
  • $('#chart').dxChart({
  • dataSource: chartData,
  • commonPaneSettings: {
  • backgroundColor: {
  • base: '#ffffff',
  • fillId: registerGradient('linear', {
  • colors: [{
  • offset: '20%',
  • color: '#dee4ff',
  • }, {
  • offset: '90%',
  • color: '#ffdeff',
  • }],
  • }),
  • },
  • },
  • });
  • });

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):

index.js
  • const registerGradient = DevExpress.common.charts.registerGradient;
  •  
  • $(() => {
  • $('#chart').dxChart({
  • dataSource: chartData,
  • valueAxis: [{
  • constantLines: [{
  • value: averageSpend,
  • color: '#0000c7',
  • label: {
  • text: 'Yearly Spend Average',
  • },
  • }],
  • }],
  • });
  • });