import { Component, AfterViewInit, OnDestroy, ViewChild } from '@angular/core';
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],
templateUrl: './chart.component.html',
})
export class AppChart implements AfterViewInit, OnDestroy {
@ViewChild('chart') chart!: ChartComponent;
private tradesData = (): any => {
var quarters = ['Q1', 'Q2', 'Q3', 'Q4']
var days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
var out = []
for (var i = 0; i < 120; i++) {
var pct =
3.0 * Math.sin(i / 5.0) +
1.3 * Math.sin(i / 1.7) +
0.4 * Math.cos(i / 3.1)
var vol = Math.round(120 + 70 * Math.sin(i / 6.0) + 40 * Math.cos(i / 2.3))
out.push({
id: i + 1,
quarter: quarters[Math.floor(i / 30) % 4],
day: days[(i * 3) % 5],
outcome: pct >= 0 ? 'Gain' : 'Loss',
pct: Math.round(pct * 100) / 100,
volume: Math.max(20, vol),
})
}
return out
};
private cf: any = ApexCharts.getCrossfilter('trades');
private readout: any = document.getElementById('cfd-readout');
public chartOptions: Partial<ChartOptions> = {
series: [],
chart: {
id: 'byQuarter',
type: 'donut',
height: 260,
fontFamily: 'Helvetica, Arial, sans-serif',
animations: { speed: 450 },
link: {
id: 'trades',
dimension: (r) => {
return r.quarter
},
reduce: 'count',
dimOpacity: 0.18,
},
},
title: { text: 'Quarter', align: 'left' },
legend: { position: 'bottom', fontSize: '12px' },
plotOptions: { pie: { expandOnClick: false } },
dataLabels: {
enabled: true,
formatter: (v, o) => {
return o.w.config.series[o.seriesIndex]
},
},
colors: ['#2563EB', '#0ea5e9', '#22c55e', '#f59e0b'],
};
public chartOptions2: Partial<ChartOptions> = {
series: [],
chart: {
id: 'byOutcome',
type: 'donut',
height: 260,
fontFamily: 'Helvetica, Arial, sans-serif',
animations: { speed: 450 },
link: {
id: 'trades',
dimension: (r) => {
return r.outcome
},
reduce: 'count',
dimOpacity: 0.18,
},
},
title: { text: 'Outcome', align: 'left' },
legend: { position: 'bottom', fontSize: '12px' },
plotOptions: { pie: { expandOnClick: false } },
dataLabels: {
enabled: true,
formatter: (v, o) => {
return o.w.config.series[o.seriesIndex]
},
},
colors: ['#22c55e', '#ef4444'],
};
public chartOptions3: Partial<ChartOptions> = {
series: [],
chart: {
id: 'byDay',
type: 'bar',
height: 260,
fontFamily: 'Helvetica, Arial, sans-serif',
animations: { speed: 450 },
link: {
id: 'trades',
dimension: (r) => {
return r.day
},
reduce: 'count',
seriesName: 'Trades',
dimOpacity: 0.18,
},
},
title: { text: 'Day of week', align: 'left' },
plotOptions: {
bar: { columnWidth: '55%', borderRadius: 3, distributed: true },
},
legend: { show: false },
dataLabels: { enabled: false },
colors: ['#2563EB', '#0ea5e9', '#22c55e', '#f59e0b', '#F43F5E'],
};
public chartOptions4: Partial<ChartOptions> = {
series: [],
chart: {
id: 'byFluctuation',
type: 'bar',
height: 240,
fontFamily: 'Helvetica, Arial, sans-serif',
animations: { speed: 400 },
zoom: { enabled: false },
selection: { enabled: true },
toolbar: {
autoSelected: 'selection',
tools: {
selection: true,
zoom: false,
pan: false,
reset: false,
download: false,
},
},
link: {
id: 'trades',
dimension: (r) => {
return r.pct
},
bins: { count: 26 },
dimOpacity: 0.16,
},
},
title: { text: 'Fluctuation % (brush a range)', align: 'left' },
plotOptions: { bar: { columnWidth: '92%' } },
dataLabels: { enabled: false },
xaxis: {
type: 'numeric',
tickAmount: 8,
labels: {
formatter: (v) => {
return Number(v).toFixed(1)
},
},
},
colors: ['#2563EB'],
};
public chartOptions5: Partial<ChartOptions> = {
series: [],
chart: {
id: 'byVolume',
type: 'bar',
height: 240,
fontFamily: 'Helvetica, Arial, sans-serif',
animations: { speed: 400 },
zoom: { enabled: false },
selection: { enabled: true },
toolbar: {
autoSelected: 'selection',
tools: {
selection: true,
zoom: false,
pan: false,
reset: false,
download: false,
},
},
link: {
id: 'trades',
dimension: (r) => {
return r.volume
},
bins: { count: 22 },
dimOpacity: 0.16,
},
},
title: { text: 'Volume (brush a range)', align: 'left' },
plotOptions: { bar: { columnWidth: '92%' } },
dataLabels: { enabled: false },
xaxis: {
type: 'numeric',
tickAmount: 8,
labels: {
formatter: (v) => {
return Math.round(v)
},
},
},
colors: ['#0ea5e9'],
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJleHBpcnlEYXRlIjoiMjEyNi0wNy0wNCIsImlzc3VlRGF0ZSI6IjIwMjYtMDctMjgiLCJwbGFuIjoicHJlbWl1bSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiLCJsb2NhbGhvc3QiXSwic2lnIjoieVBmb1VCc0Z3TU9ZdUEyaEZkR0I2Y1FtZ0JITUtXcVdJSjB2NVRESXRZbFR3eDJMUmh6R2x0RUc3VXJ4X0s3b25ZMWRZb2Z2VGItN01ydFYyNDVyOWcifQ==');
ApexCharts.crossfilter({ id: 'trades', records: this.tradesData() })
document.getElementById('cfd-reset').addEventListener('click', () => {
if (this.cf) this.cf.reset()
})
}
ngOnDestroy() {
// no cleanup needed
}
}