How to implement Google ScatterChart in Asp.net MVC

Step1: Create New MVC Application.

Step2: Now Write Below code in your _Layout.cshtml page.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
        google.charts.load('current', { 'packages': ['corechart'] });
        google.charts.setOnLoadCallback(ScatterChart);
        
        function ScatterChart() {
            var data = google.visualization.arrayToDataTable([
                ['Age', 'Height'],
                [2, 0],
                [4, 2.10],
                [5, 3],
                [6, 3.10],
                [7, 4],
                [8, 4],
                [9, 4.4],
                [10, 4.6],
                [11, 5],

            ]);

            var options = {
                title: 'Age wise Height',
                hAxis: { title: 'Age of the Child(in year) ', minValue: 0, maxValue: 15 },
                vAxis: { title: 'Height(in feet)', minValue: 0, maxValue: 15 },
                legend: 'none'
            };

            var chart = new google.visualization.ScatterChart(document.getElementById('ScatterChart_div'));

            chart.draw(data, options);
        }
    </script>
</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                </ul>
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

Step3: write below code in your index.cshtml page

<div id="ScatterChart_div" style="width: 900px; height: 500px;"></div>

Step4:  Now see the output.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories