DevExtreme v26.1 is now available.

Explore our newest features/capabilities and share your thoughts with us.

Your search did not match any results.

Angular Charts - Load Data On Demand

In this demo, the range area Chart loads data as you pan it. Once the component loads a data block, this data stays in memory to reduce requests to the server.

www.kaggle.com
Backend API
<dx-chart id="chart" title="Temperature in Toronto (2025)" [dataSource]="chartDataSource" [zoomAndPan]="{ argumentAxis: 'pan' }" > <dxo-chart-scroll-bar [visible]="true"></dxo-chart-scroll-bar> <dxo-chart-argument-axis argumentType="datetime" [(visualRange)]="currentVisualRange" [wholeRange]="bounds" > </dxo-chart-argument-axis> <dxi-chart-value-axis name="temperature" [allowDecimals]="false"> <dxo-chart-title text="Temperature, &deg;C"> <dxo-chart-font color="#ff950c"></dxo-chart-font> </dxo-chart-title> <dxo-chart-label> <dxo-chart-font color="#ff950c"></dxo-chart-font> </dxo-chart-label> </dxi-chart-value-axis> <dxi-chart-series color="#ff950c" type="rangeArea" argumentField="date" rangeValue1Field="minTemp" rangeValue2Field="maxTemp" name="Temperature range" > </dxi-chart-series> <dxo-chart-animation [enabled]="false"></dxo-chart-animation> <dxo-chart-loading-indicator backgroundColor="none"> <dxo-chart-font [size]="14"></dxo-chart-font> </dxo-chart-loading-indicator> <dxo-chart-legend [visible]="false"></dxo-chart-legend> </dx-chart>
import { bootstrapApplication } from '@angular/platform-browser'; import { Component, ViewChild, enableProdMode, provideZoneChangeDetection } from '@angular/core'; import { HttpClient, provideHttpClient, withFetch } from '@angular/common/http'; import { lastValueFrom } from 'rxjs'; import { DxChartComponent, DxChartModule } from 'devextreme-angular'; import { VisualRange } from 'devextreme-angular/common/charts'; import { DataSource } from 'devextreme-angular/common/data'; if (!/localhost/.test(document.location.host)) { enableProdMode(); } @Component({ selector: 'demo-app', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], imports: [ DxChartModule, ], }) export class AppComponent { @ViewChild(DxChartComponent, { static: false }) component: DxChartComponent; private _visualRange: VisualRange = { startValue: new Date(2025, 3, 1), length: { weeks: 2, }, }; HALFDAY = 43200000; packetsLock = 0; chartDataSource = new DataSource({ store: [], sort: 'date', paginate: false, }); bounds = { startValue: new Date(2025, 0, 1), endValue: new Date(2025, 11, 31), }; constructor(private httpClient: HttpClient) {} get currentVisualRange(): VisualRange { return this._visualRange; } set currentVisualRange(range: VisualRange) { this._visualRange.startValue = range.startValue; this._visualRange.endValue = range.endValue; this.onVisualRangeChanged(); } onVisualRangeChanged() { const items = this.component.instance.getDataSource().items(); const itemsFirstDate = items?.[0]?.date as number; const itemsLastDate = items?.[items.length - 1]?.date as number; const startDate = this._visualRange.startValue as number; const endDate = this._visualRange.endValue as number; if (!items.length || itemsFirstDate - startDate >= this.HALFDAY || endDate - itemsLastDate >= this.HALFDAY) { this.uploadDataByVisualRange(); } } uploadDataByVisualRange() { const dataSource = this.component.instance.getDataSource(); const ajaxArgs = { startVisible: this.getDateString(this._visualRange.startValue as Date), endVisible: this.getDateString(this._visualRange.endValue as Date), }; if (!this.packetsLock) { this.packetsLock += 1; this.component.instance.showLoadingIndicator(); this.getDataFrame(ajaxArgs) .then((dataFrame: Record<string, number | Date>[]) => { this.packetsLock -= 1; const componentStorage = dataSource.store(); dataFrame .map((i) => ({ date: new Date(i.Date), minTemp: i.MinTemp, maxTemp: i.MaxTemp, })) .forEach((item) => componentStorage.insert(item)); dataSource.reload(); this.onVisualRangeChanged(); }) .catch(() => { this.packetsLock -= 1; dataSource.reload(); }); } } getDataFrame(args: Record<string, string>) { const params = `startVisible=${args.startVisible}` + `&endVisible=${args.endVisible}`; return lastValueFrom( this.httpClient.get(`https://js.devexpress.com/Demos/NetCore/api/TemperatureData?${params}`), ); } getDateString(dateTime: Date) { return dateTime ? dateTime.toLocaleDateString('en-US') : ''; } } bootstrapApplication(AppComponent, { providers: [ provideZoneChangeDetection({ eventCoalescing: true, runCoalescing: true }), provideHttpClient(withFetch()), ], });
::ng-deep #chart { height: 440px; }
<!DOCTYPE html> <html lang="en"> <head> <title>DevExtreme Demo</title> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0" /> <link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/26.1.5/css/dx.light.css" /> </head> <body class="dx-viewport"> <div class="demo-container"> <demo-app>Loading...</demo-app> </div> <script src="https://cdn.jsdelivr.net/npm/zone.js@0.16.2/bundles/zone.umd.js"></script> <script src="./bundle.js" type="module"></script> </body> </html>

To implement this functionality, do the following:

  1. Configure a data source.

  2. Configure the Chart to support on-demand data loading.

  3. Implement functions that load the data.

    • In this demo, Chart displays daily information. If a user pan the Chart left or right more than half a day, additional data starts to load. The onVisualRangeChanged initiates this procedure.

    • The uploadDataByVisualRange function analyzes the starting and ending points of the visual range and the bounds of already loaded data. If necessary, it calls the getDataFrame function to obtain new data points. Finally, it reloads the DataSource and saves the current visual range.

    • The getDataFrame Ajax request gets the new data frame from the server.