Exemplo n.º 1
0
        //--------------------------------------------------------------------------
        #region ** implementation

        // when the current quarter changes, update filters, total sales, and goals
        void _quarters_CurrentChanged(object sender, EventArgs e)
        {
            // update filters
            SalesByCustomer.Refresh();
            SalesByCategory.Refresh();

            // calculate sales for this quarter and for the previous quarter
            var qtrList = _quarters.Source as IList;
            var qtrThis = Quarters.CurrentItem as string;
            var qtrPrev = Quarters.CurrentPosition < qtrList.Count - 1
                ? qtrList[Quarters.CurrentPosition + 1] as string
                : string.Empty;

            _quarterSales = 0;
            decimal _prevQuarterSales = 0;

            foreach (Order o in _orders)
            {
                if (o.Quarter == qtrThis)
                {
                    _quarterSales += o.Amount;
                }
                else if (o.Quarter == qtrPrev)
                {
                    _prevQuarterSales += o.Amount;
                }
            }

            // compute sales goals (2% gold, 15% platinum)
            _goalGold = _prevQuarterSales * (decimal)1.02;
            _goalPlat = _prevQuarterSales * (decimal)1.15;

            // raise PropertyChanged event to update bindings
            OnPropertyChanged();
        }
Exemplo n.º 2
0
        // create SalesByCustomer collection (filtered by Quarter)
        void CreateSalesByCustomer()
        {
            var lsc = new List <SalesByCustomer>();

            // group by quarter
            var qByQtr =
                from o in _orders
                group o by o.Quarter into g
                select new { Quarter = g.Key, Orders = g };

            foreach (var oq in qByQtr)
            {
                // group by customer
                var qByCust =
                    from o in oq.Orders
                    group o by o.Customer into g
                    select new { Customer = g.Key, Quarter = oq.Quarter, Orders = g };
                foreach (var oc in qByCust)
                {
                    var sbc = new SalesByCustomer();
                    sbc.Customer = oc.Customer;
                    sbc.Quarter  = oc.Quarter;
                    sbc.Amount   = (from o in oc.Orders select o.Amount).Sum();
                    sbc.Scale    = Math.Max(1, Math.Sqrt((double)sbc.Amount / 1000.0));
                    lsc.Add(sbc);
                }
            }

            // create ICollectionView
            _salesByCustomer        = new CollectionViewSource();
            _salesByCustomer.Source = lsc;

            // sort by customer name
            var sd = new SortDescription("Customer.CompanyName", ListSortDirection.Ascending);

            SalesByCustomer.SortDescriptions.Add(sd);

            // filter by current Quarter
            SalesByCustomer.Filter = (item) =>
            {
                var sbc = item as SalesByCustomer;
                return(string.Equals(sbc.Quarter, (string)Quarters.CurrentItem));
            };
        }