Skip to content

Commit

Permalink
Added Basket IRR Composition visual
Browse files Browse the repository at this point in the history
  • Loading branch information
praslnx8 committed Mar 30, 2024
1 parent 33c9405 commit cd1705f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/presentation/dashboard_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class DashboardPresenter extends Presenter<DashboardViewState> {
payments: payments,
value: totalValueOfInvestment,
valueUpdatedOn: DateTime.now());
final basketIrr = _calculateBasketIRR(investments);

updateViewState((viewState) {
viewState.invested = totalInvestedAmount;
Expand All @@ -59,6 +60,7 @@ class DashboardPresenter extends Presenter<DashboardViewState> {
viewState.valueOverTime = _getValueOverTime(investments);
viewState.investmentOverTime = _getInvestmentOverTime(investments);
viewState.overallIRR = overallIRR;
viewState.basketIrr = basketIrr;
});
});
}
Expand Down Expand Up @@ -122,6 +124,40 @@ class DashboardPresenter extends Presenter<DashboardViewState> {

return valueOverTime;
}

Map<String, double> _calculateBasketIRR(List<Investment> investments) {
Map<String, List<Payment>> basketPayments = {};
Map<String, double> basketValues = {};
Map<String, double> basketIRR = {};

for (var investment in investments) {
String basketName = investment.basketName ?? '-';
double investmentValue = investment.getValueOn(date: DateTime.now());

if (!basketPayments.containsKey(basketName)) {
basketPayments[basketName] = [];
basketValues[basketName] = 0;
}

final payments = investment.getPayments(till: DateTime.now());
basketPayments.update(basketName, (value) {
final existingPayments = List.of(value, growable: true);
existingPayments.addAll(payments);
return existingPayments;
}, ifAbsent: () => payments);
basketValues.update(basketName, (value) => value + investmentValue,
ifAbsent: () => investmentValue);
}

basketPayments.forEach((basketName, payments) {
double totalValue = basketValues[basketName] ?? 0;
double irr = _irrCalculator.calculateIRR(
payments: payments, value: totalValue, valueUpdatedOn: DateTime.now());
basketIRR[basketName] = irr;
});

return basketIRR;
}
}

int _getIrrCompositionKey(double irr) {
Expand All @@ -146,6 +182,7 @@ class DashboardViewState {
double overallIRR = 0;
Map<RiskLevel, double> riskComposition = {};
Map<String, double> basketComposition = {};
Map<String, double> basketIrr = {};
Map<int, double> irrComposition = {};
Map<DateTime, double> investmentOverTime = {};
Map<DateTime, double> valueOverTime = {};
Expand Down
27 changes: 27 additions & 0 deletions lib/ui/pages/dashboard_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class _DashboardPage
const SizedBox(height: AppDimen.minPadding),
_buildPieChart(snapshot.riskComposition),
const SizedBox(height: AppDimen.minPadding),
Text('IRR Composition:',
style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: AppDimen.minPadding),
_buildIrrContribution(data: snapshot.basketIrr.entries.toList()),
const SizedBox(height: AppDimen.minPadding),
Text('Investment Over Time:',
style: Theme.of(context).textTheme.titleMedium),
Expand Down Expand Up @@ -163,6 +167,29 @@ class _DashboardPage
return const Text('');
}

Widget _buildIrrContribution(
{required final List<MapEntry<String, double>> data}) {
if (data.isNotEmpty) {
data.sort((a, b) => a.value.compareTo(b.value));
List<MapEntry<String, double>> contribution = [];
for (var element in data) {
contribution.add(MapEntry(element.key, element.value));
}
return SfCartesianChart(
primaryXAxis: const CategoryAxis(),
series: [
ColumnSeries(
dataSource: contribution,
xValueMapper: (data, _) => data.key,
yValueMapper: (data, _) => data.value,
),
],
);
} else {
return const Text('');
}
}

String _getRiskLevelName(RiskLevel riskLevel) {
switch (riskLevel) {
case RiskLevel.veryLow:
Expand Down

0 comments on commit cd1705f

Please sign in to comment.