Exemplo n.º 1
0
        private void OnModelChanged()
        {
            DateTime startDate;
            DateTime endDate;

            switch (this.Model.Grouping)
            {
            case Grouping.Day:
                startDate = this.Model.StartDate;
                endDate   = this.Model.EndDate;
                break;

            case Grouping.Week:
                startDate = this.Model.StartDate.ToStartOfWeek();
                endDate   = this.Model.EndDate.ToEndOfWeek();
                break;

            case Grouping.Month:
                startDate = this.Model.StartDate.ToStartOfMonth();
                endDate   = this.Model.EndDate.ToEndOfMonth();
                break;

            case Grouping.Year:
                startDate = this.Model.StartDate.ToStartOfYear();
                endDate   = this.Model.EndDate.ToEndOfYear();
                break;

            default:
                throw new NotImplementedException();
            }

            var itemsSource = new List <InvoiceByGroupData>();

            var currentDate = startDate;

            while (currentDate.Date <= endDate)
            {
                var data = new InvoiceByGroupData();

                data.Start = currentDate;

                switch (this.Model.Grouping)
                {
                case Grouping.Day:
                    data.Name   = currentDate.Date.ToString("M/d");
                    data.End    = currentDate.Date;
                    currentDate = currentDate.Date.AddDays(1);
                    break;

                case Grouping.Week:
                    data.Name   = currentDate.Date.ToString("M/d");
                    data.End    = currentDate.ToEndOfWeek();
                    currentDate = currentDate.ToStartOfNextWeek();
                    break;

                case Grouping.Month:
                    data.Name   = currentDate.Date.ToString("M/yyyy");
                    data.End    = currentDate.ToEndOfMonth();
                    currentDate = currentDate.ToStartOfNextMonth();
                    break;

                case Grouping.Year:
                    data.Name   = currentDate.Date.ToString("yyyy");
                    data.End    = currentDate.ToEndOfYear();
                    currentDate = currentDate.ToStartOfNextYear();
                    break;

                default:
                    break;
                }

                data.Amount = this.Model.FilteredInvoices
                              .Where(row => row.PreparedDate >= data.Start &&
                                     row.PreparedDate <= data.End)
                              .Sum(row => row.Amount);

                itemsSource.Add(data);
            }

            this.Chart.Series.Clear();

            LineSeries series;

            series = new LineSeries();

            series.Title = "Invoices";
            series.DependentValuePath   = "Amount";
            series.IndependentValuePath = "Name";
            series.ItemsSource          = itemsSource;
            series.DataPointStyle       = (Style)Application.Current.Resources["LineDataPointStyle"];

            this.Chart.Title = string.Format("Revenues ({0:N0})", itemsSource.Sum(row => row.Amount));

            this.Chart.Series.Add(series);
        }
        private void OnModelChanged()
        {
            DateTime startDate;
            DateTime endDate;

            switch (this.Model.Grouping)
            {
                case Grouping.Day:
                    startDate = this.Model.StartDate;
                    endDate = this.Model.EndDate;
                    break;
                case Grouping.Week:
                    startDate = this.Model.StartDate.ToStartOfWeek();
                    endDate = this.Model.EndDate.ToEndOfWeek();
                    break;
                case Grouping.Month:
                    startDate = this.Model.StartDate.ToStartOfMonth();
                    endDate = this.Model.EndDate.ToEndOfMonth();
                    break;
                case Grouping.Year:
                    startDate = this.Model.StartDate.ToStartOfYear();
                    endDate = this.Model.EndDate.ToEndOfYear();
                    break;
                default:
                    throw new NotImplementedException();
            }

            var itemsSource = new List<InvoiceByGroupData>();

            var currentDate = startDate;

            while (currentDate.Date <= endDate)
            {
                var data = new InvoiceByGroupData();

                data.Start = currentDate;

                switch (this.Model.Grouping)
                {
                    case Grouping.Day:
                        data.Name = currentDate.Date.ToString("M/d");
                        data.End = currentDate.Date;
                        currentDate = currentDate.Date.AddDays(1);
                        break;
                    case Grouping.Week:
                        data.Name = currentDate.Date.ToString("M/d");
                        data.End = currentDate.ToEndOfWeek();
                        currentDate = currentDate.ToStartOfNextWeek();
                        break;
                    case Grouping.Month:
                        data.Name = currentDate.Date.ToString("M/yyyy");
                        data.End = currentDate.ToEndOfMonth();
                        currentDate = currentDate.ToStartOfNextMonth();
                        break;
                    case Grouping.Year:
                        data.Name = currentDate.Date.ToString("yyyy");
                        data.End = currentDate.ToEndOfYear();
                        currentDate = currentDate.ToStartOfNextYear();
                        break;
                    default:
                        break;
                }

                data.Amount = this.Model.FilteredInvoices
                    .Where(row => row.PreparedDate >= data.Start
                                  && row.PreparedDate <= data.End)
                    .Sum(row => row.Amount);

                itemsSource.Add(data);
            }

            this.Chart.Series.Clear();

            LineSeries series;

            series = new LineSeries();

            series.Title = "Invoices";
            series.DependentValuePath = "Amount";
            series.IndependentValuePath = "Name";
            series.ItemsSource = itemsSource;
            series.DataPointStyle = (Style)Application.Current.Resources["LineDataPointStyle"];

            this.Chart.Title = string.Format("Revenues ({0:N0})", itemsSource.Sum(row => row.Amount));

            this.Chart.Series.Add(series);
        }