Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍
TypeScript angularetffinancefintechghostfoliohacktoberfestinvestingnestjsosspersonal-financeportfolioprismasoftwarestocktrackertradingtypescriptwealth-managementweb
import { getTooltipOptions } from '@ghostfolio/common/chart-helper';
import { UNKNOWN_KEY } from '@ghostfolio/common/config';
import { getLocale, getSum, getTextColor } from '@ghostfolio/common/helper';
import { PortfolioPosition } from '@ghostfolio/common/interfaces';
import { ColorScheme } from '@ghostfolio/common/types';
import { DataSource } from '@ghostfolio/prisma/enums';
import {
AfterViewInit,
ChangeDetectionStrategy,
Component,
ElementRef,
Input,
OnChanges,
OnDestroy,
output,
viewChild
} from '@angular/core';
import { Big } from 'big.js';
import {
ArcElement,
Chart,
type ChartData,
type ChartDataset,
DoughnutController,
LinearScale,
Tooltip,
type TooltipOptions
} from 'chart.js';
import ChartDataLabels from 'chartjs-plugin-datalabels';
import { isUUID } from 'class-validator';
import Color from 'color';
import { NgxSkeletonLoaderModule } from 'ngx-skeleton-loader';
import OpenColor from 'open-color';
import { onPrefersColorSchemeChange } from '../chart';
import { PortfolioProportionChartClickEvent } from './interfaces/interfaces';
const {
blue,
cyan,
grape,
green,
indigo,
lime,
orange,
pink,
red,
teal,
violet,
yellow
} = OpenColor;
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [NgxSkeletonLoaderModule],
selector: 'gf-portfolio-proportion-chart',
styleUrls: ['./portfolio-proportion-chart.component.scss'],
templateUrl: './portfolio-proportion-chart.component.html'
})
export class GfPortfolioProportionChartComponent
implements AfterViewInit, OnChanges, OnDestroy
{
@Input() baseCurrency?: string;
@Input() colorScheme?: ColorScheme;
@Input() cursor: string;
@Input() data: {
[symbol: string]: Pick<PortfolioPosition, 'type'> & {
dataSource?: DataSource;
isClickable?: boolean;
name?: string | null;
value: number;
};
} = {};
@Input() isInPercentage?: boolean = false;
@Input() keys: string[] = [];
@Input() locale?: string = getLocale();
@Input() maxItems?: number;
@Input() showLabels = false;
public chart: Chart<'doughnut'>;
public isLoading = true;
protected readonly proportionChartClicked =
output<PortfolioProportionChartClickEvent>();
private readonly OTHER_KEY = 'OTHER';
private readonly chartCanvas =
viewChild.required<ElementRef<HTMLCanvasElement>>('chartCanvas');
private colorMap: {
[symbol: string]: string;
} = {};
public constructor() {
Chart.register(ArcElement, DoughnutController, LinearScale, Tooltip);
onPrefersColorSchemeChange(() => {
if (this.chart && !this.colorScheme) {
this.initialize();
}
});
}
public ngAfterViewInit() {
if (this.data) {
this.initialize();
}
}
public ngOnChanges() {
if (this.data) {
this.initialize();
}
}
public ngOnDestroy() {
this.chart?.destroy();
}
private initialize() {
this.isLoading = true;
const chartData: {
[symbol: string]: {
color?: string;
name: string;
subCategory?: { [symbol: string]: { value: Big } };
value: Big;
};
} = {};
const textColor = getTextColor(this.colorScheme);
this.colorMap = {
[this.OTHER_KEY]: `rgba(${textColor}, 0.24)`,
[UNKNOWN_KEY]: `rgba(${textColor}, 0.12)`
};
if (this.keys.length > 0) {
const primaryKey = this.keys[0];
const secondaryKey = this.keys[1];
Object.keys(this.data).forEach((symbol) => {
const asset = this.data[symbol];
const assetValue = asset.value || 0;
const primaryKeyValue = (asset[primaryKey] as string)?.toUpperCase();
const secondaryKeyValue = asset[secondaryKey] as string;
if (primaryKeyValue) {
if (chartData[primaryKeyValue]) {
chartData[primaryKeyValue].value =
chartData[primaryKeyValue].value.plus(assetValue);
const targetSubCategory =
chartData[primaryKeyValue].subCategory?.[secondaryKeyValue];
if (targetSubCategory) {
targetSubCategory.value =
targetSubCategory.value.plus(assetValue);
} else {
if (chartData[primaryKeyValue].subCategory) {
chartData[primaryKeyValue].subCategory[
secondaryKeyValue ?? UNKNOWN_KEY
] = {
value: new Big(assetValue)
};
}
}
} else {
chartData[primaryKeyValue] = {
name: asset[primaryKey] as string,
subCategory: {},
value: new Big(assetValue)
};
if (secondaryKeyValue) {
chartData[primaryKeyValue].subCategory = {
[secondaryKeyValue]: {
value: new Big(assetValue)
}
};
}
}
} else {
if (chartData[UNKNOWN_KEY]) {
chartData[UNKNOWN_KEY].value = chartData[UNKNOWN_KEY].value.plus(
this.data[symbol].value || 0
);
} else {
chartData[UNKNOWN_KEY] = {
name: this.data[symbol].name ?? '',
subCategory: secondaryKey
? { [secondaryKey]: { value: new Big(0) } }
: undefined,
value: new Big(assetValue)
};
}
}
});
} else {
Object.keys(this.data).forEach((symbol) => {
chartData[symbol] = {
name: this.data[symbol].name ?? '',
value: new Big(this.data[symbol].value || 0)
};
});
}
if (this.isInPercentage) {
const totalValueInPercentage = getSum(
Object.values(chartData).map(({ value }) => {
return value;
})
);
const unknownValueInPercentage = new Big(1).minus(totalValueInPercentage);
// If total is below 100%, allocate the remaining percentage to UNKNOWN_KEY,
// but ignore negligible remainders to avoid a phantom slice
if (unknownValueInPercentage.gt(0.0001)) {
if (chartData[UNKNOWN_KEY]) {
chartData[UNKNOWN_KEY].value = chartData[UNKNOWN_KEY].value.plus(
unknownValueInPercentage
);
} else {
chartData[UNKNOWN_KEY] = {
name: UNKNOWN_KEY,
value: unknownValueInPercentage
};
}
}
}
let chartDataSorted = Object.entries(chartData)
.sort((a, b) => {
return a[1].value.minus(b[1].value).toNumber();
})
.reverse();
if (this.maxItems && chartDataSorted.length > this.maxItems) {
// Add surplus items to OTHER group
const rest = chartDataSorted.splice(
this.maxItems,
chartDataSorted.length - 1
);
chartDataSorted.push([
this.OTHER_KEY,
{ name: this.OTHER_KEY, subCategory: {}, value: new Big(0) }
]);
const otherItem = chartDataSorted[chartDataSorted.length - 1];
rest.forEach((restItem) => {
if (otherItem?.[1]) {
otherItem[1] = {
name: this.OTHER_KEY,
subCategory: {},
value: otherItem[1].value.plus(restItem[1].value)
};
}
});
// Sort data again
chartDataSorted = chartDataSorted
.sort((a, b) => {
return a[1].value.minus(b[1].value).toNumber();
})
.reverse();
}
chartDataSorted.forEach(([symbol, item], index) => {
if (this.colorMap[symbol]) {
// Reuse color
item.color = this.colorMap[symbol];
} else {
item.color =
this.getColorPalette()[index % this.getColorPalette().length];
}
});
const backgroundColorSubCategory: string[] = [];
const dataSubCategory: number[] = [];
const labelSubCategory: string[] = [];
chartDataSorted.forEach(([, item]) => {
let lightnessRatio = 0.2;
Object.keys(item.subCategory ?? {}).forEach((subCategory) => {
if (item.name === UNKNOWN_KEY) {
backgroundColorSubCategory.push(item.color ?? '');
} else {
backgroundColorSubCategory.push(
Color(item.color).lighten(lightnessRatio).hex()
);
}
dataSubCategory.push(
item.subCategory?.[subCategory].value.toNumber() ?? 0
);
labelSubCategory.push(subCategory);
lightnessRatio += 0.1;
});
});
const datasets: ChartDataset<'doughnut'>[] = [
{
backgroundColor: chartDataSorted.map(([, item]) => {
return item.color;
}),
borderWidth: 0,
data: chartDataSorted.map(([, item]) => {
return item.value.toNumber();
})
}
];
let labels = chartDataSorted.map(([, { name }]) => {
return name;
});
if (this.keys[1]) {
datasets.unshift({
backgroundColor: backgroundColorSubCategory,
borderWidth: 0,
data: dataSubCategory
});
labels = labelSubCategory.concat(labels);
}
if (datasets[0]?.data?.length === 0 || datasets[0]?.data?.[0] === 0) {
labels = [''];
datasets[0].backgroundColor = [this.colorMap[UNKNOWN_KEY]];
datasets[0].data[0] = Number.MAX_SAFE_INTEGER;
}
if (datasets[1]?.data?.length === 0 || datasets[1]?.data?.[1] === 0) {
labels = [''];
datasets[1].backgroundColor = [this.colorMap[UNKNOWN_KEY]];
datasets[1].data[1] = Number.MAX_SAFE_INTEGER;
}
const data: ChartData<'doughnut'> = {
datasets,
labels
};
if (this.chartCanvas()) {
if (this.chart) {
this.chart.data = data;
this.chart.options.plugins ??= {};
this.chart.options.plugins.tooltip =
this.getTooltipPluginConfiguration(data);
this.chart.update();
} else {
this.chart = new Chart<'doughnut'>(this.chartCanvas().nativeElement, {
data,
options: {
animation: false,
cutout: '70%',
layout: {
padding: this.showLabels === true ? 100 : 0
},
onClick: (_, activeElements, chart) => {
try {
const dataIndex = activeElements[0].index;
const symbol = chart.data.labels?.[dataIndex] as string;
if (this.data[symbol]?.isClickable === false) {
return;
}
const dataSource = this.data[symbol]?.dataSource;
this.proportionChartClicked.emit(
dataSource ? { dataSource, symbol } : { accountId: symbol }
);
} catch {}
},
onHover: (event, chartElement, chart) => {
if (this.cursor) {
const symbol = chartElement[0]
? (chart.data.labels?.[chartElement[0].index] as string)
: undefined;
(event.native?.target as HTMLElement).style.cursor =
symbol && this.data[symbol]?.isClickable !== false
? this.cursor
: 'default';
}
},
plugins: {
datalabels: {
color: (context) => {
return this.getColorPalette()[
context.dataIndex % this.getColorPalette().length
];
},
display: this.showLabels === true ? 'auto' : false,
labels: {
index: {
align: 'end',
anchor: 'end',
formatter: (value, context) => {
const symbol = context.chart.data.labels?.[
context.dataIndex
] as string;
if (value <= 0) {
return '';
}
return isUUID(symbol)
? (this.data[symbol]?.name ?? symbol)
: symbol;
},
offset: 8
}
}
},
legend: { display: false },
tooltip: this.getTooltipPluginConfiguration(data)
}
},
plugins: [ChartDataLabels],
type: 'doughnut'
});
}
}
this.isLoading = false;
}
private getColorPalette() {
return [
blue[5],
teal[5],
lime[5],
orange[5],
pink[5],
violet[5],
indigo[5],
cyan[5],
green[5],
yellow[5],
red[5],
grape[5]
];
}
private getTooltipPluginConfiguration(
data: ChartData<'doughnut'>
): Partial<TooltipOptions<'doughnut'>> {
return {
...getTooltipOptions({
colorScheme: this.colorScheme,
currency: this.baseCurrency,
locale: this.locale
}),
// @ts-expect-error: no need to set all attributes in callbacks
callbacks: {
label: (context) => {
const labelIndex =
(data.datasets[context.datasetIndex - 1]?.data?.length ?? 0) +
context.dataIndex;
let symbol =
(context.chart.data.labels?.[labelIndex] as string) ?? '';
if (symbol === this.OTHER_KEY) {
symbol = $localize`Other`;
} else if (symbol === UNKNOWN_KEY) {
symbol = $localize`No data available`;
}
const name = this.data[symbol]?.name;
let sum = 0;
for (const item of context.dataset.data) {
sum += item;
}
const percentage = (context.parsed * 100) / sum;
if ((context.raw as number) === Number.MAX_SAFE_INTEGER) {
return $localize`No data available`;
} else if (this.isInPercentage) {
return [`${name ?? symbol}`, `${percentage.toFixed(2)}%`];
} else {
const value = context.raw as number;
return [
`${name ?? symbol}`,
`${value.toLocaleString(this.locale, {
maximumFractionDigits: 2,
minimumFractionDigits: 2,
useGrouping: true
})} ${this.baseCurrency ?? ''} (${percentage.toFixed(2)}%)`
];
}
},
title: () => {
return '';
}
}
};
}
}