Skip to content

Commit

Permalink
fix(actualbudget#2575): Stop cash flow card labels from getting cutti…
Browse files Browse the repository at this point in the history
…ng off if bar height is too low
  • Loading branch information
ttlgeek committed Apr 13, 2024
1 parent 9aeab0f commit 0ea19e9
Showing 1 changed file with 50 additions and 22 deletions.
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,43 @@ 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 xOffsets = {
right: 5,
left: -valueLengthOffset,
};

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

return (
<>
<text
{...props}
dy={10}
dx={position === 'right' ? 20 : -5}
textAnchor={position === 'right' ? 'start' : 'end'}
x={x + barWidth + xOffsets[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 + xOffsets[position]}
y={yOffset + 25}
textAnchor={anchorValue[position]}
fill={theme.tableText}
>
<PrivacyFilter>{integerToCurrency(value)}</PrivacyFilter>
Expand Down Expand Up @@ -100,18 +120,26 @@ 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"
content={props =>
CustomLabel({ ...props, name: 'Income', position: 'left' })
}
/>
</Bar>
<Bar dataKey="expenses" fill={chartTheme.colors.red} barSize={14}>
<LabelList
dataKey="expenses"
content={props =>
CustomLabel({
...props,
name: 'Expenses',
position: 'right',
})
}
/>
</Bar>
</BarChart>
</ResponsiveContainer>
) : (
Expand Down

0 comments on commit 0ea19e9

Please sign in to comment.