I am using Asp.NET Mvc frame work. WIth my code I am trying to show data week wise on Chart. I am fairly new to Chart, and also new to GroupBy Week Wise Data. Kindly share yous help so that I can generate Report for Data as per Week
<pre lang="ASP.NET">
<pre>Week1 = QAScore 94%(Average)
Week2 = 95%
Week3 = 90%
Week4 = 91%
public ActionResult ChartQA()
{
DateTime now = DateTime.Now;
var startDate = new DateTime(now.Year, now.Month, 1);
var endDate = startDate.AddMonths(1).AddDays(-1);
var mostRecentMonday = DateTime.Now.AddDays(-7).StartOfWeek(DayOfWeek.Monday);
var weekEnd = mostRecentMonday.AddDays(7).AddSeconds(-1);
ViewBag.Monday = mostRecentMonday;
ViewBag.lastWeekSunday = weekEnd;
var thisWeekMonday = DateTime.Now.StartOfWeek(DayOfWeek.Monday);
var Thisweekend = thisWeekMonday.AddDays(7).AddSeconds(-1);
var QMSCount = db.Chats.Where(x => System.Data.Entity.DbFunctions.TruncateTime(x.MSTChatCreatedDateTime) >= mostRecentMonday && System.Data.Entity.DbFunctions.TruncateTime(x.MSTChatCreatedDateTime)
<= weekEnd && x.Feedback != null && x.ChatNotFound == null).Count();
var QMSTotal = db.Chats.Where(x => System.Data.Entity.DbFunctions.TruncateTime(x.MSTChatCreatedDateTime) >= mostRecentMonday && System.Data.Entity.DbFunctions.TruncateTime(x.MSTChatCreatedDateTime)
<= weekEnd && x.Feedback != null && x.ChatNotFound == null).Sum(x => (float?)x.QualityScore);
ViewBag.TodaysAudit = QMSCount;
ViewBag.QMS = QMSTotal / QMSCount;
var chart = new Chart();
var area = new ChartArea();
chart.ChartAreas.Add(area);
var series = new Series();
series.Points.AddXY("Week1", Math.Round(ViewBag.QMS));
series.IsValueShownAsLabel = true;
series.LabelFormat = "{0}%";
chart.ChartAreas[0].AxisY.LabelStyle.Format = "###0\\%";
chart.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
chart.ChartAreas[0].AxisX.LabelStyle.Angle = 0;
chart.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
chart.ChartAreas[0].AxisX.LabelStyle.Interval = 1;
series.Font = new Font("Arial", 9.0f, FontStyle.Bold);
series.ChartType = SeriesChartType.Column;
chart.Series.Add(series);
var returnStream = new MemoryStream();
chart.ImageType = ChartImageType.Png;
chart.SaveImage(returnStream);
returnStream.Position = 0;
return new FileStreamResult(returnStream, "image/png");
}
Thank you for your help in advance
What I have tried:
Created a DateTimeExtentions to get Week wise data
<pre lang="ASP.NET">
public static class DateTimeExtensions
{
public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
{
int diff = (7 + (dt.DayOfWeek - startOfWeek)) % 7;
return dt.AddDays(-1 * diff).Date;
}
static GregorianCalendar _gc = new GregorianCalendar();
public static int GetWeekOfMonth(this DateTime time)
{
DateTime first = new DateTime(time.Year, time.Month, 1);
return time.GetWeekOfYear() - first.GetWeekOfYear() + 1;
}
static int GetWeekOfYear(this DateTime time)
{
return _gc.GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
}
}
Action Method as below
<pre lang="ASP.NET">
<pre> var result = chats.Select(c => new
{
ChatId = c.ChatId,
Title = c.Title,
Year = c.MSTChatCreatedDateTime.Year,
Month = c.MSTChatCreatedDateTime.Month,
WeekofMonth = c.MSTChatCreatedDateTime.GetWeekOfMonth()
});
//use GrouBy method to filter data per weeks.
var reult2 = result.GroupBy(c => new { c.Year, c.Month, c.WeekofMonth }).Select(c => c);
T