public JsonResult jsonPieData(string DepartmentId, string SectionId, string YearId, string MonthId, string MatNameId)
        {
            //add summary data
            List<DeliveredOntimePieChartMonthlyViewModels> viewSummaryModel = new List<DeliveredOntimePieChartMonthlyViewModels>();

            // filter by department
            var q = objBs.ontimeDeliveryMonthBs.GetAll()
                .Where(x => !String.IsNullOrEmpty(x.DepartmentName)
                && !String.IsNullOrEmpty(x.SectionName)
                && !String.IsNullOrEmpty(x.MatName)
                && x.Year == YearId);
            //&& x.DepartmentId == DepartmentId

            //filter Department
            if (!String.IsNullOrEmpty(DepartmentId))
                q = q.Where(x => x.DepartmentId == DepartmentId);

            //filter Section
            if (!String.IsNullOrEmpty(SectionId))
                q = q.Where(x => x.SectionId == SectionId);

            //filter matname
            if (!String.IsNullOrEmpty(MatNameId))
                q = q.Where(x => x.MatFriGrp == MatNameId);

            //filter month
            if (!String.IsNullOrEmpty(MonthId))
                q = q.Where(x => x.Month == MonthId);

            int TotalDelivery = q.Sum(x => x.SumOfDelivery);
            var results = (from c in q
                           group c by new { c.MatFriGrp, c.MatName } into g
                           select new {
                               MatFriGrp = g.Key.MatFriGrp,
                               MatName = g.Key.MatName,
                               SumOfDelivery = g.Sum(x => x.SumOfDelivery),
                               Ratio = ((double)g.Sum(x => x.SumOfDelivery) / (double)TotalDelivery) * 100
                           }).OrderByDescending(x=>x.Ratio);

            var random = new Random();
            foreach (var item in results) {
                DeliveredOntimePieChartMonthlyViewModels model = new DeliveredOntimePieChartMonthlyViewModels();
                var color = String.Format("#{0:X6}", random.Next(0x1000000)); // = "#A197B9"
                //string[] labelArray = (item.MatName + "-" + Math.Round(item.Ratio, 2).ToString()).Split('-');
                model.value = Math.Round(item.Ratio, 2);
                model.color = color;
                model.highlight = color;
                model.label = item.MatName + " - " + Math.Round(item.Ratio, 2).ToString();
                viewSummaryModel.Add(model);
            }

            return Json(viewSummaryModel, JsonRequestBehavior.AllowGet);
        }
Exemplo n.º 2
0
        public JsonResult jsonPieData(string DepartmentId, string SectionId, string YearId, string MonthId, string MatNameId)
        {
            //add summary data
            List <DeliveredOntimePieChartMonthlyViewModels> viewSummaryModel = new List <DeliveredOntimePieChartMonthlyViewModels>();

            // filter by department
            var q = objBs.ontimeDeliveryMonthBs.GetAll()
                    .Where(x => !String.IsNullOrEmpty(x.DepartmentName) &&
                           !String.IsNullOrEmpty(x.SectionName) &&
                           !String.IsNullOrEmpty(x.MatName) &&
                           x.Year == YearId);

            //&& x.DepartmentId == DepartmentId

            //filter Department
            if (!String.IsNullOrEmpty(DepartmentId))
            {
                q = q.Where(x => x.DepartmentId == DepartmentId);
            }

            //filter Section
            if (!String.IsNullOrEmpty(SectionId))
            {
                q = q.Where(x => x.SectionId == SectionId);
            }

            //filter matname
            if (!String.IsNullOrEmpty(MatNameId))
            {
                q = q.Where(x => x.MatFriGrp == MatNameId);
            }

            //filter month
            if (!String.IsNullOrEmpty(MonthId))
            {
                q = q.Where(x => x.Month == MonthId);
            }

            int TotalDelivery = q.Sum(x => x.SumOfDelivery);
            var results       = (from c in q
                                 group c by new { c.MatFriGrp, c.MatName } into g
                                 select new {
                MatFriGrp = g.Key.MatFriGrp,
                MatName = g.Key.MatName,
                SumOfDelivery = g.Sum(x => x.SumOfDelivery),
                Ratio = ((double)g.Sum(x => x.SumOfDelivery) / (double)TotalDelivery) * 100
            }).OrderByDescending(x => x.Ratio);

            var random = new Random();

            foreach (var item in results)
            {
                DeliveredOntimePieChartMonthlyViewModels model = new DeliveredOntimePieChartMonthlyViewModels();
                var color = String.Format("#{0:X6}", random.Next(0x1000000)); // = "#A197B9"
                //string[] labelArray = (item.MatName + "-" + Math.Round(item.Ratio, 2).ToString()).Split('-');
                model.value     = Math.Round(item.Ratio, 2);
                model.color     = color;
                model.highlight = color;
                model.label     = item.MatName + " - " + Math.Round(item.Ratio, 2).ToString();
                viewSummaryModel.Add(model);
            }

            return(Json(viewSummaryModel, JsonRequestBehavior.AllowGet));
        }