In this article, we are going to learn about highcharts. In order to add highcharts, we will use the step by step process, which is described as follows:
Step 1 :
At first, we are going create new app. the following command will be used
ng new HighchartsExample
Step 2 :
In this step, we are going to Install Npm Package using command
npm install highcharts –save
npm install highcharts-angular –save
Step 3 :
In app.component.ts
import { AfterViewInit, Component} from '@angular/core'; import * as Highcharts from 'highcharts'; import HighchartsMore from 'highcharts/highcharts-more'; import HighchartsSolidGauge from 'highcharts/modules/solid-gauge'; HighchartsMore(Highcharts); HighchartsSolidGauge(Highcharts); @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'] }) export class AppComponentimplements AfterViewInit { public ngAfterViewInit(): void { this.GaugeChart(); this.PieChart(); this.ColumnChart(); this.LineChart(); } private getRandomNumber(min: number, max: number): number { return Math.floor(Math.random() * (max - min + 1) + min) } private GaugeChart(): void { const chart = Highcharts.chart('chart-gauge', { chart: { type: 'solidgauge', }, title: { text: 'Gauge Chart', }, credits: { enabled: false, }, pane: { startAngle: -90, endAngle: 90, center: ['50%', '85%'], size: '160%', background: { innerRadius: '60%', outerRadius: '100%', shape: 'arc', }, }, yAxis: { min: 0, max: 100, stops: [ [0.1, '#55BF3B'], // green [0.5, '#DDDF0D'], // yellow [0.9, '#DF5353'], // red ], minorTickInterval: null, tickAmount: 2, labels: { y: 16, }, }, plotOptions: { solidgauge: { dataLabels: { y: -25, borderWidth: 0, useHTML: true, }, }, }, tooltip: { enabled: false, }, series: [{ name: null, data: [this.getRandomNumber(0, 100)], dataLabels: { format: '<div style="text-align: center"><span style="font-size: 1.25rem">{y}</span></div>', }, }], } as any); setInterval(() => { chart.series[0].points[0].update(this.getRandomNumber(0, 100)); }, 1000); } private PieChart(): void { let date = new Date(); const data: any[] = []; for (let i = 0; i < 5; i++) { date.setDate(new Date().getDate() + i); data.push({ name: `${date.getDate()}/${date.getMonth() + 1}`, y: this.getRandomNumber(0, 1000), }); } const chart = Highcharts.chart('chart-pie', { chart: { type: 'pie', }, title: { text: 'Pie Chart', }, credits: { enabled: false, }, tooltip: { headerFormat: `<span class="mb-2">Date: {point.key}</span><br>`, pointFormat: '<span>Amount: {point.y}</span>', useHTML: true, }, series: [{ name: null, innerSize: '50%', data, }], } as any); setInterval(() => { date.setDate(date.getDate() + 1); chart.series[0].addPoint({ name: `${date.getDate()}/${date.getMonth() + 1}`, y: this.getRandomNumber(0, 1000), }, true, true); }, 1500); } private ColumnChart(): void { let date = new Date(); const data: any[] = []; for (let i = 0; i < 10; i++) { date.setDate(new Date().getDate() + i); data.push({ name: `${date.getDate()}/${date.getMonth() + 1}`, y: this.getRandomNumber(0, 1000), }); } const chart = Highcharts.chart('chart-column' as any, { chart: { type: 'column', }, title: { text: 'Column Chart', }, credits: { enabled: false, }, legend: { enabled: false, }, yAxis: { min: 0, title: undefined, }, xAxis: { type: 'category', }, tooltip: { headerFormat: `<div>Date: {point.key}</div>`, pointFormat: `<div>{series.name}: {point.y}</div>`, shared: true, useHTML: true, }, plotOptions: { bar: { dataLabels: { enabled: true, }, }, }, series: [{ name: 'Amount', data, }], } as any); setInterval(() => { date.setDate(date.getDate() + 1); chart.series[0].addPoint({ name: `${date.getDate()}/${date.getMonth() + 1}`, y: this.getRandomNumber(0, 1000), }, true, true); }, 1500); } private LineChart(): void { let date = new Date(); const data: any[] = []; for (let i = 0; i < 10; i++) { date.setDate(new Date().getDate() + i); data.push([`${date.getDate()}/${date.getMonth() + 1}`, this.getRandomNumber(0, 1000)]); } const chart = Highcharts.chart('chart-line', { chart: { type: 'line', }, title: { text: 'Line Chart', }, credits: { enabled: false, }, legend: { enabled: false, }, yAxis: { title: { text: null, } }, xAxis: { type: 'category', }, tooltip: { headerFormat: `<div>Date: {point.key}</div>`, pointFormat: `<div>{series.name}: {point.y}</div>`, shared: true, useHTML: true, }, series: [{ name: 'Amount', data, }], } as any); setInterval(() => { date.setDate(date.getDate() + 1); chart.series[0].addPoint([`${date.getDate()}/${date.getMonth() + 1}`, this.getRandomNumber(0, 1000)], true, true); }, 1500); } }
Step 4 :
In app.component.html
<main id="main"> <div id="chart-gauge"></div> <div id="chart-pie"></div> <div id="chart-line"></div> <div id="chart-column"></div> </main>
Step 5 :
In app.component.scss
#main{ display: flex; } #chart-gauge, #chart-pie, #chart-column, #chart-line { display: block; min-width: 300px; max-width: 400px; height: 200px; margin: 0 auto; }
Step 6 :
Now our above code is ready to run. we will use the following command:
ng serve