Skip to content

Commit

Permalink
✨ (dashboards) piecharts - sorting and label spacing (actualbudget#3855)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatissJanis authored Dec 7, 2024
1 parent 3ce7ae9 commit fa8ff79
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 70 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ export function ChooseGraph({
return (
<DonutGraph
style={graphStyle}
compact={compact}
data={data}
filters={filters}
groupBy={groupBy}
Expand Down
169 changes: 100 additions & 69 deletions packages/desktop-client/src/components/reports/graphs/DonutGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,38 @@ const ActiveShapeMobile = props => {
} = props;
const yAxis = payload.name ?? payload.date;

const sin = Math.sin(-RADIAN * 240);
const my = cy + outerRadius * sin;
const ey = my - 5;

return (
<g>
<text x={cx} y={cy + 70} dy={-8} textAnchor="middle" fill={fill}>
<text
x={cx}
y={cy + outerRadius * Math.sin(-RADIAN * 270) + 15}
dy={0}
textAnchor="middle"
fill={fill}
>
{`${yAxis}`}
</text>
<PrivacyFilter>
<text x={cx - 40} y={cy + 40} dy={0} textAnchor="end" fill={fill}>
<text
x={cx + outerRadius * Math.cos(-RADIAN * 240) - 30}
y={ey}
dy={0}
textAnchor="end"
fill={fill}
>
{`${amountToCurrency(value)}`}
</text>
<text x={cx + 45} y={cy + 40} dy={0} textAnchor="start" fill="#999">
<text
x={cx + outerRadius * Math.cos(-RADIAN * 330) + 10}
y={ey}
dy={0}
textAnchor="start"
fill="#999"
>
{`${(percent * 100).toFixed(2)}%`}
</text>
</PrivacyFilter>
Expand Down Expand Up @@ -184,7 +206,6 @@ type DonutGraphProps = {
filters: RuleConditionEntity[];
groupBy: string;
balanceTypeOp: balanceTypeOpType;
compact?: boolean;
viewLabels: boolean;
showHiddenCategories?: boolean;
showOffBudget?: boolean;
Expand All @@ -197,7 +218,6 @@ export function DonutGraph({
filters,
groupBy,
balanceTypeOp,
compact,
viewLabels,
showHiddenCategories,
showOffBudget,
Expand All @@ -211,7 +231,7 @@ export function DonutGraph({
const accounts = useAccounts();
const [pointer, setPointer] = useState('');

const getVal = obj => {
const getVal = (obj: DataEntity) => {
if (['totalDebts', 'netDebts'].includes(balanceTypeOp)) {
return -1 * obj[balanceTypeOp];
} else {
Expand All @@ -221,71 +241,82 @@ export function DonutGraph({

const [activeIndex, setActiveIndex] = useState(0);

// Sort the data in the pie chart
const unsortedData = data[splitData];
const sortedData = unsortedData.slice().sort((a, b) => getVal(b) - getVal(a));

return (
<Container
style={{
...style,
...(compact && { height: 'auto' }),
}}
>
{(width, height) =>
data[splitData] && (
<ResponsiveContainer>
<div>
{!compact && <div style={{ marginTop: '15px' }} />}
<PieChart
width={width}
height={height}
style={{ cursor: pointer }}
>
<Pie
activeIndex={activeIndex}
activeShape={compact ? ActiveShapeMobile : ActiveShape}
dataKey={val => getVal(val)}
nameKey={yAxis}
isAnimationActive={false}
data={data[splitData]}
innerRadius={Math.min(width, height) * 0.2}
fill="#8884d8"
labelLine={false}
label={e =>
viewLabels && !compact ? customLabel(e) : <div />
}
onMouseLeave={() => setPointer('')}
onMouseEnter={(_, index) => {
setActiveIndex(index);
if (!['Group', 'Interval'].includes(groupBy)) {
setPointer('pointer');
}
}}
onClick={item =>
((compact && showTooltip) || !compact) &&
!['Group', 'Interval'].includes(groupBy) &&
showActivity({
navigate,
categories,
accounts,
balanceTypeOp,
filters,
showHiddenCategories,
showOffBudget,
type: 'totals',
startDate: data.startDate,
endDate: data.endDate,
field: groupBy.toLowerCase(),
id: item.id,
})
}
<Container style={style}>
{(width, height) => {
const compact = height <= 300 || width <= 300;

return (
sortedData && (
<ResponsiveContainer>
<div>
{!compact && <div style={{ marginTop: '15px' }} />}
<PieChart
width={width}
height={height}
style={{ cursor: pointer }}
>
{data.legend.map((entry, index) => (
<Cell key={`cell-${index}`} fill={entry.color} />
))}
</Pie>
</PieChart>
</div>
</ResponsiveContainer>
)
}
<Pie
activeIndex={activeIndex}
activeShape={
width < 220 || height < 130
? undefined
: compact
? ActiveShapeMobile
: ActiveShape
}
dataKey={val => getVal(val)}
nameKey={yAxis}
isAnimationActive={false}
data={sortedData}
innerRadius={Math.min(width, height) * 0.2}
fill="#8884d8"
labelLine={false}
label={e =>
viewLabels && !compact ? customLabel(e) : <div />
}
startAngle={90}
endAngle={-270}
onMouseLeave={() => setPointer('')}
onMouseEnter={(_, index) => {
setActiveIndex(index);
if (!['Group', 'Interval'].includes(groupBy)) {
setPointer('pointer');
}
}}
onClick={item =>
((compact && showTooltip) || !compact) &&
!['Group', 'Interval'].includes(groupBy) &&
showActivity({
navigate,
categories,
accounts,
balanceTypeOp,
filters,
showHiddenCategories,
showOffBudget,
type: 'totals',
startDate: data.startDate,
endDate: data.endDate,
field: groupBy.toLowerCase(),
id: item.id,
})
}
>
{data.legend.map((entry, index) => (
<Cell key={`cell-${index}`} fill={entry.color} />
))}
</Pie>
</PieChart>
</div>
</ResponsiveContainer>
)
);
}}
</Container>
);
}
6 changes: 6 additions & 0 deletions upcoming-release-notes/3855.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [MatissJanis]
---

Dashboards: sort pie-chart according to balances; add more spacing for labels.

0 comments on commit fa8ff79

Please sign in to comment.