connectseekers.com

Thursday, 31 July 2014

Dynamic Google Scatter Chart In an Asp.Net MVC Using C# and Javascript

7/31/2014 - By Pranav Singh 0

This article will show you how you can create a dynamic Google scatter chart in an asp.net MVC using C# and JavaScript. This article can be used inMVC2,MVC3,MVC4 and MVC5.


So for this article first we will create a new MVC application and then add a model file into it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ScatterChartMVC.Models
{
    public class CompanyData
    {
        public string SaleValue { get; set; }
        public string PurchaseValue { get; set; }
        public int PurchaseMaxValue { get; set; }
        public int SaleMaxValue { get; set; }
        public string SaleTitle { get; set; }
        public string PurchaseTitle { get; set; }
    }
}

Now we will add controller and add the below code into it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ScatterChartMVC.Models;

namespace ScatterChartMVC.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            CompanyData objcompanydata = new CompanyData();
            /*Plepare the record with comma seprated value as shown*/
            objcompanydata.PurchaseValue = "2000,1200,1000,2400,300,600";
            objcompanydata.SaleValue = "1800,1100,1000,2000,299,600";
            objcompanydata.PurchaseMaxValue = 2500;
            objcompanydata.SaleMaxValue = 2500;
            objcompanydata.SaleTitle = "Sale";
            objcompanydata.PurchaseTitle = "Purchase";
            return View(objcompanydata);
        }       
    }
}

In above code I have a string containing value with comma. You for making this just retrieve the record from data base and prepare the data which you want to display in the string with comma(,).

Now generate the view and add the below code for displaying the chart.

@model ScatterChartMVC.Models.CompanyData
@{
    ViewBag.Title = "Scatter Chart in MVC";
}
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load("visualization", "1", { packages: ["corechart"] });
    google.setOnLoadCallback(drawChart);
    function drawChart() {
    // Create and populate the data table.
        var Sale = [@Model.SaleValue];
        var Purchase = [@Model.PurchaseValue];

        var data = new google.visualization.DataTable();
        data.addColumn('number', '@Model.SaleTitle');
        data.addColumn('number', '@Model.SaleTitle');
        for (i = 0; i < Sale.length; i++) {
            data.addRow([Sale[i], Purchase[i]]);
        }

        var options = {
            title: 'Sale vs. Purchase Data',
            hAxis: { title: 'Sale', minValue: 0, maxValue: @Model.SaleMaxValue },
            vAxis: { title: 'Purchase', minValue: 0, maxValue: @Model.PurchaseMaxValue },
            legend: 'none',

        };

        var chart = new google.visualization.ScatterChart(document.getElementById('divchart'));
        chart.draw(data, options);
    }
</script>
<div id="divchart" style="width: 400px; height: 200px;">
</div>

Now we have done run the application for final output.



 DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top