public ELMAH_Monthly_Info GetBarDataPerMonth(int month) { var random = new Random(); ELMAH_Monthly_Info barPerMonth = new ELMAH_Monthly_Info(); barPerMonth.Month = month; barPerMonth.MonthName = Enum.GetName(typeof(Enum_Month), month); barPerMonth.Color = String.Format("#{0:X6}", random.Next(0x1000000)); using (var context = new ELMAH_Entities()) { DateTime beginTime = DateHelper.GetDateMonthStart(DateTime.Today.Year, month).ToUniversalTime(); DateTime endTime = DateHelper.GetDateMonthEnd(DateTime.Today.Year, month).ToUniversalTime(); //int totalPerMonth = (from x in context.ELMAH_Error // where x.TimeUtc > beginTime // && x.TimeUtc < endTime // select x).Count(); var groupResult = from x in context.ELMAH_Error where x.TimeUtc > beginTime && x.TimeUtc < endTime group x by x.Type into grp select new { TypeName = grp.Key, Count = grp.Count() }; barPerMonth.ExceptionInfo = new List <Tuple <string, int> >(); foreach (var item in groupResult) { Tuple <string, int> t = new Tuple <string, int>(item.TypeName, item.Count); barPerMonth.ExceptionInfo.Add(t); } barPerMonth.TotalPerMonth = groupResult.Any() ? groupResult.Sum(p => p.Count) : 0; //totalPerMonth; } return(barPerMonth); }
// GET: api/BarChart public BarData GetData(int baseMonth, int monthToShow) { BarData barData = new BarData(); ELMAH_BarChart chartManager = new ELMAH_BarChart(); List <int> howManyMonth = new List <int>(); switch (monthToShow) { case 3: howManyMonth.Add(DateTime.Today.Month - 2); howManyMonth.Add(DateTime.Today.Month - 1); howManyMonth.Add(DateTime.Today.Month); break; case 5: howManyMonth.Add(DateTime.Today.Month - 4); howManyMonth.Add(DateTime.Today.Month - 3); howManyMonth.Add(DateTime.Today.Month - 2); howManyMonth.Add(DateTime.Today.Month - 1); howManyMonth.Add(DateTime.Today.Month); break; default: howManyMonth.Add(DateTime.Today.Month); break; } for (int i = 0; i < howManyMonth.Count(); i++) { ELMAH_Monthly_Info infoMonth = chartManager.GetBarDataPerMonth(howManyMonth[i]); barData.BarInfo.Add(infoMonth); } return(barData); }