Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have tried a couple of things to display data in graphs using js library, but no success.

What I am looking for is to show WeekStart Date on X axis, and a Bar, at the top of the bar show the Total Chats taken for that week.

Below is the code I wrote till now, but failed to show the results.

Help is very much appreciated.

  1. Action Method
public ActionResult Dashboard()
{
    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;

    try
    {
        ViewBag.DataPoints = JsonConvert.SerializeObject(_db.Chats.Where(x => System.Data.Entity.DbFunctions.TruncateTime(x.MSTChatCreatedDateTime) >= mostRecentMonday
                   && System.Data.Entity.DbFunctions.TruncateTime(x.MSTChatCreatedDateTime) <= weekEnd).GroupBy(a => System.Data.Entity.DbFunctions.TruncateTime(a.MSTChatCreatedDateTime)).Select(b => new ReportVM()
                   {
                       CreatdDate = b.Key,
                       ChatCountCreatdDate = b.Count()

                   }).ToList(), _jsonSetting);

        return View();
    }
    catch (System.Data.Entity.Core.EntityException)
    {
        return View("Error");
    }
    catch (System.Data.SqlClient.SqlException)
    {
        return View("Error");
    }
}

JsonSerializerSettings _jsonSetting = new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore };
  1. Dashboard View
<head>
    <script src="~/Content/vendor/chart.js/Chart.min.js"></script>
</head>

<body>
    <div id="chartContainer"></div>

    

    <script type="text/javascript">
        
        var dataPoints =[];
        for (var i in ViewBag.DataPoints){
            dataPoints.push({label:result[i].x, y:result[i].y});
        }

        window.onload = function() {
            var chart = new CanvasJS.Chart("chartContainer", {
                theme: "light2",
                zoomEnabled: true,
                animationEnabled: true,
                title: {
                    text: "Line Chart with Data-Points from DataBase"
                },
                data: [
                {
                    type: "barcode",

                    dataPoints: dataPoints,
                }
                ]
            });
            chart.render();
        };
    </script>
</body>

Thank you in advance.

question from:https://stackoverflow.com/questions/66065564/graphs-chart-in-asp-net-mvc-using-js-library

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
189 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...