1

I want to show data that comes from databse with chart.js.

In asp.net MVC i have a controller that retrieves data from the database and shows them in view with the following code:

@foreach(var items in Model)
{
    <h6>@items.title</h6>
    <h6>@items.cuntr</h6>
}

How can I show @items.title and @items.cuntr in chart.js? Thanks a lot.

1
  • Enhanced syntax Commented Aug 15, 2017 at 18:47

3 Answers 3

4

Take a look at this link: http://www.c-sharpcorner.com/article/create-free-charts-using-chart-js-in-asp-net-mvc/ It describes all the relevant steps to work with charts in mvc.

Let us say that this is your controller method:

    public ActionResult Index()
    {
      ViewBag.Data = "Value,Value1,Value2,Value3"; //list of strings that you need to show on the chart. as mentioned in the example from c-sharpcorner
      ViewBag.ObjectName = "Test,Test1,Test2,Test3";
    }

You can return the data in a view bag and pass them to the data property oif the chart:

And this is your view:

@{  
    Layout = null;  
}  
<!DOCTYPE html>  
<html>  
<head>  
    <meta name="viewport" content="width=device-width" />  
    <title>Charts</title>  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.2/Chart.bundle.min.js"></script>  
    <script>  
        var barChartData =  
            {  
                labels: [@Html.Raw(ViewBag.ObjectName)], //the names displayed on the x-axis, see images below
                datasets: [{  
                    label: 'ProductWise Sales Count',  
                    backgroundColor: [  
                        "#f990a7",  
                        "#aad2ed",  
                        "#9966FF",  
                        "#99e5e5",  
                        "#f7bd83",  
                    ],  
                    borderWidth: 2,  
                    data: [@ViewBag.Data]  //what you returned back from controller. values displayed on the y-axis, see images below
                }]  
            };  

            window.onload = function () {  
                var ctx1 = document.getElementById("barcanvas").getContext("2d");  
                window.myBar = new Chart(ctx1,  
                    {  
                        type: 'bar',  
                        data: barChartData,  
                        options:  
                            {  
                                title:  
                                {  
                                    display: true,  
                                    text: "ProductWise Sales Count"  
                                },  
                                responsive: true,  
                                maintainAspectRatio: true  
                            }  
                    });  
            }  
    </script>  
</head>  
<body>  
    <div style="text-align: center">  
        <canvas id="barcanvas"></canvas>  
    </div>  
    <div style="text-align: center">  
        Disclaimer:- This data is for demo it is   
        not real data it wont relate to any company  
    </div>  
</body>  
</html>  

enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

thanks. i saw it. but i want to handle it from view. or simpler way.
what do you mean, you wanna handle it from view?
@masoudamirian, i will udpate my answer
thanks a lot. in this tutorial it use dapper. but i can't use this. because in dotnetnuke not allowed.
@masoudamirian, i edited my answer. no need to use dapper. Dapper is just an ORM framework, you are not obliged to use it
|
1

You should be able to access it directly from the ViewBag. e.g. @Viewbag.YourObject

I found that this project helped when creating charts , might need a few tweaks but works

Link to MVC Charts Project

Comments

-1

This is by far the easiest example I've come across just copy paste the code in your View & controller and bobs your uncle. you can modify thereafter

once thats done you can call your database like:

     dr = dt.NewRow();
        dr["Employee"] = "Someone";
        dr["Credit"] = rawData.Where(a => a.person == "Someone").Count().ToString();
        dt.Rows.Add(dr);

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.