Angular 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.

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

app.component.html
app.component.ts
app.module.ts
  • <dx-chart id="chart"
  • <!-- Configuration goes here -->
  • >
  • </dx-chart>
  • import { Component } from '@angular/core';
  •  
  • @Component({
  • selector: 'app-root',
  • templateUrl: './app.component.html',
  • styleUrls: ['./app.component.css']
  • })
  • export class AppComponent {
  •  
  • }
  • import { BrowserModule } from '@angular/platform-browser';
  • import { NgModule } from '@angular/core';
  • import { AppComponent } from './app.component';
  •  
  • import { DxChartModule } from 'devextreme-angular';
  •  
  • @NgModule({
  • declarations: [
  • AppComponent
  • ],
  • imports: [
  • BrowserModule,
  • DxChartModule
  • ],
  • providers: [ ],
  • bootstrap: [AppComponent]
  • })
  • export class AppModule { }

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.component.html
app.component.ts
  • <dx-chart [dataSource]="chartData" >
  • <dxi-chart-series
  • argumentField="month"
  • valueField="targetSpend"
  • ></dxi-chart-series>
  • </dx-chart>
  • export class AppComponent {
  • 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.

app.component.html
  • <dx-chart [dataSource]="chartData" >
  • <dxi-chart-series
  • type="bar"
  • argumentField="month"
  • valueField="targetSpend"
  • name="Budget"
  • color="#5996ff"
  • ></dxi-chart-series>
  • </dx-chart>

Specify Multiple Series

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

app.component.html
app.component.ts
  • <dx-chart [dataSource]="chartData" >
  • <dxi-chart-series
  • type="bar"
  • argumentField="month"
  • valueField="targetSpend"
  • name="Budget"
  • color="#5996ff"
  • ></dxi-chart-series>
  • <dxi-chart-series
  • type="spline"
  • argumentField="month"
  • valueField="actualSpend"
  • name="Amount Spent"
  • color="#cb4bfa"
  • ></dxi-chart-series>
  • </dx-chart>
  • export class AppComponent {
  • 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):

app.component.html
app.component.ts
  • <dx-chart [dataSource]="chartData" >
  • <dxo-chart-tooltip
  • [enabled]="true"
  • [customizeTooltip]="customizeTooltip"
  • ></dxo-chart-tooltip>
  • </dx-chart>
  • import { formatNumber } from 'devextreme/localization';
  •  
  • export class AppComponent {
  • averageSpend = this.calculateAverageSpend();
  •  
  • calculateAverageSpend(): number {
  • const sum = this.chartData.reduce((accumulator, { actualSpend }) => accumulator + actualSpend, 0);
  •  
  • return sum / this.chartData.length;
  • }
  •  
  • customizeTooltip: (data: { value: number; seriesName: string }) => { text: string } = (data: { value: number; seriesName: string }) => {
  • if (data.seriesName === 'Budget') {
  • return { text: formatNumber(data.value, 'currency') };
  • }
  • const isValueAboveAverage = data.value > this.averageSpend;
  • if (isValueAboveAverage) {
  • return { text: `${formatNumber(data.value, 'currency')}\n${formatNumber(data.value - this.averageSpend, 'currency')} above average spending.` };
  • }
  •  
  • return { text: `${formatNumber(data.value, 'currency')}\n${formatNumber(this.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:

app.component.html
app.component.ts
  • <dx-chart [dataSource]="chartData" >
  • <dxo-chart-common-pane-settings>
  • <dxo-chart-background-color
  • base="#ffffff"
  • [fillId]="chartGradient"
  • ></dxo-chart-background-color>
  • </dxo-chart-common-pane-settings>
  • </dx-chart>
  • import { registerGradient } from 'devextreme-angular/common/charts';
  •  
  • export class AppComponent {
  • chartGradient = 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):

app.component.html
  • <dx-chart [dataSource]="chartData">
  • <dxi-chart-value-axis>
  • <dxi-chart-constant-line [value]="averageSpend">
  • <dxo-chart-label text="Yearly Spend Average"></dxo-chart-label>
  • </dxi-chart-constant-line>
  • </dxi-chart-value-axis>
  • </dx-chart>