1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { PieChart } from 'react-native-gifted-charts';
import { Colors, Radius, Spacing, Typography } from '@/constants/theme';
import type { DashboardExpenseItem } from '@/types';
import { Card, SectionLabel } from './primitives';
interface ExpensesChartProps {
recurring: DashboardExpenseItem[];
oneTime: DashboardExpenseItem[];
}
const CATEGORY_COLORS: Record<string, string> = {
moradia: '#4C6EF5',
alimentação: '#FA5252',
transporte: '#FAB005',
saúde: '#12B886',
educação: '#BE4BDB',
lazer: '#FF922B',
serviços: '#228BE6',
outros: '#868E96',
};
const CATEGORY_LABELS: Record<string, string> = {
moradia: 'Moradia',
alimentação: 'Alimentação',
transporte: 'Transporte',
saúde: 'Saúde',
educação: 'Educação',
lazer: 'Lazer',
serviços: 'Serviços',
outros: 'Outros',
};
export function ExpensesChart({ recurring, oneTime }: ExpensesChartProps) {
const categoryTotals: Record<string, number> = {};
const allExpenses = [
...recurring.filter(e => e.is_active && e.type === 'expense'),
...oneTime.filter(e => e.type === 'expense')
];
if (allExpenses.length === 0) {
return (
<Card style={styles.card}>
<SectionLabel text="Análise de Gastos" />
<View style={styles.emptyContainer}>
<Text style={styles.emptyText}>Não há despesas para analisar neste mês.</Text>
</View>
</Card>
);
}
let totalExpenses = 0;
allExpenses.forEach(e => {
if (!categoryTotals[e.category]) {
categoryTotals[e.category] = 0;
}
categoryTotals[e.category] += e.amount;
totalExpenses += e.amount;
});
const pieData = Object.keys(categoryTotals)
.map(category => ({
value: categoryTotals[category],
color: CATEGORY_COLORS[category] || CATEGORY_COLORS.outros,
label: CATEGORY_LABELS[category] || 'Outros',
percentage: ((categoryTotals[category] / totalExpenses) * 100).toFixed(0) + '%',
}))
.sort((a, b) => b.value - a.value);
return (
<Card style={styles.card}>
<SectionLabel text="Análise de Gastos" />
<View style={styles.chartRow}>
<View style={styles.chartWrapper}>
<PieChart
data={pieData}
donut
radius={70}
innerRadius={50}
innerCircleColor={Colors.surface}
centerLabelComponent={() => {
return (
<View style={styles.centerLabel}>
<Text style={styles.centerLabelValue}>
{totalExpenses.toLocaleString('pt-BR', {
style: 'currency',
currency: 'BRL',
minimumFractionDigits: 0,
maximumFractionDigits: 0,
})}
</Text>
<Text style={styles.centerLabelText}>Total</Text>
</View>
);
}}
/>
</View>
<View style={styles.legendContainer}>
{pieData.map((item, index) => (
<View key={index} style={styles.legendItem}>
<View style={[styles.legendDot, { backgroundColor: item.color }]} />
<View style={styles.legendTextWrapper}>
<Text style={styles.legendLabel} numberOfLines={1}>{item.label}</Text>
<Text style={styles.legendValue}>
{item.value.toLocaleString('pt-BR', {
style: 'currency',
currency: 'BRL',
})} ({item.percentage})
</Text>
</View>
</View>
))}
</View>
</View>
</Card>
);
}
const styles = StyleSheet.create({
card: {
gap: Spacing.md,
},
emptyContainer: {
paddingVertical: Spacing.xl,
alignItems: 'center',
justifyContent: 'center',
},
emptyText: {
...Typography.body,
color: Colors.textMuted,
},
chartRow: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
},
chartWrapper: {
alignItems: 'center',
justifyContent: 'center',
},
centerLabel: {
alignItems: 'center',
justifyContent: 'center',
},
centerLabelValue: {
...Typography.body,
fontFamily: 'DMSans_700Bold',
color: Colors.textPrimary,
fontSize: 14,
},
centerLabelText: {
...Typography.caption,
color: Colors.textSecondary,
fontSize: 10,
},
legendContainer: {
flex: 1,
marginLeft: Spacing.lg,
gap: Spacing.sm,
},
legendItem: {
flexDirection: 'row',
alignItems: 'center',
},
legendDot: {
width: 10,
height: 10,
borderRadius: 5,
marginRight: Spacing.xs,
},
legendTextWrapper: {
flex: 1,
},
legendLabel: {
...Typography.caption,
color: Colors.textSecondary,
},
legendValue: {
...Typography.caption,
color: Colors.textPrimary,
fontFamily: 'DMSans_600SemiBold',
},
});
|