Hey so I have this this sample List data from api, and im trying to show it to fl_charts
[
{"type": "expense", "amount": 250, "paymentDate": "22-02-2022"},
{"type": "income", "amount": 350, "paymentDate": "22-02-2022"},
{"type": "expense", "amount": 150, "paymentDate": "22-02-2022"},
{"type": "expense", "amount": 450, "paymentDate": "22-03-2022"},
{"type": "income", "amount": 650, "paymentDate": "22-03-2022"},
{"type": "expense", "amount": 650, "paymentDate": "22-05-2022"},
{"type": "income", "amount": 650, "paymentDate": "22-05-2022"},
{"type": "income", "amount": 650, "paymentDate": "22-05-2022"}
]
I was able to show/reflect it on fl charts
LineChartBarData(
spots: currentScenario?.transactions
.where((element) => element['type'] == 'income')
.map((e) {
return FlSpot(
DateTime.parse(e['paymentDate']).month
as double,
e['amount']);
}).toList(),
),
LineChartBarData(
spots: currentScenario?.transactions
.where((element) => element['type'] == 'expense')
.map((e) {
return FlSpot(
DateTime.parse(e['paymentDate']).month
as double,
e['amount']);
}).toList(),
)
Now what i want to do is only show in chart the total for each month, I tried using fold but i get the total of all not within each month, I think im doing it wrong tho, any hints?
2
Answers
Copy and Pest this Code and check it:
In this code, the transactions are first filtered by type, then grouped by month using groupBy method, and then the total amount is calculated for each group using reduce method.