Skip to content

A local-first personal finance app

:sparkles: (dashboards) piecharts - sorting and label spacing (#3855)

Matiss Janis Aboltinsfa8ff79208f8
diff --git a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-1-chromium-linux.png b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-1-chromium-linux.png
index 2f01d4b21a..8b6fb7dfc0 100644
Binary files a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-1-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-1-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-2-chromium-linux.png b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-2-chromium-linux.png
index 2619303ee1..e821884d43 100644
Binary files a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-2-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-2-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-3-chromium-linux.png b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-3-chromium-linux.png
index e91f7df9a8..986929ddd5 100644
Binary files a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-3-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-3-chromium-linux.png differ
diff --git a/packages/desktop-client/src/components/reports/ChooseGraph.tsx b/packages/desktop-client/src/components/reports/ChooseGraph.tsx
index e477e6e349..a743ac8446 100644
--- a/packages/desktop-client/src/components/reports/ChooseGraph.tsx
+++ b/packages/desktop-client/src/components/reports/ChooseGraph.tsx
@@ -137,7 +137,6 @@ export function ChooseGraph({
return (
<DonutGraph
style={graphStyle}
- compact={compact}
data={data}
filters={filters}
groupBy={groupBy}
diff --git a/packages/desktop-client/src/components/reports/graphs/DonutGraph.tsx b/packages/desktop-client/src/components/reports/graphs/DonutGraph.tsx
index 42d3dcb22b..4dae3d6d88 100644
--- a/packages/desktop-client/src/components/reports/graphs/DonutGraph.tsx
+++ b/packages/desktop-client/src/components/reports/graphs/DonutGraph.tsx
@@ -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>
@@ -184,7 +206,6 @@ type DonutGraphProps = {
filters: RuleConditionEntity[];
groupBy: string;
balanceTypeOp: balanceTypeOpType;
- compact?: boolean;
viewLabels: boolean;
showHiddenCategories?: boolean;
showOffBudget?: boolean;
@@ -197,7 +218,6 @@ export function DonutGraph({
filters,
groupBy,
balanceTypeOp,
- compact,
viewLabels,
showHiddenCategories,
showOffBudget,
@@ -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 {
@@ -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>
);
}
diff --git a/upcoming-release-notes/3855.md b/upcoming-release-notes/3855.md
new file mode 100644
index 0000000000..028980feef
--- /dev/null
+++ b/upcoming-release-notes/3855.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [MatissJanis]
+---
+
+Dashboards: sort pie-chart according to balances; add more spacing for labels.