首页 > 解决方案 > 在堆积条形图上获取平均值而不是总计?

问题描述

我编写了一个代码来使用 ASP.NET MVC 显示堆积条形图。但是,结果呈现是总计,因为我正在寻找平均值而不是总数。

已处理 = 个人处理的聊天(这工作正常) 团队 = 这应该呈现团队处理的平均聊天(这显示聊天的总数)

因此,为了获得结果,我需要每个月获取用户计数,然后将其除以总数以获得平均值。但是,我无法获得 UserCount,因此我可以将总数除以显示平均值。下面是我的代码

动作方法


public ActionResult Stats()
{
int year = DateTime.Now.Year;
DateTime firstDay = new DateTime(year, 1, 1);
DateTime lastDay = new DateTime(year, 12, 31).AddDays(1).AddSeconds(-1);

var uName = User.Identity.Name;

var result = db.Chats.Where(c => System.Data.Entity.DbFunctions.TruncateTime(c.MSTChatCreatedDateTime) >= firstDay &&
System.Data.Entity.DbFunctions.TruncateTime(c.MSTChatCreatedDateTime) <= lastDay && c.Username == uName)
.Select(x => new { x.MSTChatCreatedDateTime }).ToList().GroupBy(c => new
{
Year = c.MSTChatCreatedDateTime.ToCleanDateTime().Year,
Month = c.MSTChatCreatedDateTime.ToCleanDateTime().ToMonthName()

}).Select(c => new ReportVM
{
Title = string.Format("{0}", c.Key.Month),  //chart x Axis value.
ChatCountCreatdDate = c.Count() //chart y Axis value.
}).ToList();

            
var teamChats = db.Chats.Where(c => System.Data.Entity.DbFunctions.TruncateTime(c.MSTChatCreatedDateTime) >= firstDay &&
System.Data.Entity.DbFunctions.TruncateTime(c.MSTChatCreatedDateTime) <= lastDay).Select(x => new { x.MSTChatCreatedDateTime }).ToList().GroupBy(c => new
{
Year = c.MSTChatCreatedDateTime.ToCleanDateTime().Year,
Month = c.MSTChatCreatedDateTime.ToCleanDateTime().ToMonthName(),
}).Select(c => new ReportVM
{
Title = string.Format("{0}", c.Key.Month),  //chart x Axis value.
ChatCountCreatdDate = c.Count()  ///chart y Axis value.
}).ToList();

ViewBag.TeamStat = teamChats;


            

return View(result);

}

附上截图

字符

先感谢您!

标签: c#asp.net-mvcc#-3.0

解决方案


推荐阅读