0

I am pretty new to asp.net so I tried to add a simple bar chart with charts.js to my view but all I get is an empty page.

Controller

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        List<string> iData = new List<string>();
        List<string> iData2 = new List<string>();

        iData.Add("Sam");
        iData2.Add("555");
        iData.Add("Alex");
        iData2.Add("666");
        iData.Add("Michael");
        iData2.Add("777");

        ViewBag.Value_List = iData2;
        ViewBag.Name_List = iData;

        return View();
    }
}

View:

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Charts</title>
    <script src="~/Scripts/Chart.min.js"></script>
    <script src="~/Scripts/jquery-3.3.1.min.js"></script>
    <script>

        var barChartData =
            {
                labels: [@Html.Raw(ViewBag.Name_List)],
                datasets: [{
                    label: 'ChartTest',
                    backgroundColor: [
                        "#f990a7",
                        "#aad2ed",
                        "#9966FF",
                        "#99e5e5",
                        "#f7bd83",
                    ],
                    borderWidth: 2,
                    data: [@ViewBag.Value_List]
                }]
            };

            window.onload = function () {
                var ctx1 =     document.getElementById("barcanvas").getContext("2d");
                window.myBar = new Chart(ctx1,
                    {
                        type: 'bar',
                        data: barChartData,
                        options:
                            {
                                title:
                                {
                                    display: true,
                                    text: "ChartTest"
                                },
                                responsive: true,
                                maintainAspectRatio: true
                            }
                    });
            }
    </script>
</head>
<body>
    <div style="text-align: center">
        <canvas id="barcanvas"></canvas>
    </div>
</body>
</html> 

I tried to help me with some tutorials but nothing worked for me.

Can someone tell me what I am doing wrong?

1
  • Please format your question next time. Commented Jun 8, 2018 at 13:43

1 Answer 1

1

Two changes needed:

Serialize the data into JSON in the controller:

using System.Web.Script.Serialization;

var js = new JavaScriptSerializer();
ViewBag.Value_List = js.Serialize( iData2);
ViewBag.Name_List = js.Serialize(iData);

Remove the extra square brackets in the view:

labels: @Html.Raw(ViewBag.Name_List),

data: @Html.Raw(ViewBag.Value_List)

You need to create "drop in" JavaScript to represent the data.

labels: ["555","666","777"] ,

data: ["Sam","Alex","Michael"] 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.