Esempio n. 1
0
        public void UpdateChart()
        {
            if (!this.IsVisible || LoanPayements == null)
            {
                return;
            }

            try
            {
                decimal totalPrincipal = 0;
                decimal totalInterest  = 0;

                Dictionary <int, Payment> cumulatedPayementsPerYear = CumulatePayementsPerYear();

                collection.Clear();

                foreach (Payment payment in cumulatedPayementsPerYear.Values)
                {
                    collection.Add(
                        new Payment()
                    {
                        Principal = Math.Abs(payment.Principal),
                        Interest  = Math.Abs(payment.Interest),
                        Label     = payment.Label
                    }
                        );

                    totalPrincipal += payment.Principal;
                    totalInterest  += payment.Interest;
                }

                SeriePrincipal.Title = string.Format("Principal {0:C}", Math.Abs(totalPrincipal));
                SerieInterest.Title  = string.Format("Interest {0:C}", Math.Abs(totalInterest));

                AreaChart.InvalidateArrange();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
            }
        }
Esempio n. 2
0
        public void UpdateChart(bool rangeFixed)
        {
            if (updating)
            {
                return;
            }
            updating = true;

            try
            {
                SeriesAmount.ItemsSource = null;
                collection.Clear();

                if (this.Selection == null)
                {
                    return;
                }

                IEnumerable <Transaction> rows = this.Selection.Transactions;

                if (rows == null || !this.IsVisible)
                {
                    return;
                }

                HistoryRange range = HistoryRange.Year;
                if (this.RangeCombo.SelectedItem != null)
                {
                    string value = this.RangeCombo.SelectedItem.ToString();
                    Enum.TryParse <HistoryRange>(value, out range);
                }

                Brush brush = this.Selection.Brush;
                if (brush == null)
                {
                    brush = Brushes.DarkSlateBlue;
                }

                TimeSpan dateRange = ComputeChartParameters(rows);
                if (!rangeFixed)
                {
                    if (dateRange <= TimeSpan.FromDays(31))
                    {
                        range = HistoryRange.Day;
                        this.RangeCombo.SelectedItem = range;
                    }
                    else if (dateRange <= TimeSpan.FromDays(365))
                    {
                        range = HistoryRange.Month;
                        this.RangeCombo.SelectedItem = range;
                    }
                    else
                    {
                        range = HistoryRange.Year;
                        this.RangeCombo.SelectedItem = range;
                    }
                }

                DateTime startDate  = DateTime.Now;
                int      maxColumns = 20;
                if (range == HistoryRange.Month)
                {
                    maxColumns = 24;
                    startDate  = this.selection.EndDate.AddMonths(-maxColumns);
                }
                else if (range == HistoryRange.Day)
                {
                    maxColumns = 31;
                    startDate  = this.selection.EndDate.AddDays(-maxColumns);
                }
                else
                {
                    maxColumns = 20;
                    startDate  = this.selection.EndDate.AddYears(-maxColumns);
                }

                decimal            total        = 0;
                DateTime           start        = DateTime.MinValue;
                List <Transaction> transactions = new List <Transaction>();

                foreach (Transaction t in rows)
                {
                    if (t.Transfer != null)
                    {
                        // skip transfers.
                        continue;
                    }
                    if (t.Date < startDate)
                    {
                        continue;
                    }
                    decimal amount = t.Amount;
                    if (invert)
                    {
                        amount = -amount;
                    }
                    DateTime td = t.Date;
                    while (start == DateTime.MinValue || start.Year < td.Year ||
                           (range == HistoryRange.Month && start.Month < td.Month) ||
                           (range == HistoryRange.Day && start.Day < td.Day))
                    {
                        if (start != DateTime.MinValue)
                        {
                            AddColumn(start, range, total, transactions, brush);
                        }
                        if (start == DateTime.MinValue)
                        {
                            start = new DateTime(td.Year, (range == HistoryRange.Month || range == HistoryRange.Day) ? td.Month : 1,
                                                 range == HistoryRange.Day ? td.Day : 1);
                        }
                        else
                        {
                            switch (range)
                            {
                            case HistoryRange.All:
                            case HistoryRange.Year:
                                start = start.AddYears(1);
                                break;

                            case HistoryRange.Month:
                                start = start.AddMonths(1);
                                break;

                            case HistoryRange.Day:
                                start = start.AddDays(1);
                                break;
                            }
                        }
                        total        = 0;
                        transactions = new List <Transaction>();
                    }
                    total += amount;
                    transactions.Add(t);
                }
                if (total != 0)
                {
                    AddColumn(start, range, total, transactions, brush);
                }
                while (collection.Count > maxColumns)
                {
                    collection.RemoveAt(0);
                }

                ComputeLinearRegression();

                ObservableCollection <HistoryChartColumn> copy = new ObservableCollection <HistoryChartColumn>(this.collection);

                SeriesAmount.ItemsSource  = copy;
                AverageSeries.ItemsSource = copy;
                AreaChart.InvalidateArrange();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
            }
            finally
            {
                updating = false;
            }
        }