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 days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
// quarter -> trades per weekday (Mon..Fri); column totals differ per quarter.
var plan = {
Q1: [8, 4, 3, 2, 1], // early-week heavy
Q2: [1, 2, 5, 3, 1], // mid-week heavy
Q3: [2, 2, 3, 4, 5], // late-week heavy
Q4: [3, 6, 2, 2, 1], // Tuesday spike
}
// Share of Gains by weekday index (Mon..Fri): high early, low late.
var gainByDay = [0.85, 0.7, 0.5, 0.3, 0.15]
var out = []
Object.keys(plan).forEach(function (q) {
plan[q].forEach(function (count, di) {
var gains = Math.round(count * gainByDay[di])
for (var k = 0; k < count; k++) {
out.push({
q: q,
day: days[di],
gl: k < gains ? 'Gain' : 'Loss',
})
}
})
})
return out
};
private fmtFilters = (state: any): any => {
var keys = Object.keys(state.filters)
if (!keys.length) return 'No filter (all ' + state.total + ' trades)'
var parts = keys.map(function (id) {
return id + ': ' + state.filters[id].join(', ')
})
return (
parts.join(' | ') +
' -> ' +
state.filteredCount +
' / ' +
state.total +
' trades'
)
};
private cf: any = ApexCharts.getCrossfilter('trades');
private readout: any = document.getElementById('cf-readout');
public chartOptions: Partial<ChartOptions> = {
series: [],
chart: {
id: 'byQuarter',
type: 'donut',
height: 300,
fontFamily: 'Helvetica, Arial, sans-serif',
animations: { speed: 500 },
link: {
id: 'trades',
dimension: (r) => {
return r.q
},
reduce: 'count',
dimOpacity: 0.18,
},
},
title: { text: 'By quarter', align: 'left' },
legend: { position: 'bottom' },
plotOptions: { pie: { expandOnClick: false } },
dataLabels: {
enabled: true,
formatter: (val, opts) => {
return opts.w.config.series[opts.seriesIndex]
},
style: { colors: ['#334155'], fontWeight: 600 },
dropShadow: { enabled: false },
},
colors: ['#2563EB', '#38bdf8', '#4ade80', '#fbbf24'],
stroke: { width: 2, colors: ['#fff'] },
};
public chartOptions2: Partial<ChartOptions> = {
series: [],
chart: {
id: 'byOutcome',
type: 'donut',
height: 300,
fontFamily: 'Helvetica, Arial, sans-serif',
animations: { speed: 500 },
link: {
id: 'trades',
dimension: (r) => {
return r.gl
},
reduce: 'count',
order: 'asc', // Gain before Loss, so the colors below map semantically
dimOpacity: 0.18,
},
},
title: { text: 'By outcome', align: 'left' },
legend: { position: 'bottom' },
plotOptions: { pie: { expandOnClick: false } },
dataLabels: {
enabled: true,
formatter: (val, opts) => {
return opts.w.config.series[opts.seriesIndex]
},
style: { colors: ['#334155'], fontWeight: 600 },
dropShadow: { enabled: false },
},
colors: ['#4ade80', '#f87171'],
stroke: { width: 2, colors: ['#fff'] },
};
public chartOptions3: Partial<ChartOptions> = {
series: [],
chart: {
id: 'byDay',
type: 'bar',
height: 280,
fontFamily: 'Helvetica, Arial, sans-serif',
animations: { speed: 500 },
link: {
id: 'trades',
dimension: (r) => {
return r.day
},
reduce: 'count',
seriesName: 'Trades',
// `order` also takes a comparator: keep the weekdays in calendar order
// instead of the order they first appear in the records.
order: (a, b) => {
var days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
return days.indexOf(a) - days.indexOf(b)
},
dimOpacity: 0.18,
},
},
title: { text: 'By day of week (click a bar too)', align: 'left' },
plotOptions: {
bar: { columnWidth: '55%', borderRadius: 3, distributed: true },
},
legend: { show: false },
dataLabels: { enabled: false },
colors: ['#2563EB', '#38bdf8', '#4ade80', '#fbbf24', '#f472b6'],
};
ngAfterViewInit() {
(window as any).ApexCharts.setLicense('APEX-eyJleHBpcnlEYXRlIjoiMjEyNi0wNy0wNCIsImlzc3VlRGF0ZSI6IjIwMjYtMDctMjgiLCJwbGFuIjoicHJlbWl1bSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiLCJsb2NhbGhvc3QiXSwic2lnIjoieVBmb1VCc0Z3TU9ZdUEyaEZkR0I2Y1FtZ0JITUtXcVdJSjB2NVRESXRZbFR3eDJMUmh6R2x0RUc3VXJ4X0s3b25ZMWRZb2Z2VGItN01ydFYyNDVyOWcifQ==');
ApexCharts.crossfilter({ id: 'trades', records: this.tradesData() })
document.getElementById('cf-reset').addEventListener('click', () => {
if (this.cf) this.cf.reset()
})
}
ngOnDestroy() {
// no cleanup needed
}
}