使用Linq和Mvc显示条形图上的数据

时间:2017-03-24 05:33:36

标签: asp.net-mvc linq

希望得到代码的帮助。 我想在一个条形图上显示我的数据,我能够做但我想更专业地做这个和在linq的组,但我无法这样做。 我正在使用Genetella Admin Layout模板来绘制图形。 我在控制器部分的代码如下

public ActionResult Home()
        {
            int jan = 0,feb = 0,mar = 0,apr = 0,may = 0,jun = 0,jul = 0 ,aug = 0 ,sep = 0,oct = 0 ,nov = 0,dec = 0;
            ViewData["Freetrialcompanies"] = admin.getAllCompanies();
            List<string> month = new List<string>();
            foreach ( var cmpMonth in ViewData["Freetrialcompanies"] as IEnumerable<IntuitiveCrm.ViewModels.CompanyMasterViewModel>)
            {
                var mth = DateTimeOffset.Parse((cmpMonth.CreatedDate).ToString()).ToString("MMMM");
                month.Add(mth);
                switch (mth)
                {
                    case "January":
                        jan = jan + 1;
                        var Janmth = jan;
                        if(Janmth != 0)
                        {
                            ViewData["jan"] = Janmth;
                        }
                        else
                        {
                            ViewData["jan"] = 0;
                        }

                    break;

                    case "February":
                        feb = feb + 1;
                        var Febmth = feb;
                        if (Febmth != 0)
                        {
                            ViewData["feb"] = Febmth;
                        }
                        else
                        {
                            ViewData["feb"] = 0;
                        }
                        break;

                    case "March":
                         mar = mar + 1;
                        var Marmth = mar;
                        if (Marmth != 0)
                        {
                            ViewData["mar"] = Marmth;
                        }
                        else
                        {
                            ViewData["mar"] = 0;
                        }
                        break;

                    case "April":
                        apr = apr + 1;
                        var Aprmth = apr;
                        if (Aprmth != 0)
                        {
                            ViewData["apr"] = Aprmth;
                        }
                        else
                        {
                            ViewData["apr"] = 0;
                        }
                        break;

                    case "May":
                        may = may + 1;
                        var Maymth = may;
                        if (Maymth != 0)
                        {
                            ViewData["may"] = Maymth;
                        }
                        else
                        {
                            ViewData["may"] = 0;
                        }
                        break;

                    case "June":
                        jun = jun + 1;
                        var Junmth = jun;
                        if (Junmth != 0)
                        {
                            ViewData["jun"] = Junmth;
                        }
                        else
                        {
                            ViewData["jun"] = 0;
                        }
                        break;

                    case "July":
                        jul = jul + 1;
                        var Julmth = jul;
                        if (Julmth != 0)
                        {
                            ViewData["jul"] = Julmth;
                        }
                        else
                        {
                            ViewData["jul"] = 0;
                        }
                        break;

                    case "August":
                        aug = aug + 1;
                        var Augmth = aug;
                        if (Augmth != 0)
                        {
                            ViewData["aug"] = Augmth;
                        }
                        else
                        {
                            ViewData["aug"] = 0;
                        }
                        break;

                    case "September":
                        sep = sep + 1;
                        var Sepmth = sep;
                        if (Sepmth != 0)
                        {
                            ViewData["sep"] = Sepmth;
                        }
                        else
                        {
                            ViewData["sep"] = 0;
                        }
                        break;

                    case "October":
                        oct = oct + 1;
                        var Octmth = oct;
                        if (Octmth != 0)
                        {
                            ViewData["oct"] = Octmth;
                        }
                        else
                        {
                            ViewData["oct"] = 0;
                        }
                        break;

                    case "November":
                        nov = nov + 1;
                        var Novmth = nov;
                        if (Novmth != 0)
                        {
                            ViewData["nov"] = Novmth;
                        }
                        else
                        {
                            ViewData["nov"] = 0;
                        }
                        break;

                    case "December":
                        dec = dec + 1;
                        var Decmth = dec;
                        if (Decmth != 0)
                        {
                            ViewData["dec"] = Decmth;
                        }
                        else
                        {
                            ViewData["dec"] = 0;
                        }
                        break;

                }

            }
            ViewData["Mnth"] = month;
            return View();
        }

在这里,我从创建日期获取月份名称,该日期名称存储在日期时间偏移格式中&#34; mth&#34;并将其分开来计算。

我的cs文件代码如下所示admin.getAllCompanies();

public List<CompanyMasterViewModel> getAllCompanies()
        {
            var taskData = (from t in Comp.GetAll()
                            join p in plan.GetAll()
                            on t.PlanId equals p.PlanId
                            join f in freeTrial.GetAll()
                            on t.CompanyId equals f.CompanyId
                            where t.Status == 1
                            select new CompanyMasterViewModel
                            {
                                CompanyName = t.CompanyName,
                                PlanName = p.PlanName,
                                Email = t.Email,
                                CreatedDate = f.CreatedDate,
                                EndDate = f.EndDate,
                                Status = t.Status,
                            }).ToList<CompanyMasterViewModel>();
            return taskData;
        }

有了这个我生成以下条形图 enter image description here

任何人都可以帮助我更好地使用这些代码以及如何编写linq查询,这样我就可以按月订购createdate(它存储在datetimeoffset中)。

谢谢。

1 个答案:

答案 0 :(得分:0)

我可以想象几种不同的解决方案,但您可以尝试以下方法。我不知道你是否在更多的地方使用library(dplyr) library(lubridate) # Setting up a data.frame df_stackOverflow <- data.frame(ID = c(1,1, 2,2,2,2, 3,3, 4,4, 5,5,5, 6,6,6), time = c("2016-11-11 10:25:07", "2016-11-11 11:13:09", "2016-11-16 21:13:28", "2016-11-29 19:18:58", "2016-12-01 18:44:41", "2016-12-04 12:46:44", "2016-12-26 20:49:07", "2016-12-30 11:41:51", "2016-11-25 10:25:52", "2016-12-26 22:04:36", "2016-12-07 21:27:53", "2016-12-07 21:52:58", "2016-12-09 18:32:23", "2016-11-25 14:10:24", "2016-11-25 20:06:43", "2016-11-25 21:07:33"), Factor = c("A","B", "C","B","B","C", "B","B", "A","D", "D","D","D", "B","E","B")) # My try to save a data.frame # I want to save all ros where the previous value for that ID was "B". # And also the the time difference between this and the previous value need to be under a certain threshold. # This threshold will be looped for different values df_res <- list() for(i in 1:15) { df_res[[i]] <- df_stackOverflow %>% group_by(ID) %>% filter(lag(Factor) == "B" & as.period(interval(as.POSIXct(time), as.POSIXct(lag(time))), units = "day") < days(i)) } 方法,但我认为是这样的,所以我不打算改变这种方法。您可以在业务层中添加另一种方法,如下所示:

getAllCompanies()

CompanyCountView类是我为保持整洁而创建的新视图模型:

public IEnumerable<CompanyCountView> companyCountByMonth()
{
    List<CompanyMasterViewModel> companies = this.getAllCompanies();
    ILookup<int, Company> byMonths = companies.ToLookup(key => key.CreatedDate.Month);

    for (var i = 1; i <= 12; i++)
    {
        yield return new CompanyCountView
        {
            Date = new DateTime(2016, i, 1),
            Count = months[i].Count()
        };
    }
}
在Controller中

,您只需调用此函数即可。我建议在视图中使用视图模型,并将ViewData限制为最小值。这对维护来说更好。

public class CompanyCountView
{
    public DateTime Date { get; set; }
    public int Count { get; set; }
}

在视图中,您需要显示3个字母的缩写。只需使用:

public ActionResult Home() 
{
    IEnumerable<CompanyCountView> model = admin.companyCountByMonth();
    return View(model);
}

我不确定你是怎么处理多年的。如果图表应该在一年或几个月内显示所有条目的月份 - 某种统计数据。如果它只是某一年,只需将我的硬编码值“2016”替换为您需要的年份。

如果是另一种情况,并且您不想创建DateTime类型的实例只是为了存储一个月,或者您需要考虑本地化。然后你可以使用略有不同的方法。您只需要创建枚举

@item.Date.ToString("MMM")

并更新模型视图public enum Months { Jan = 1, Feb = 2, Mar = 3 // etc. }

CompanyCountview

public class CompanyCountView { public Months Date { get; set; } public int Count { get; set; } } 函数:

companyCountByMonth()

最后一个注释是关于public IEnumerable<CompanyCountView> companyCountByMonth() { List<CompanyMasterViewModel> companies = this.getAllCompanies(); ILookup<int, Company> byMonths = companies.ToLookup(key => key.CreatedDate.Month); for (var i = 1; i <= 12; i++) { yield return new CompanyCountView { Date = (Months)i, Count = months[i].Count() }; } } 功能。如果仅为此特定视图创建此函数,我会考虑在将所有内容加载到内存之前对查询执行getAllCompanies()。但在这种情况下,我的解决方案必须进行调整:)我希望你能得到一个如何简化它的要点。