Click here to Skip to main content
15,885,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to display a chart in a web page using CanvasJS in mvc. i can see the JSON data in a web page but it does not display in a chart. my Controller and script code looks like this: please help:

C#
namespace ContsoSite.Controllers
{
    public class DashboardController : Controller
    {
        //
        // GET: /Dashboard/
        public ActionResult Index()
        {
            return View();
        }
        public JsonResult GetData()
        {
            using (var db = new ContosoUniversityEntities ())
            {
                var result = (from tags in db.Courses
                              orderby tags.Title ascending
                              select new { tags.Title, tags.Credits }).ToList();
                return Json(JsonConvert.SerializeObject(result), JsonRequestBehavior.AllowGet);
                //return Content(JsonConvert.SerializeObject(_dataPoints), "application/json");
            }
        }

        public ActionResult Dashboard_Pie()
        {
            return View();
        }

        public ActionResult Dashboard_AreaChart()
        {
            return View();
        }


        public ActionResult Dashboard_ColumnChart()
        {
            return View();
        }
    }
}


and my script looks like this:

XML
@model IEnumerable<ContsoSite.Models.ContosoUniversityEntities>
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<script src="~/Scripts/canvasjs.min.js"></script>
@*<script src="~/Scripts/canvasjs.js"></script>*@
@*<script src="~/Scripts/excanvas.js"></script>*@

<script type="text/javascript">
    $(document).ready(function () {

        $.getJSON("/Dashboard/GetData/", function (data) {
            var chart = new CanvasJS.Chart("chartContainer", {
                theme: "theme2",//theme1
                title: {
                    text: "CanvasJS Charts in ASP.Net MVC using AJAX & JSON"
                },
                data: [
                {
                    // Change type to "bar", "splineArea", "area", "spline", "pie",etc.
                    type: "column",
                    dataPoints: data
                }
                ]
            });

            chart.render();
        });

    });
</script>

    <body>
        <div id="chartContainer" style="height: 300px; width: 100%;"></div>


    </body>
Posted
Updated 8-Jul-14 23:48pm
v2

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