Improved stacked graph

This commit is contained in:
2024-02-11 22:49:57 +01:00
parent cbaf94d866
commit a51ca14041
5 changed files with 118 additions and 24 deletions

View File

@@ -1,6 +1,7 @@
import 'package:equatable/equatable.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:tunas/domains/charts/models/month_totals.dart';
import 'package:tunas/domains/transaction/models/transaction_line.dart';
import 'package:tunas/domains/charts/models/chart_item.dart';
import 'package:tunas/repositories/account/account_repository.dart';
@@ -100,7 +101,10 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
List<ChartItem> scopedCategoriesPositiveTotals = [];
List<ChartItem> scopedCategoriesNegativeTotals = [];
Map<int, FlSpot> scopedMonthlyTotals = {};
Map<int, Map<String, double>> scopedCategoriesMonthlyTotals = {};
Map<int, MonthTotals> scopedCategoriesMonthlyTotals = {};
Map<int, double> scopedMonthlyPostitiveTotals = {};
Map<int, double> scopedMonthlyNegativeTotals = {};
for(var transaction in state.transactions) {
globalTotal += transaction.value;
@@ -123,6 +127,12 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
continue;
}
MonthTotals? categoryMonthTotal = scopedCategoriesMonthlyTotals[transaction.date.month];
if (categoryMonthTotal == null) {
categoryMonthTotal = MonthTotals(negatives: {}, positives: {});
scopedCategoriesMonthlyTotals[transaction.date.month] = categoryMonthTotal;
}
if (transaction.value >= 0) {
ChartItem? chartItem = scopedCategoriesPositiveTotals.firstWhere(
(item) => item.label == transaction.category,
@@ -133,6 +143,9 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
}
);
chartItem.value += transaction.value;
scopedMonthlyPostitiveTotals[transaction.date.month] = transaction.value + (scopedMonthlyPostitiveTotals[transaction.date.month] ?? 0);
categoryMonthTotal.positives[transaction.category] = transaction.value.abs() + (categoryMonthTotal.positives[transaction.category] ?? 0);
}
if (transaction.value < 0) {
@@ -146,13 +159,8 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
);
chartItem.value += transaction.value;
Map<String, double>? a = scopedCategoriesMonthlyTotals[transaction.date.month];
if (scopedCategoriesMonthlyTotals[transaction.date.month] == null) {
a = {};
}
a?[transaction.category] = transaction.value.abs() + (a[transaction.category] ?? 0);
scopedCategoriesMonthlyTotals[transaction.date.month] = a!;
scopedMonthlyNegativeTotals[transaction.date.month] = transaction.value + (scopedMonthlyPostitiveTotals[transaction.date.month] ?? 0);
categoryMonthTotal.negatives[transaction.category] = transaction.value.abs() + (categoryMonthTotal.negatives[transaction.category] ?? 0);
}
}
@@ -214,6 +222,8 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
scopedSimplifiedCategoriesNegativeTotalsPercents: scopedSimplifiedCategoriesNegativeTotalsPercents,
scopedMonthlyTotals: scopedMonthlyTotals.values.toList(),
scopedCategoriesMonthlyTotals: scopedCategoriesMonthlyTotals,
scopedMonthlyPostitiveTotals: scopedMonthlyPostitiveTotals,
scopedMonthlyNegativeTotals: scopedMonthlyNegativeTotals,
);
}
}

View File

@@ -27,7 +27,9 @@ final class ChartState extends Equatable {
final List<ChartItem> scopedSimplifiedCategoriesNegativeTotalsPercents;
final List<FlSpot> scopedMonthlyTotals;
final Map<int, Map<String, double>> scopedCategoriesMonthlyTotals;
final Map<int, MonthTotals> scopedCategoriesMonthlyTotals;
final Map<int, double> scopedMonthlyPostitiveTotals;
final Map<int, double> scopedMonthlyNegativeTotals;
final double scoppedProfit;
@@ -53,6 +55,8 @@ final class ChartState extends Equatable {
this.scopedMonthlyTotals = const [],
this.scopedCategoriesMonthlyTotals = const {},
this.scoppedProfit = 0,
this.scopedMonthlyPostitiveTotals = const {},
this.scopedMonthlyNegativeTotals = const {},
});
ChartState copyWith({
@@ -75,8 +79,10 @@ final class ChartState extends Equatable {
List<ChartItem>? scopedSimplifiedCategoriesNegativeTotals,
List<ChartItem>? scopedSimplifiedCategoriesNegativeTotalsPercents,
List<FlSpot>? scopedMonthlyTotals,
Map<int, Map<String, double>>? scopedCategoriesMonthlyTotals,
Map<int, MonthTotals>? scopedCategoriesMonthlyTotals,
double? scoppedProfit,
Map<int, double>? scopedMonthlyPostitiveTotals,
Map<int, double>? scopedMonthlyNegativeTotals,
}) {
return ChartState(
transactions: transactions ?? this.transactions,
@@ -100,6 +106,8 @@ final class ChartState extends Equatable {
scopedMonthlyTotals: scopedMonthlyTotals ?? this.scopedMonthlyTotals,
scopedCategoriesMonthlyTotals: scopedCategoriesMonthlyTotals ?? this.scopedCategoriesMonthlyTotals,
scoppedProfit: scoppedProfit ?? this.scoppedProfit,
scopedMonthlyPostitiveTotals: scopedMonthlyPostitiveTotals ?? this.scopedMonthlyPostitiveTotals,
scopedMonthlyNegativeTotals: scopedMonthlyNegativeTotals ?? this.scopedMonthlyNegativeTotals,
);
}
@@ -124,6 +132,8 @@ final class ChartState extends Equatable {
scopedMonthlyTotals,
scopedCategoriesMonthlyTotals,
scoppedProfit,
scopedMonthlyPostitiveTotals,
scopedMonthlyNegativeTotals,
];
}

View File

@@ -0,0 +1,29 @@
class MonthTotals {
Map<String, double> positives;
Map<String, double> negatives;
MonthTotals({
required this.positives,
required this.negatives,
});
double maxValue() {
double max = 0.0;
if (positives.isNotEmpty) {
double localMax = positives.values.reduce((value, element) => value + element);
if (localMax > max) {
max = localMax;
}
}
if (negatives.isNotEmpty) {
double localMax2 = negatives.values.reduce((value, element) => value + element);
if (localMax2 > max) {
max = localMax2;
}
}
return max;
}
}