Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#2575): Stop cash flow card labels from getting cutting off if bar height is too low #2597

Merged
merged 10 commits into from
Apr 16, 2024
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState, useMemo, useCallback } from 'react';

import { Bar, BarChart, ResponsiveContainer } from 'recharts';
import { Bar, BarChart, LabelList, ResponsiveContainer } from 'recharts';

import * as monthUtils from 'loot-core/src/shared/months';
import { integerToCurrency } from 'loot-core/src/shared/util';
Expand All @@ -17,23 +17,48 @@ import { ReportCard } from '../ReportCard';
import { simpleCashFlow } from '../spreadsheets/cash-flow-spreadsheet';
import { useReport } from '../useReport';

function CustomLabel({ value, name, position, ...props }) {
function CustomLabel({
value,
name,
position,
x,
y,
width: barWidth,
height: barHeight,
}) {
const valueLengthOffset = value.toString().length < 5 ? -40 : 20;

const yOffset = barHeight < 25 ? 105 : y;

const labelXOffsets = {
right: 6,
left: -valueLengthOffset + 1,
};

const valueXOffsets = {
right: 6,
left: -valueLengthOffset + 2,
};

const anchorValue = {
right: 'start',
left: 'end',
};

return (
<>
<text
{...props}
dy={10}
dx={position === 'right' ? 20 : -5}
textAnchor={position === 'right' ? 'start' : 'end'}
x={x + barWidth + labelXOffsets[position]}
y={yOffset + 10}
textAnchor={anchorValue[position]}
fill={theme.tableText}
>
{name}
</text>
<text
{...props}
dy={26}
dx={position === 'right' ? 20 : -4}
textAnchor={position === 'right' ? 'start' : 'end'}
x={x + barWidth + valueXOffsets[position]}
y={yOffset + 26}
textAnchor={anchorValue[position]}
fill={theme.tableText}
>
<PrivacyFilter>{integerToCurrency(value)}</PrivacyFilter>
Expand Down Expand Up @@ -100,18 +125,20 @@ export function CashFlowCard() {
bottom: 0,
}}
>
<Bar
dataKey="income"
fill={chartTheme.colors.blue}
barSize={14}
label={<CustomLabel name="Income" position="left" />}
/>
<Bar
dataKey="expenses"
fill={chartTheme.colors.red}
barSize={14}
label={<CustomLabel name="Expenses" position="right" />}
/>
<Bar dataKey="income" fill={chartTheme.colors.blue} barSize={14}>
<LabelList
dataKey="income"
position="left"
content={<CustomLabel name="Income" />}
/>
</Bar>
<Bar dataKey="expenses" fill={chartTheme.colors.red} barSize={14}>
<LabelList
dataKey="expenses"
position="right"
content={<CustomLabel name="Expenses" />}
/>
</Bar>
</BarChart>
</ResponsiveContainer>
) : (
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/2597.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [ttlgeek]
---

Stop cash flow card labels from getting cutting off if bar height is too low
Loading