Browse Source

feat(ui): implement input signal

pull/7325/head
KenTandrian 2 days ago
parent
commit
838ca5958f
  1. 43
      libs/ui/src/lib/treemap-chart/treemap-chart.component.ts

43
libs/ui/src/lib/treemap-chart/treemap-chart.component.ts

@ -15,7 +15,7 @@ import {
ChangeDetectionStrategy, ChangeDetectionStrategy,
Component, Component,
ElementRef, ElementRef,
Input, input,
OnChanges, OnChanges,
OnDestroy, OnDestroy,
output, output,
@ -50,12 +50,12 @@ const { gray, green, red } = OpenColor;
export class GfTreemapChartComponent export class GfTreemapChartComponent
implements AfterViewInit, OnChanges, OnDestroy implements AfterViewInit, OnChanges, OnDestroy
{ {
@Input() baseCurrency: string; public readonly baseCurrency = input.required<string>();
@Input() colorScheme: ColorScheme; public readonly colorScheme = input.required<ColorScheme>();
@Input() cursor: string; public readonly cursor = input.required<string>();
@Input() dateRange: DateRange; public readonly dateRange = input.required<DateRange>();
@Input() holdings: PortfolioPosition[] | undefined; public readonly holdings = input<PortfolioPosition[]>();
@Input() locale = getLocale(); public readonly locale = input<string>(getLocale());
public readonly treemapChartClicked = output<AssetProfileIdentifier>(); public readonly treemapChartClicked = output<AssetProfileIdentifier>();
@ -68,6 +68,7 @@ export class GfTreemapChartComponent
public constructor() { public constructor() {
Chart.register(LinearScale, Tooltip, TreemapController, TreemapElement); Chart.register(LinearScale, Tooltip, TreemapController, TreemapElement);
} }
public ngAfterViewInit() { public ngAfterViewInit() {
this.initialize(); this.initialize();
} }
@ -154,17 +155,19 @@ export class GfTreemapChartComponent
} }
private initialize() { private initialize() {
if (!this.holdings) { const holdings = this.holdings();
if (!holdings) {
return; return;
} }
this.isLoading = true; this.isLoading = true;
const { endDate, startDate } = getIntervalFromDateRange({ const { endDate, startDate } = getIntervalFromDateRange({
dateRange: this.dateRange dateRange: this.dateRange()
}); });
const netPerformancePercentsWithCurrencyEffect = this.holdings.map( const netPerformancePercentsWithCurrencyEffect = holdings.map(
({ dateOfFirstActivity, netPerformancePercentWithCurrencyEffect }) => { ({ dateOfFirstActivity, netPerformancePercentWithCurrencyEffect }) => {
return getAnnualizedPerformancePercent({ return getAnnualizedPerformancePercent({
daysInMarket: differenceInDays( daysInMarket: differenceInDays(
@ -300,7 +303,7 @@ export class GfTreemapChartComponent
}, },
spacing: 1, spacing: 1,
// @ts-expect-error: should be PortfolioPosition[] // @ts-expect-error: should be PortfolioPosition[]
tree: this.holdings tree: this.holdings()
} }
] ]
}; };
@ -338,9 +341,9 @@ export class GfTreemapChartComponent
} catch {} } catch {}
}, },
onHover: (event, chartElement) => { onHover: (event, chartElement) => {
if (this.cursor) { if (this.cursor()) {
(event.native?.target as HTMLElement).style.cursor = (event.native?.target as HTMLElement).style.cursor =
chartElement[0] ? this.cursor : 'default'; chartElement[0] ? this.cursor() : 'default';
} }
}, },
plugins: { plugins: {
@ -358,9 +361,9 @@ export class GfTreemapChartComponent
private getTooltipPluginConfiguration(): Partial<TooltipOptions<'treemap'>> { private getTooltipPluginConfiguration(): Partial<TooltipOptions<'treemap'>> {
return { return {
...getTooltipOptions({ ...getTooltipOptions({
colorScheme: this.colorScheme, colorScheme: this.colorScheme(),
currency: this.baseCurrency, currency: this.baseCurrency(),
locale: this.locale locale: this.locale()
}), }),
// @ts-expect-error: no need to set all attributes in callbacks // @ts-expect-error: no need to set all attributes in callbacks
callbacks: { callbacks: {
@ -380,19 +383,19 @@ export class GfTreemapChartComponent
return [ return [
`${name ?? symbol} (${allocationInPercentage})`, `${name ?? symbol} (${allocationInPercentage})`,
`${value?.toLocaleString(this.locale, { `${value?.toLocaleString(this.locale(), {
maximumFractionDigits: 2, maximumFractionDigits: 2,
minimumFractionDigits: 2 minimumFractionDigits: 2
})} ${this.baseCurrency}`, })} ${this.baseCurrency()}`,
'', '',
$localize`Change` + ' (' + $localize`Performance` + ')', $localize`Change` + ' (' + $localize`Performance` + ')',
`${sign}${raw._data.netPerformanceWithCurrencyEffect.toLocaleString( `${sign}${raw._data.netPerformanceWithCurrencyEffect.toLocaleString(
this.locale, this.locale(),
{ {
maximumFractionDigits: 2, maximumFractionDigits: 2,
minimumFractionDigits: 2 minimumFractionDigits: 2
} }
)} ${this.baseCurrency} (${netPerformanceInPercentageWithSign})` )} ${this.baseCurrency()} (${netPerformanceInPercentageWithSign})`
]; ];
} else { } else {
return [ return [

Loading…
Cancel
Save