Example shows Angular Live / Dynamic Line Chart that updates every 1 second. They are also referred to as Realtime Line Charts.
/* app.component.ts */ import { Component, OnDestroy } from '@angular/core'; import { CommonModule } from '@angular/common'; import { RouterOutlet } from '@angular/router'; import { HttpClientModule, HttpClient } from '@angular/common/http'; import { CanvasJSAngularChartsModule } from '@canvasjs/angular-charts'; @Component({ selector: 'app-root', standalone: true, imports: [CommonModule, RouterOutlet, HttpClientModule, CanvasJSAngularChartsModule], templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnDestroy { constructor(private http : HttpClient) { } dataPoints:any[] = []; timeout:any = null; xValue:number = 1; yValue:number = 10; newDataCount:number = 10; chart: any; chartOptions = { theme: "light2", title: { text: "Live Data" }, data: [{ type: "line", dataPoints: this.dataPoints }] } getChartInstance(chart: object) { this.chart = chart; this.updateData(); } ngOnDestroy() { clearTimeout(this.timeout); } updateData = () => { this.http.get("https://canvasjs.com/services/data/datapoints.php?xstart="+this.xValue+"&ystart="+this.yValue+"&length="+this.newDataCount+"type=json", { responseType: 'json' }).subscribe(this.addData); } addData = (data:any) => { if(this.newDataCount != 1) { data.forEach( (val:any[]) => { this.dataPoints.push({x: val[0], y: parseInt(val[1])}); this.xValue++; this.yValue = parseInt(val[1]); }) } else { //this.dataPoints.shift(); this.dataPoints.push({x: data[0][0], y: parseInt(data[0][1])}); this.xValue++; this.yValue = parseInt(data[0][1]); } this.newDataCount = 1; this.chart.render(); this.timeout = setTimeout(this.updateData, 1000); } }
<div> <canvasjs-chart [options]="chartOptions" [styles]="{width: '100%', height: '360px'}" (chartInstance)="getChartInstance($event)"></canvasjs-chart> </div>
The thickness and the dash-type of the line can be modified using lineThickness and lineDashType properties. Other commonly used customization options include markerColor, lineColor, etc.
Note For step by step instructions, follow our Angular Integration Tutorial