Using jqxChart With ASP.NET MVC 4

In this article, we will see how we can use jqxChart in MVC application.

Introduction
 
In this post, I will explain to you how we can use jqxChart control in MVC4 application, using C# and Entity Framework, JSON.
 
What is jqxChart?
 
jqxChart is an easy to use chart widget based on the popular jQuery library. It is written 100 percent in JavaScript and uses W3C standards, like HTML5, CSS and SVG. jqxChart provides optimal chart rendering performance and high quality visualization across browsers and devices (PCs, Laptops, Tablets and Mobile phones).
 
Set up database
 
Firstly, we need to create a table, as shown below:
 
 
 
In the next step, you can fill it with the following rows.
 
 
 
Create your MVC application
 
Open Visual Studio. Click on File > New > Project and name your project, as shown below:
 
 
 
 
 
Creating ADO.NET Entity Data Model
 
In this step, we proceed to generate our Entity Data Model.
 
Right click on the project name. Click Add > Add New Item. In the dialog displayed, select Data and click Add button.
 
 
 
Now, select your server name and your database.
 
 
 
If you have followed all steps described above, you will see that EDMX model generates Browsers class.
 
 
Create a Controller
 
Right click on the controller folder > Add > Controller > Enter Controller name (‘HomeController’).
 
 
HomeController.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace jqxChartMVC4.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.         //  
  12.         // GET: /Home/  
  13.   
  14.         public ActionResult Index()  
  15.         {  
  16.             return View();  
  17.         }  
  18.   
  19.         //Db Context  
  20.         private Db_PersonEntities db = new Db_PersonEntities();  
  21.   
  22.   
  23.         public JsonResult GetBrowsers()  
  24.         {  
  25.             var DbResult = from d in db.Browsers  
  26.                            select new  
  27.                            {  
  28.                                d.Browser,  
  29.                                d.Share  
  30.                            };  
  31.   
  32.             return Json(DbResult, JsonRequestBehavior.AllowGet);  
  33.         }  
  34.   
  35.     }  
  36. }  
Creating a strongly typed View
 
 
 
Index.cshtml 
  1. @model jqxChartMVC4.Browsers  
  2.   
  3. @{  
  4.     ViewBag.Title = "Pie Series";  
  5. }  
  6.   
  7. <h2>Browsers Share</h2>  
  8.   
  9. <div id="chartDiv" style="width:700px; height:400px;">  
  10. </div>  
  11.   
  12. @section scripts{  
  13.   
  14.     <!-- CSS -->  
  15.     <link href="~/Content/jqx.base.css" rel="stylesheet" />  
  16.   
  17.      
  18.   
  19.   
  20.  <!-- Scripts JS that should be included for using jqxChart control -->  
  21.   
  22.     <script src="~/Scripts/jquery-1.7.1.min.js"></script>  
  23.     <script src="~/Scripts/jqxcore.js"></script>  
  24.     <script src="~/Scripts/jqxdraw.js"></script>  
  25.     <script src="~/Scripts/jqxchart.core.js"></script>  
  26.     <script src="~/Scripts/jqxdata.js"></script>  
  27.   
  28.   
  29.     <script type="text/javascript">  
  30.         $(document).ready(function () {  
  31.   
  32.             // prepare chart data as an array  
  33.             var source =  
  34.             {  
  35.                 datatype: "json",  
  36.                 datafields: [  
  37.                     { name: 'Browser' },  
  38.                     { name: 'Share' }  
  39.                 ],  
  40.   
  41.                 // calling GetBrowsers action  
  42.                 url: 'Home/GetBrowsers '  
  43.             };  
  44.             var dataAdapter = new $.jqx.dataAdapter(source);  
  45.   
  46.             // prepare jqxChart settings  
  47.             var settings = {  
  48.                 title: "Browsers Share",  
  49.                 description: "",  
  50.                 enableAnimations: true,  
  51.                 showLegend: false,  
  52.                 showBorderLine: true,  
  53.                 legendPosition: { left: 520, top: 140, width: 100, height: 100 },  
  54.                 padding: { left: 5, top: 5, right: 5, bottom: 5 },  
  55.                 titlePadding: { left: 0, top: 0, right: 0, bottom: 10 },  
  56.                 source: dataAdapter,  
  57.                 colorScheme: 'scheme02',  
  58.                 seriesGroups:  
  59.                     [  
  60.                         {  
  61.                             type: 'donut',  
  62.                             showLabels: true,  
  63.                             series:  
  64.                                 [  
  65.                                     {  
  66.                                         dataField: 'Share',  
  67.                                         displayText: 'Browser',  
  68.                                         labelRadius: 120,  
  69.                                         initialAngle: 15,  
  70.                                         radius: 170,  
  71.                                         innerRadius: 70,  
  72.                                         centerOffset: 0,  
  73.                                         formatSettings: { sufix: '%', decimalPlaces: 1 }  
  74.                                     }  
  75.                                 ]  
  76.                         }  
  77.                     ]  
  78.             };  
  79.             // setup the chart  
  80.             $('#chartDiv').jqxChart(settings);  
  81.   
  82.         });  
  83.     </script>  
  84.   
  85. }  
Output
 
 
For more details related to jqxChart, you can use the following link:
 


Similar Articles