import { Component, AfterViewInit, OnDestroy, ViewChild } from '@angular/core';
import { NgIf } from '@angular/common';
import ApexCharts from 'apexcharts';
import {
ChartComponent,
ApexAxisChartSeries,
ApexNonAxisChartSeries,
ApexChart,
ApexXAxis,
ApexYAxis,
ApexTitleSubtitle,
ApexDataLabels,
ApexStroke,
ApexFill,
ApexLegend,
ApexTooltip,
ApexMarkers,
ApexPlotOptions,
ApexResponsive,
ApexGrid,
ApexAnnotations,
ApexStates,
ApexTheme,
NgApexchartsModule,
} from 'ng-apexcharts';
export type ChartOptions = {
series?: ApexAxisChartSeries | ApexNonAxisChartSeries;
chart?: ApexChart;
xaxis?: ApexXAxis;
yaxis?: ApexYAxis | ApexYAxis[];
title?: ApexTitleSubtitle;
subtitle?: ApexTitleSubtitle;
dataLabels?: ApexDataLabels;
stroke?: ApexStroke;
fill?: ApexFill;
legend?: ApexLegend;
tooltip?: ApexTooltip;
markers?: ApexMarkers;
plotOptions?: ApexPlotOptions;
responsive?: ApexResponsive[];
grid?: ApexGrid;
annotations?: ApexAnnotations;
states?: ApexStates;
theme?: ApexTheme;
colors?: string[];
labels?: any;
};
@Component({
selector: 'app-chart',
standalone: true,
imports: [NgApexchartsModule, NgIf],
templateUrl: './chart.component.html',
})
export class AppChart implements AfterViewInit, OnDestroy {
@ViewChild('chart') chart!: ChartComponent;
private mulberry32 = (seed: any): any => {
return function () {
seed |= 0
seed = (seed + 0x6d2b79f5) | 0
var t = Math.imul(seed ^ (seed >>> 15), 1 | seed)
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t
return ((t ^ (t >>> 14)) >>> 0) / 4294967296
}
};
private rand: any = this.mulberry32(42);
private data1: any = [];
private data2: any = [];
private ts: any = new Date('01 Oct 2024').getTime();
private level: any = 300;
public chartOptions: Partial<ChartOptions> = {
series: [
{
name: 'Orders',
data: this.data1,
},
{
name: 'Revenue',
data: this.data2,
},
],
chart: {
id: 'chart2',
type: 'line',
height: 230,
toolbar: {
autoSelected: 'pan',
show: false,
},
},
title: {
text: 'Daily Orders & Revenue',
align: 'left',
},
colors: ['#008FFB', '#00E396'],
dataLabels: {
enabled: false,
},
stroke: {
width: [2, 4],
curve: ['straight', 'monotoneCubic'],
},
fill: {
opacity: [1, 0.75],
},
markers: {
size: 0,
},
yaxis: [
{
seriesName: 'Orders',
axisTicks: {
show: true,
color: '#008FFB',
},
axisBorder: {
show: true,
color: '#008FFB',
},
labels: {
style: {
colors: '#008FFB',
},
},
title: {
text: 'Orders',
style: {
color: '#008FFB',
},
},
},
{
seriesName: 'Revenue',
opposite: true,
axisTicks: {
show: true,
color: '#00E396',
},
axisBorder: {
show: true,
color: '#00E396',
},
labels: {
style: {
colors: '#00E396',
},
},
title: {
text: 'Revenue ($k)',
style: {
color: '#00E396',
},
},
},
],
xaxis: {
type: 'datetime',
},
};
public chartOptions2: Partial<ChartOptions> = {
series: [
{
name: 'Orders',
data: this.data1,
},
{
name: 'Revenue',
data: this.data2,
},
],
chart: {
id: 'chart1',
height: 130,
type: 'area',
brush: {
target: 'chart2',
enabled: true,
},
selection: {
enabled: true,
xaxis: {
min: new Date('05 Jan 2025').getTime(),
max: new Date('10 Feb 2025').getTime(),
},
},
},
colors: ['#008FFB', '#00E396'],
stroke: {
width: [1, 3],
curve: ['straight', 'monotoneCubic'],
},
fill: {
type: 'gradient',
gradient: {
opacityFrom: 0.91,
opacityTo: 0.1,
},
},
xaxis: {
type: 'datetime',
tooltip: {
enabled: false,
},
},
yaxis: {
tickAmount: 2,
},
};
public brushChartReady = false;
ngAfterViewInit() {
const poll = () => {
if ((ApexCharts as any).getChartByID('chart2')) {
this.brushChartReady = true;
} else {
requestAnimationFrame(poll);
}
};
requestAnimationFrame(poll);
}
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJpc3N1ZURhdGUiOiIyMDI2LTA0LTE2IiwiZXhwaXJ5RGF0ZSI6IjIwNTItMDQtMTciLCJwbGFuIjoiZW50ZXJwcmlzZSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiXX0=');
for (var i = 0; i < 180; i++) {
this.level += (this.rand() - 0.5) * 55
this.level += (300 - this.level) * 0.03
if (this.rand() > 0.94) this.level += (this.rand() - 0.5) * 90
if (this.level < 120) this.level = 120
var orders = Math.round(this.level)
this.data1.push([this.ts, orders])
this.data2.push([this.ts, Math.round(orders * (0.32 + this.rand() * 0.12) * 10) / 10])
this.ts += 86400000
}
}
ngOnDestroy() {
// no cleanup needed
}
}