Browse Source

Refactoring

pull/1605/head
Thomas 3 years ago
parent
commit
bbd9e18100
  1. 10
      apps/client/src/app/components/investment-chart/investment-chart.component.ts
  2. 39
      libs/common/src/lib/chart-helper.ts
  3. 16
      libs/common/src/lib/helper.ts
  4. 3
      libs/ui/src/lib/fire-calculator/fire-calculator.component.ts

10
apps/client/src/app/components/investment-chart/investment-chart.component.ts

@ -11,7 +11,8 @@ import {
import { import {
getTooltipOptions, getTooltipOptions,
getTooltipPositionerMapTop, getTooltipPositionerMapTop,
getVerticalHoverLinePlugin getVerticalHoverLinePlugin,
transformTickToAbbreviation
} from '@ghostfolio/common/chart-helper'; } from '@ghostfolio/common/chart-helper';
import { primaryColorRgb, secondaryColorRgb } from '@ghostfolio/common/config'; import { primaryColorRgb, secondaryColorRgb } from '@ghostfolio/common/config';
import { import {
@ -19,8 +20,7 @@ import {
getBackgroundColor, getBackgroundColor,
getDateFormatString, getDateFormatString,
getTextColor, getTextColor,
parseDate, parseDate
transformTickToAbbreviation
} from '@ghostfolio/common/helper'; } from '@ghostfolio/common/helper';
import { LineChartItem } from '@ghostfolio/common/interfaces'; import { LineChartItem } from '@ghostfolio/common/interfaces';
import { InvestmentItem } from '@ghostfolio/common/interfaces/investment-item.interface'; import { InvestmentItem } from '@ghostfolio/common/interfaces/investment-item.interface';
@ -328,9 +328,9 @@ export class InvestmentChartComponent implements OnChanges, OnDestroy {
...getTooltipOptions({ ...getTooltipOptions({
colorScheme: this.colorScheme, colorScheme: this.colorScheme,
currency: this.isInPercent ? undefined : this.currency, currency: this.isInPercent ? undefined : this.currency,
groupBy: this.groupBy,
locale: this.isInPercent ? undefined : this.locale, locale: this.isInPercent ? undefined : this.locale,
unit: this.isInPercent ? '%' : undefined, unit: this.isInPercent ? '%' : undefined
groupBy: this.groupBy
}), }),
mode: 'index', mode: 'index',
position: <unknown>'top', position: <unknown>'top',

39
libs/common/src/lib/chart-helper.ts

@ -1,20 +1,43 @@
import { Chart, TooltipPosition } from 'chart.js'; import { Chart, TooltipPosition } from 'chart.js';
import { format } from 'date-fns';
import { formatGroupedDate, getBackgroundColor, getTextColor } from './helper'; import {
DATE_FORMAT,
DATE_FORMAT_MONTHLY,
DATE_FORMAT_YEARLY,
getBackgroundColor,
getTextColor
} from './helper';
import { ColorScheme, GroupBy } from './types'; import { ColorScheme, GroupBy } from './types';
export function formatGroupedDate({
date,
groupBy
}: {
date: Date;
groupBy: GroupBy;
}) {
if (groupBy === 'month') {
return format(date, DATE_FORMAT_MONTHLY);
} else if (groupBy === 'year') {
return format(date, DATE_FORMAT_YEARLY);
}
return format(date, DATE_FORMAT);
}
export function getTooltipOptions({ export function getTooltipOptions({
colorScheme, colorScheme,
currency = '', currency = '',
groupBy,
locale = '', locale = '',
unit = '', unit = ''
groupBy
}: { }: {
colorScheme?: ColorScheme; colorScheme?: ColorScheme;
currency?: string; currency?: string;
groupBy?: GroupBy;
locale?: string; locale?: string;
unit?: string; unit?: string;
groupBy?: GroupBy;
} = {}) { } = {}) {
return { return {
backgroundColor: getBackgroundColor(colorScheme), backgroundColor: getBackgroundColor(colorScheme),
@ -43,8 +66,10 @@ export function getTooltipOptions({
}, },
title: (contexts) => { title: (contexts) => {
if (groupBy) { if (groupBy) {
return formatGroupedDate(contexts[0].parsed.x, groupBy); return formatGroupedDate({ groupBy, date: contexts[0].parsed.x });
} }
return contexts[0].label;
} }
}, },
caretSize: 0, caretSize: 0,
@ -104,3 +129,7 @@ export function getVerticalHoverLinePlugin(
id: 'verticalHoverLine' id: 'verticalHoverLine'
}; };
} }
export function transformTickToAbbreviation(value: number) {
return value < 1000000 ? `${value / 1000}K` : `${value / 1000000}M`;
}

16
libs/common/src/lib/helper.ts

@ -1,11 +1,11 @@
import * as currencies from '@dinero.js/currencies'; import * as currencies from '@dinero.js/currencies';
import { DataSource } from '@prisma/client'; import { DataSource } from '@prisma/client';
import { format, getDate, getMonth, getYear, parse, subDays } from 'date-fns'; import { getDate, getMonth, getYear, parse, subDays } from 'date-fns';
import { de, es, fr, it, nl, pt } from 'date-fns/locale'; import { de, es, fr, it, nl, pt } from 'date-fns/locale';
import { ghostfolioScraperApiSymbolPrefix, locale } from './config'; import { ghostfolioScraperApiSymbolPrefix, locale } from './config';
import { Benchmark } from './interfaces'; import { Benchmark } from './interfaces';
import { ColorScheme, GroupBy } from './types'; import { ColorScheme } from './types';
const NUMERIC_REGEXP = /[-]{0,1}[\d]*[.,]{0,1}[\d]+/g; const NUMERIC_REGEXP = /[-]{0,1}[\d]*[.,]{0,1}[\d]+/g;
@ -240,18 +240,6 @@ export function parseDate(date: string) {
return parse(date, DATE_FORMAT, new Date()); return parse(date, DATE_FORMAT, new Date());
} }
export function formatGroupedDate(date: Date, groupBy: GroupBy) {
if (groupBy === 'month') {
return format(date, DATE_FORMAT_MONTHLY);
} else if (groupBy === 'year') {
return format(date, DATE_FORMAT_YEARLY);
}
}
export function prettifySymbol(aSymbol: string): string { export function prettifySymbol(aSymbol: string): string {
return aSymbol?.replace(ghostfolioScraperApiSymbolPrefix, ''); return aSymbol?.replace(ghostfolioScraperApiSymbolPrefix, '');
} }
export function transformTickToAbbreviation(value: number) {
return value < 1000000 ? `${value / 1000}K` : `${value / 1000000}M`;
}

3
libs/ui/src/lib/fire-calculator/fire-calculator.component.ts

@ -13,9 +13,8 @@ import {
ViewChild ViewChild
} from '@angular/core'; } from '@angular/core';
import { FormBuilder, FormControl } from '@angular/forms'; import { FormBuilder, FormControl } from '@angular/forms';
import { getTooltipOptions } from '@ghostfolio/common/chart-helper'; import { getTooltipOptions, transformTickToAbbreviation } from '@ghostfolio/common/chart-helper';
import { primaryColorRgb } from '@ghostfolio/common/config'; import { primaryColorRgb } from '@ghostfolio/common/config';
import { transformTickToAbbreviation } from '@ghostfolio/common/helper';
import { ColorScheme } from '@ghostfolio/common/types'; import { ColorScheme } from '@ghostfolio/common/types';
import { import {
BarController, BarController,

Loading…
Cancel
Save