Browse Source

Overriding title for graphs where a groupement is defined

Signed-off-by: Martin Vandenbussche <vandenbusschemartin@gmail.com>
pull/1605/head
Martin Vandenbussche 3 years ago
parent
commit
5e68b5206f
  1. 14
      apps/client/src/app/components/investment-chart/investment-chart.component.ts
  2. 13
      libs/common/src/lib/chart-helper.ts
  3. 14
      libs/common/src/lib/helper.ts

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

@ -136,10 +136,13 @@ export class InvestmentChartComponent implements OnChanges, OnDestroy {
date, date,
investment: last(this.investments).investment investment: last(this.investments).investment
}); });
this.values.push({ date, value: last(this.values).value }); this.values.push({
date,
value: last(this.values).value
});
} }
const data = { const chartData = {
labels: this.historicalDataItems.map(({ date }) => { labels: this.historicalDataItems.map(({ date }) => {
return parseDate(date); return parseDate(date);
}), }),
@ -191,7 +194,7 @@ export class InvestmentChartComponent implements OnChanges, OnDestroy {
if (this.chartCanvas) { if (this.chartCanvas) {
if (this.chart) { if (this.chart) {
this.chart.data = data; this.chart.data = chartData;
this.chart.options.plugins.tooltip = <unknown>( this.chart.options.plugins.tooltip = <unknown>(
this.getTooltipPluginConfiguration() this.getTooltipPluginConfiguration()
); );
@ -210,7 +213,7 @@ export class InvestmentChartComponent implements OnChanges, OnDestroy {
this.chart.update(); this.chart.update();
} else { } else {
this.chart = new Chart(this.chartCanvas.nativeElement, { this.chart = new Chart(this.chartCanvas.nativeElement, {
data, data: chartData,
options: { options: {
animation: false, animation: false,
elements: { elements: {
@ -326,7 +329,8 @@ export class InvestmentChartComponent implements OnChanges, OnDestroy {
colorScheme: this.colorScheme, colorScheme: this.colorScheme,
currency: this.isInPercent ? undefined : this.currency, currency: this.isInPercent ? undefined : this.currency,
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',

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

@ -1,18 +1,20 @@
import { Chart, TooltipPosition } from 'chart.js'; import { Chart, TooltipPosition } from 'chart.js';
import { getBackgroundColor, getTextColor } from './helper'; import { formatGroupedDate, getBackgroundColor, getTextColor } from './helper';
import { ColorScheme } from './types'; import { ColorScheme, GroupBy } from './types';
export function getTooltipOptions({ export function getTooltipOptions({
colorScheme, colorScheme,
currency = '', currency = '',
locale = '', locale = '',
unit = '' unit = '',
groupBy
}: { }: {
colorScheme?: ColorScheme; colorScheme?: ColorScheme;
currency?: string; currency?: string;
locale?: string; locale?: string;
unit?: string; unit?: string;
groupBy?: GroupBy;
} = {}) { } = {}) {
return { return {
backgroundColor: getBackgroundColor(colorScheme), backgroundColor: getBackgroundColor(colorScheme),
@ -38,6 +40,11 @@ export function getTooltipOptions({
} }
} }
return label; return label;
},
title: (contexts) => {
if (groupBy) {
return formatGroupedDate(contexts[0].parsed.x, groupBy);
}
} }
}, },
caretSize: 0, caretSize: 0,

14
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 { getDate, getMonth, getYear, parse, subDays } from 'date-fns'; import { format, 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 } from './types'; import { ColorScheme, GroupBy } from './types';
const NUMERIC_REGEXP = /[-]{0,1}[\d]*[.,]{0,1}[\d]+/g; const NUMERIC_REGEXP = /[-]{0,1}[\d]*[.,]{0,1}[\d]+/g;
@ -233,11 +233,21 @@ export function resolveMarketCondition(
} }
export const DATE_FORMAT = 'yyyy-MM-dd'; export const DATE_FORMAT = 'yyyy-MM-dd';
export const DATE_FORMAT_MONTHLY = 'MMMM yyyy';
export const DATE_FORMAT_YEARLY = 'yyyy';
export function parseDate(date: string) { 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, '');
} }

Loading…
Cancel
Save