示例#1
0
        public void UpdateChart(bool rangeFixed)
        {
            if (updating)
            {
                return;
            }
            updating = true;

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

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

                IEnumerable <HistoryDataValue> rows = this.Selection.Values;

                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;
                // the current column fills this bucket until the next column date boundary is reached
                List <HistoryDataValue> bucket = new List <HistoryDataValue>();

                foreach (HistoryDataValue t in rows)
                {
                    decimal amount = t.Value;
                    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, bucket, 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;
                        bucket = new List <HistoryDataValue>();
                    }
                    if (t.Date < start)
                    {
                        continue;
                    }
                    total += amount;
                    bucket.Add(t);
                }
                if (total != 0)
                {
                    AddColumn(start, range, total, bucket, brush);
                }
                while (collection.Count > maxColumns)
                {
                    collection.RemoveAt(0);
                }

                ComputeLinearRegression();

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

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