void CheckForMaxValue(BasicMonthlyBarchart chart)
        {
            var localMax = this.GetLocalMax(GetLast(chart.Bars));

            if (chart.MaxValue < localMax)
            {
                chart.MaxValue = localMax;
            }
        }
        public BasicMonthlyBarchart Get(DateTime from, DateTime to)
        {
            // Format dates
            from = new DateTime(from.Year, from.Month, 1);
            to   = new DateTime(to.Year, to.Month + 1, 1).AddDays(-1);

            // Init return value
            var chart = new BasicMonthlyBarchart();

            chart.From     = from;
            chart.To       = to;
            chart.MaxValue = 0;

            // Get transactions
            var transactions = new TransactionRepository().Get().ToList();

            if (transactions.Count == 0)
            {
                return(chart);
            }

            // Order transactions
            transactions = transactions.OrderBy(x => x.Date).ToList();

            // Fill up starting dates (if needed)
            this.FillInDateGaps(chart.Bars, from, transactions.First().Date.Value.AddMonths(-1));

            foreach (var tr in transactions)
            {
                if (tr.Date < chart.From || tr.Date > chart.To)
                {
                    continue;
                }

                if (tr.Date > chart.To)
                {
                    break;
                }

                BasicMonthlyBar currentMonthBar = null;

                if (chart.Bars.Count > 0)
                {
                    currentMonthBar = GetLast(chart.Bars);
                }

                if (currentMonthBar == null || !this.IsSameMonth(currentMonthBar.Date, tr.Date.Value))
                {
                    // Check for last item's localmax
                    this.CheckForMaxValue(chart);

                    // Fill in date gaps
                    if (currentMonthBar != null) // validated with !IsSameMonth
                    {
                        this.FillInDateGaps(chart.Bars, currentMonthBar.Date.AddMonths(1), tr.Date.Value.AddMonths(-1));
                    }

                    // New item
                    currentMonthBar      = new BasicMonthlyBar();
                    currentMonthBar.Date = new DateTime(tr.Date.Value.Year, tr.Date.Value.Month, 1);

                    // Add
                    chart.Bars.Add(currentMonthBar);
                }

                currentMonthBar.Flow += tr.Sum.Value;

                if (tr.Sum.Value < 0)
                {
                    currentMonthBar.Expense += tr.Sum.Value;
                }
                else
                {
                    currentMonthBar.Income += tr.Sum.Value;
                }

                currentMonthBar.Transactions.Add(tr);
            }

            this.CheckForMaxValue(chart);

            // Fill up ending dates (if needed)
            this.FillInDateGaps(chart.Bars, GetLast(chart.Bars).Date.AddMonths(1), to);

            return(chart);
        }