classlib/ExpenseTracker.Application/Accounts/Queries/Charts/DailyTransactionsBarChart.cs
2024-08-07 21:12:02 +03:00

40 lines
1.1 KiB
C#

namespace ExpenseTracker.Application.Accounts.Queries.Charts;
public class DailyTransactionsBarChart
{
public DailyTransactionsBarChart(IEnumerable<IGrouping<DateTime, double>> groupings, DateOnly fromDate, DateOnly toDate)
{
var days = toDate.DayNumber - fromDate.DayNumber;
var dateAmount = new Dictionary<DateOnly, double>(days);
for (int i = 0; i < days; i++)
{
dateAmount.Add(fromDate.AddDays(i), 0);
}
foreach (var group in groupings)
{
var date = DateOnly.FromDateTime(group.Key);
var amount = group.Aggregate(0.0, (a, x) => a + x);
dateAmount[date] = amount;
}
Dates = dateAmount.Keys.ToList();
Amounts = dateAmount.Values.ToList();
}
public double MinimumAmount => Amounts.Min();
public double MaximumAmount => Amounts.Max();
public double AverageAmount => Double.Round(TotalAmount / Amounts.Count(), 4);
public double TotalAmount => Amounts.Aggregate(0.0, (a, x) => a +x);
public IEnumerable<DateOnly> Dates { get; set; }
public IEnumerable<double> Amounts { get; set; }
}