public GroupInfo AddData(GroupingDefinition definition, List<CalculateDefinition> listCalculateDefinition, List<TransactionInfo> transactionInfo)
        {
            var root = new GroupInfo()
            {
                Key = string.Empty,
                ListData = transactionInfo,
                InnerGroupData = new Dictionary<string, GroupInfo>(),
                ListCalculateDefinition = listCalculateDefinition,
            };
            root.RefreshCalculateValue();
            AddDataGroup(root, definition);

            return root;
        }
        private void AddDataGroup(GroupInfo root, GroupingDefinition definition)
        {
            foreach (var info in root.ListData)
            {
                string rootKey = root.Key;
                string groupKey = GetItemKey(definition.GetKeyMethod, info);
                string key = GetKey(rootKey, groupKey);

                GroupInfo groupInfo = null;
                if (!root.InnerGroupData.TryGetValue(key, out groupInfo))
                {
                    groupInfo = new GroupInfo()
                    {
                        RootKey = rootKey,
                        GroupKey = groupKey,
                        Key = key,
                        GroupingDefinition = definition,
                        ListData = new List<TransactionInfo>(),
                        InnerGroupData = new Dictionary<string, GroupInfo>(),
                        ListCalculateDefinition = root.ListCalculateDefinition
                    };
                    root.InnerGroupData.Add(key, groupInfo);
                }

                groupInfo.ListData.Add(info);
            }

            foreach (var groupInfoInner in root.InnerGroupData.Values)
            {
                if (definition.InnerDefinition != null)
                {
                    AddDataGroup(groupInfoInner, definition.InnerDefinition);
                }
                groupInfoInner.RefreshCalculateValue();
            }
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            ExcelRead reader = new ExcelRead();
            reader.InitializeWorkbook("20130417 Tong Doanh Thu 2013-Phan Tich.xls");

            var listProductInfo = reader.ReadProductData();
            var listData = reader.ReadTransactionData(listProductInfo);

            var listFilterRequest = new List<FilteringItem>(new FilteringItem[]
                {
                    new FilteringItem()
                        {
                            FilterName = "Year",
                            FilterValue = 2012,
                        },
                    new FilteringItem()
                        {
                            FilterName = "Month",
                            FilterValue = 1,
                        },
                    new FilteringItem()
                        {
                            FilterName = "CustomerName",
                            FilterValue = "Bác sĩ Nguyễn Văn Cót",
                        }
                });

            var calculateDefinitions = new List<CalculateDefinition>(new CalculateDefinition[]
                {
                    new CalculateDefinition()
                        {
                            CalculateName = "Quantity",
                            Type = CalculateType.Sum,
                        },
                    new CalculateDefinition()
                        {
                            CalculateName = "ContractPrice",
                            Type = CalculateType.Average,
                        },
                    new CalculateDefinition()
                        {
                            CalculateName = "SellPrice",
                            Type = CalculateType.Sum,
                        },
                });

            var groupXDefinitionCustomerName = new GroupingDefinition()
                {
                    KeyName = "CustomerName"
                };

            var groupXDefinitionProductCategory = new GroupingDefinition()
                {
                    KeyName = "ProductCategory"
                };

            var groupXDefinitionProductName = new GroupingDefinition()
                {
                    KeyName = "ProductName"
                };

            groupXDefinitionCustomerName.UpperDefinition = null;
            groupXDefinitionCustomerName.InnerDefinition = groupXDefinitionProductCategory;

            groupXDefinitionProductCategory.UpperDefinition = groupXDefinitionCustomerName;
            groupXDefinitionProductCategory.InnerDefinition = groupXDefinitionProductName;

            groupXDefinitionProductName.UpperDefinition = groupXDefinitionProductCategory;
            groupXDefinitionProductName.InnerDefinition = null;

            var groupYDefinitionYear = new GroupingDefinition()
                {
                    KeyName = "Year",
                };

            var groupYDefinitionMonth = new GroupingDefinition()
                {
                    KeyName = "Month",
                };

            groupYDefinitionYear.UpperDefinition = null;
            groupYDefinitionYear.InnerDefinition = groupYDefinitionMonth;

            groupYDefinitionMonth.UpperDefinition = groupYDefinitionYear;
            groupYDefinitionMonth.InnerDefinition = null;

            Console.WriteLine(listProductInfo.Count);
            Console.WriteLine(listData.Count);

            //DataFilteringController filterControl = new DataFilteringController()
            //    {
            //        FilterProps = listFilterRequest
            //    };
            //var filterData = filterControl.GetFilterData(listData);

            GroupingController controller = new GroupingController();
            var result = controller.AddData(groupYDefinitionYear, calculateDefinitions, listData);

            var pivotTable = new PivotTableController()
                {
                    GroupingX = groupXDefinitionCustomerName,
                    GroupingY = groupYDefinitionYear,
                    Data = listData,
                    //Data = filterData,
                    CalculateDefinitions = calculateDefinitions,
                    DataFiltering = new DataFilteringController()
                    {
                        FilterProps = listFilterRequest
                    }
                };

            pivotTable.AnalyzeData();
            Console.ReadKey();
        }
        private void LoopGroupDefinitionY(Dictionary<string, Combine2DDimensionGroupingDefinition> dictionary,
            GroupingDefinition groupXGroupingDefinition,
            GroupingDefinition groupYGroupingDefinition)
        {
            Combine2DDimensionGroupingDefinition root = new Combine2DDimensionGroupingDefinition()
            {
                ListCalculateDefinition = this.CalculateDefinitions,
                XDefinition = groupXGroupingDefinition,
                YDefinition = groupYGroupingDefinition,
                Key = GenerateGroupingConditionKey(groupXGroupingDefinition, groupYGroupingDefinition),
            };

            _logger.DebugFormat("LoopGroupY: {0}", root.Key);

            if (groupYGroupingDefinition != null)
            {
                LoopGroupDefinitionY(dictionary, groupXGroupingDefinition, groupYGroupingDefinition.InnerDefinition);
            }

            dictionary.Add(root.Key, root);
        }
        private string GetDataGroupKey(GroupingDefinition groupingDefinition, TransactionInfo data)
        {
            if (groupingDefinition == null)
            {
                return string.Empty;
            }

            StringBuilder builder = new StringBuilder();
            builder.Append(groupingDefinition.GetKeyMethod.GetValue(data, null));
            if (groupingDefinition.UpperDefinition != null)
            {
                builder.Append(",");
                string upperKey = GetDataGroupKey(groupingDefinition.UpperDefinition, data);
                builder.Append(upperKey);
            }

            return builder.ToString();
        }
        private String GenerateGroupingConditionKey(GroupingDefinition groupXGroupingDefinition,
                                                    GroupingDefinition groupYGroupingDefinition)
        {
            string xKey = GenerateGroupingConditionKey(groupXGroupingDefinition);
            string yKey = GenerateGroupingConditionKey(groupYGroupingDefinition);

            return string.Format(DefinitionKeyTemplate, xKey, yKey);
        }
        private String GenerateGroupingConditionKey(GroupingDefinition groupingDefinition)
        {
            StringBuilder builder = new StringBuilder();
            if (groupingDefinition == null)
            {
                return string.Empty;
            }
            else if (groupingDefinition.UpperDefinition != null)
            {
                string upperKey = GenerateGroupingConditionKey(groupingDefinition.UpperDefinition);
                builder.Append(upperKey);
                builder.Append(",");

            }
            builder.Append(groupingDefinition.KeyName);

            return builder.ToString();
        }
        public void LoopGroupDefinitionX(Dictionary<string, Combine2DDimensionGroupingDefinition> dictionary,
            GroupingDefinition groupXGroupingDefinition,
            GroupingDefinition groupYGroupingDefinition)
        {
            if (groupXGroupingDefinition != null)
            {
                LoopGroupDefinitionX(dictionary, groupXGroupingDefinition.InnerDefinition, groupYGroupingDefinition);
            }

            LoopGroupDefinitionY(dictionary, groupXGroupingDefinition, groupYGroupingDefinition);
        }