From 5794044e390b8d18cb1dc476d1ab3c933ffa827b Mon Sep 17 00:00:00 2001 From: mister-ideas <36502095+mister-ideas@users.noreply.github.com> Date: Sat, 31 Oct 2026 11:40:38 +0100 Subject: [PATCH] Feature/color the performance charts in green or red according to the trend --- CHANGELOG.md | 6 ++++ .../benchmark-comparator.component.ts | 15 ++++++++-- .../holding-detail-dialog.html | 1 + .../home-overview/home-overview.html | 1 + libs/common/src/lib/chart-helper.spec.ts | 17 ++++++++++- libs/common/src/lib/chart-helper.ts | 30 +++++++++++++++++++ .../benchmark-detail-dialog.html | 1 + .../lib/line-chart/line-chart.component.ts | 11 +++++-- 8 files changed, 76 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc3e536b6f..22a45d9060 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Changed + +- Colored the line of the performance chart on the overview page, of the portfolio in the benchmark comparator and of the charts in the holding and benchmark detail dialogs in green for a gain and red for a loss + ## 3.100.0 - 2026-11-15 ### Changed diff --git a/apps/client/src/app/components/benchmark-comparator/benchmark-comparator.component.ts b/apps/client/src/app/components/benchmark-comparator/benchmark-comparator.component.ts index 6a00e1a35e..60bca6d158 100644 --- a/apps/client/src/app/components/benchmark-comparator/benchmark-comparator.component.ts +++ b/apps/client/src/app/components/benchmark-comparator/benchmark-comparator.component.ts @@ -2,11 +2,12 @@ import { getChartBorderColor, getChartElementsOptions, getTimeAxisOptions, + getTrendColorRgb, getValueAxisOptions, getVerticalHoverLinePlugin, getZeroLineAnnotation } from '@ghostfolio/common/chart-helper'; -import { primaryColorRgb, secondaryColorRgb } from '@ghostfolio/common/config'; +import { secondaryColorRgb } from '@ghostfolio/common/config'; import { getLocale, parseDate } from '@ghostfolio/common/helper'; import { LineChartItem, User } from '@ghostfolio/common/interfaces'; import { hasPermission, permissions } from '@ghostfolio/common/permissions'; @@ -147,11 +148,19 @@ export class GfBenchmarkComparatorComponent implements OnChanges, OnDestroy { benchmarkDataValues[date] = value; } + // Green for a gain, red for a loss + const portfolioColor = `rgb(${getTrendColorRgb({ + reference: 0, + values: this.performanceDataItems().map(({ value }) => { + return value; + }) + })})`; + const data: ChartData<'line'> = { datasets: [ { - backgroundColor: `rgb(${primaryColorRgb.r}, ${primaryColorRgb.g}, ${primaryColorRgb.b})`, - borderColor: `rgb(${primaryColorRgb.r}, ${primaryColorRgb.g}, ${primaryColorRgb.b})`, + backgroundColor: portfolioColor, + borderColor: portfolioColor, borderWidth: 2, data: this.performanceDataItems().map(({ date, value }) => { return { diff --git a/apps/client/src/app/components/holding-detail-dialog/holding-detail-dialog.html b/apps/client/src/app/components/holding-detail-dialog/holding-detail-dialog.html index ad9fb58f54..5019d63e50 100644 --- a/apps/client/src/app/components/holding-detail-dialog/holding-detail-dialog.html +++ b/apps/client/src/app/components/holding-detail-dialog/holding-detail-dialog.html @@ -28,6 +28,7 @@ [currency]="assetProfile?.currency" [historicalDataItems]="historicalDataItems" [isAnimated]="true" + [isTrendColored]="true" [label]=" isUUID(data.symbol) ? (assetProfile?.name ?? data.symbol) : data.symbol " diff --git a/apps/client/src/app/components/home-overview/home-overview.html b/apps/client/src/app/components/home-overview/home-overview.html index 0f9e1bcfae..561772176b 100644 --- a/apps/client/src/app/components/home-overview/home-overview.html +++ b/apps/client/src/app/components/home-overview/home-overview.html @@ -75,6 +75,7 @@ [hidden]="historicalDataItems()?.length === 0" [historicalDataItems]="historicalDataItems()" [isAnimated]="user()?.settings?.dateRange === '1d' ? false : true" + [isTrendColored]="true" [label]="performanceLabel" [locale]="locale()" [showGradient]="true" diff --git a/libs/common/src/lib/chart-helper.spec.ts b/libs/common/src/lib/chart-helper.spec.ts index 29508fa5bb..42c48fa0a5 100644 --- a/libs/common/src/lib/chart-helper.spec.ts +++ b/libs/common/src/lib/chart-helper.spec.ts @@ -1,4 +1,4 @@ -import { getTooltipOptions } from './chart-helper'; +import { getTooltipOptions, getTrendColorRgb } from './chart-helper'; describe('getTooltipOptions', () => { const getLabel = ({ @@ -47,3 +47,18 @@ describe('getTooltipOptions', () => { expect(getLabel({ locale: 'de', y: 1234.5 })).toBe('Performance: 1.234,50'); }); }); + +describe('getTrendColorRgb', () => { + it('returns green for a gain and red for a loss', () => { + const green = getTrendColorRgb({ reference: 0, values: [0, 1, 2] }); + const red = getTrendColorRgb({ reference: 0, values: [0, 1, -0.5, null] }); + + expect(green).toBe('40, 167, 69'); + expect(red).toBe('220, 53, 69'); + }); + + it('compares with the first value without a reference', () => { + expect(getTrendColorRgb({ values: [100, 120, 90] })).toBe('220, 53, 69'); + expect(getTrendColorRgb({ values: [100, 80, 110] })).toBe('40, 167, 69'); + }); +}); diff --git a/libs/common/src/lib/chart-helper.ts b/libs/common/src/lib/chart-helper.ts index 179a3b4f1e..9e30ab37a8 100644 --- a/libs/common/src/lib/chart-helper.ts +++ b/libs/common/src/lib/chart-helper.ts @@ -41,6 +41,36 @@ export function formatGroupedDate({ return format(date, DATE_FORMAT); } +// Green and red of the theme (`$green` and `$red` of Bootstrap) +const NEGATIVE_COLOR_RGB = { b: 69, g: 53, r: 220 }; +const POSITIVE_COLOR_RGB = { b: 69, g: 167, r: 40 }; + +/** + * Returns the green (gain) or red (loss) of a chart line as `r, g, b`, from + * its last value compared to a reference (e.g. 0 % or its first value) + */ +export function getTrendColorRgb({ + reference, + values +}: { + reference?: number; + values: (number | null | undefined)[]; +}) { + const definedValues = values.filter((value): value is number => { + return typeof value === 'number' && Number.isFinite(value); + }); + + const start = reference ?? definedValues[0]; + const end = definedValues.at(-1); + + const { b, g, r } = + start !== undefined && end !== undefined && end < start + ? NEGATIVE_COLOR_RGB + : POSITIVE_COLOR_RGB; + + return `${r}, ${g}, ${b}`; +} + export function getChartBorderColor(colorScheme?: ColorScheme) { return `rgba(${getTextColor(colorScheme)}, 0.1)`; } diff --git a/libs/ui/src/lib/benchmark/benchmark-detail-dialog/benchmark-detail-dialog.html b/libs/ui/src/lib/benchmark/benchmark-detail-dialog/benchmark-detail-dialog.html index f2291e7d1d..fc5d879ffb 100644 --- a/libs/ui/src/lib/benchmark/benchmark-detail-dialog/benchmark-detail-dialog.html +++ b/libs/ui/src/lib/benchmark/benchmark-detail-dialog/benchmark-detail-dialog.html @@ -26,6 +26,7 @@ [currency]="assetProfile?.currency" [historicalDataItems]="historicalDataItems" [isAnimated]="true" + [isTrendColored]="true" [label]="data.symbol" [locale]="data.locale" [showGradient]="true" diff --git a/libs/ui/src/lib/line-chart/line-chart.component.ts b/libs/ui/src/lib/line-chart/line-chart.component.ts index d910033bc0..2537b2e17a 100644 --- a/libs/ui/src/lib/line-chart/line-chart.component.ts +++ b/libs/ui/src/lib/line-chart/line-chart.component.ts @@ -1,6 +1,7 @@ import { getChartBorderColor, getTimeAxisOptions, + getTrendColorRgb, getValueAxisOptions, getVerticalHoverLinePlugin } from '@ghostfolio/common/chart-helper'; @@ -62,6 +63,8 @@ export class GfLineChartComponent @Input() currency?: string; @Input() historicalDataItems: LineChartItem[] | null; @Input() isAnimated = false; + /** Draws the line in green (gain) or red (loss) instead of the primary color */ + @Input() isTrendColored = false; @Input() label: string; @Input() locale = getLocale(); @Input() showGradient = false; @@ -160,10 +163,14 @@ export class GfLineChartComponent 5 ); + const lineColorRgb = this.isTrendColored + ? getTrendColorRgb({ values: marketPrices }) + : `${primaryColorRgb.r}, ${primaryColorRgb.g}, ${primaryColorRgb.b}`; + if (gradient && this.showGradient) { gradient.addColorStop( 0, - `rgba(${primaryColorRgb.r}, ${primaryColorRgb.g}, ${primaryColorRgb.b}, 0.01)` + `rgba(${lineColorRgb}, ${this.isTrendColored ? 0.15 : 0.01})` ); gradient.addColorStop(1, getBackgroundColor(this.colorScheme)); } @@ -182,7 +189,7 @@ export class GfLineChartComponent }, { backgroundColor: gradient, - borderColor: `rgb(${primaryColorRgb.r}, ${primaryColorRgb.g}, ${primaryColorRgb.b})`, + borderColor: `rgb(${lineColorRgb})`, borderWidth: 2, data: marketPrices, fill: true,