Example shows Angular Line Chart with Data being read from JSON using AJAX (http.get).
/* app.component.ts */
import { Component, AfterViewInit } 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',
templateUrl: './app.component.html',
standalone: true,
imports: [CommonModule, RouterOutlet, HttpClientModule, CanvasJSAngularChartsModule]
})
export class AppComponent implements AfterViewInit {
dataPoints:any = [];
chart:any;
constructor(private http : HttpClient) {
}
chartOptions = {
theme: "light2",
zoomEnabled: true,
exportEnabled: true,
title: {
text:"Bitcoin Closing Price"
},
subtitles: [{
text: "Loading Data...",
fontSize: 24,
horizontalAlign: "center",
verticalAlign: "center",
dockInsidePlotArea: true
}],
axisY: {
title: "Closing Price (in USD)",
prefix: "$"
},
data: [{
type: "line",
name: "Closing Price",
yValueFormatString: "$#,###.00",
xValueType: "dateTime",
dataPoints: this.dataPoints
}]
}
getChartInstance(chart: object) {
this.chart = chart;
}
ngAfterViewInit() {
this.http.get('https://canvasjs.com/data/gallery/angular/btcusd2021.json', { responseType: 'json' }).subscribe((response: any) => {
let data = response;
for(let i = 0; i < data.length; i++){
this.dataPoints.push({x: new Date(data[i].date), y: Number(data[i].close) });
}
this.chart.subtitles[0].remove();
});
}
}
prefix property of the axis can be used to set prefixes in the axis labels. Zooming & Panning can be enabled by setting zoomEnabled property. Some other customizations include toolbar, risingColor, fallingColor etc.
Note For step by step instructions, follow our Angular Integration Tutorial