Browse Source

feat(lib): improve type safety in chart data calculation

pull/6491/head
KenTandrian 4 weeks ago
parent
commit
0dff634da0
  1. 68
      libs/ui/src/lib/portfolio-proportion-chart/portfolio-proportion-chart.component.ts

68
libs/ui/src/lib/portfolio-proportion-chart/portfolio-proportion-chart.component.ts

@ -130,45 +130,45 @@ export class GfPortfolioProportionChartComponent
}; };
if (this.keys.length > 0) { if (this.keys.length > 0) {
const primaryKey = this.keys[0];
const secondaryKey = this.keys[1];
Object.keys(this.data).forEach((symbol) => { Object.keys(this.data).forEach((symbol) => {
if (this.data[symbol][this.keys[0]]?.toUpperCase()) { const asset = this.data[symbol];
if (chartData[this.data[symbol][this.keys[0]].toUpperCase()]) { const assetValue = asset.value || 0;
chartData[this.data[symbol][this.keys[0]].toUpperCase()].value = const primaryKeyValue = (asset[primaryKey] as string)?.toUpperCase();
chartData[ const secondaryKeyValue = asset[secondaryKey] as string;
this.data[symbol][this.keys[0]].toUpperCase()
].value.plus(this.data[symbol].value || 0); if (primaryKeyValue) {
if (chartData[primaryKeyValue]) {
if ( chartData[primaryKeyValue].value =
chartData[this.data[symbol][this.keys[0]].toUpperCase()] chartData[primaryKeyValue].value.plus(assetValue);
.subCategory[this.data[symbol][this.keys[1]]]
) { const targetSubCategory =
chartData[ chartData[primaryKeyValue].subCategory?.[secondaryKeyValue];
this.data[symbol][this.keys[0]].toUpperCase() if (targetSubCategory) {
].subCategory[this.data[symbol][this.keys[1]]].value = chartData[ targetSubCategory.value =
this.data[symbol][this.keys[0]].toUpperCase() targetSubCategory.value.plus(assetValue);
].subCategory[this.data[symbol][this.keys[1]]].value.plus(
this.data[symbol].value || 0
);
} else { } else {
chartData[ if (chartData[primaryKeyValue].subCategory) {
this.data[symbol][this.keys[0]].toUpperCase() chartData[primaryKeyValue].subCategory[
].subCategory[this.data[symbol][this.keys[1]] ?? UNKNOWN_KEY] = { secondaryKeyValue ?? UNKNOWN_KEY
value: new Big(this.data[symbol].value || 0) ] = {
value: new Big(assetValue)
}; };
} }
}
} else { } else {
chartData[this.data[symbol][this.keys[0]].toUpperCase()] = { chartData[primaryKeyValue] = {
name: this.data[symbol][this.keys[0]], name: asset[primaryKey] as string,
subCategory: {}, subCategory: {},
value: new Big(this.data[symbol].value || 0) value: new Big(assetValue)
}; };
if (this.data[symbol][this.keys[1]]) { if (secondaryKeyValue) {
chartData[ chartData[primaryKeyValue].subCategory = {
this.data[symbol][this.keys[0]].toUpperCase() [secondaryKeyValue]: {
].subCategory = { value: new Big(assetValue)
[this.data[symbol][this.keys[1]]]: {
value: new Big(this.data[symbol].value || 0)
} }
}; };
} }
@ -181,10 +181,10 @@ export class GfPortfolioProportionChartComponent
} else { } else {
chartData[UNKNOWN_KEY] = { chartData[UNKNOWN_KEY] = {
name: this.data[symbol].name, name: this.data[symbol].name,
subCategory: this.keys[1] subCategory: secondaryKey
? { [this.keys[1]]: { value: new Big(0) } } ? { [secondaryKey]: { value: new Big(0) } }
: undefined, : undefined,
value: new Big(this.data[symbol].value || 0) value: new Big(assetValue)
}; };
} }
} }

Loading…
Cancel
Save