Browse Source

Feature/color the performance charts in green or red according to the trend

pull/8292/head
mister-ideas 4 weeks ago
parent
commit
5794044e39
  1. 6
      CHANGELOG.md
  2. 15
      apps/client/src/app/components/benchmark-comparator/benchmark-comparator.component.ts
  3. 1
      apps/client/src/app/components/holding-detail-dialog/holding-detail-dialog.html
  4. 1
      apps/client/src/app/components/home-overview/home-overview.html
  5. 17
      libs/common/src/lib/chart-helper.spec.ts
  6. 30
      libs/common/src/lib/chart-helper.ts
  7. 1
      libs/ui/src/lib/benchmark/benchmark-detail-dialog/benchmark-detail-dialog.html
  8. 11
      libs/ui/src/lib/line-chart/line-chart.component.ts

6
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

15
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 {

1
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
"

1
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"

17
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');
});
});

30
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)`;
}

1
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"

11
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,

Loading…
Cancel
Save