Click here to Skip to main content
16,019,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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()
{
            /*This Month Begin*/

            DateTime now = DateTime.Now;
            var startDate = new DateTime(now.Year, now.Month, 1);
            var endDate = startDate.AddMonths(1).AddDays(-1);
            /*This Month Ends*/

            /*Last Week Begin*/
            var mostRecentMonday = DateTime.Now.AddDays(-7).StartOfWeek(DayOfWeek.Monday);
            var weekEnd = mostRecentMonday.AddDays(7).AddSeconds(-1); //will return the end of the day on Sunday

            ViewBag.Monday = mostRecentMonday;
            ViewBag.lastWeekSunday = weekEnd;
            /*Last Week Ends*/

            /*This Week Begin*/
            var thisWeekMonday = DateTime.Now.StartOfWeek(DayOfWeek.Monday);//get week start of most recent Monday morning
            var Thisweekend = thisWeekMonday.AddDays(7).AddSeconds(-1); //will return the end of the day on Sunday
            /*This Week Ends*/

            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();
            //chart.Width = Unit.Pixel(800);
            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\\%";
            //series.Label = "#PERCENT{P0}";
            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;
            //series["PieLabelStyle"] = "Outside";
            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
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900