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

✨ (dashboards) piecharts - sorting and label spacing #3855

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.