Introduction

In this post, I will explain to you how to create charts with jqPlot jQuery Plugin in ASP.NET MVC 4 using C# and Entity Framework, JSON.

What is JqPlot?

jqPlot is a plotting and charting plugin for the jQuery Javascript framework. jqPlot produces beautiful line, bar, and pie charts, with many features:

In this example, we will see how to use PieChart jqPlot.

SQL Database part

Create Table

You will find the table, given below, used in our application:

Table

After creating the table, you can fill it using the data, as shown below:

Table

Create your MVC application

First, Open Visual Studio. Click on File > New > Project and name your project, as shown below:

 MVC application

 MVC application

Creating ADO.NET Entity Data Model

In this level, we need to create an Entity Data Model which allows us to retrieve data from database.

Right click on the project name. Click Add > Add New Item. In the dialog box displayed, select Data > Click Add button.

ADO.NET Entity Data Model

ADO.NET Entity Data Model

After clicking Next button, the dialog box will be displayed, as shown below. You need to choose your server name and select your database.

database

Finally, we see that EDMX model generates Populations class.

EDMX model

Create a controller

Now, we proceed to creating a controller. Right click on the controller folder > Add > Controller > Enter Controller name (‘Home Controller’).

Create a controller

HomeController.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace JqPlot_ASP.NET_MVC4.Controllers
  7. {
  8. public class HomeController : Controller
  9. {
  10. //DbContext
  11. private Db_PersonEntities db = new Db_PersonEntities();
  12. // PieChart
  13. //
  14. // GET: /Home/
  15. public ActionResult PieRenderer()
  16. {
  17. return View();
  18. }
  19. public JsonResult GetPieRenderer()
  20. {
  21. var DbResult = from d in db.Populations
  22. select new
  23. {
  24. d.COUNTRY_NAME_P,
  25. d.POPULATION_P
  26. };
  27. return Json(DbResult, JsonRequestBehavior.AllowGet);
  28. }
  29. }
  30. }
As you can see, I am creating GetPieRenderer() action to retrieve the data from Populations table as JSON format.

Creating a strongly typed view

We are going to create a strongly typed View.

view

PieRenderer.cshtml
  1. @model JqPlot_ASP.NET_MVC4.Populations
  2. @{
  3. ViewBag.Title = "PieRenderer";
  4. }
  5. <h2>PieRenderer</h2>
  6. <div id="chartdiv" style="height: 400px; width: 500px;"></div>
  7. @section scripts {
  8. <!-- CSS -->
  9. <link href="~/Content/jquery.jqplot.min.css" rel="stylesheet" />
  10. <!-- Scripts JS -->
  11. <script src="~/Scripts/jquery-1.7.1.min.js"></script>
  12. <script src="~/Scripts/jquery.jqplot.min.js"></script>
  13. <script src="~/Scripts/jqplot.pieRenderer.js"></script>
  14. <script type="text/javascript">
  15. $(document).ready(function () {
  16. $.ajax({
  17. type: "POST",
  18. url: "Home/GetPieRenderer",
  19. contentType: "application/json; charset=utf-8",
  20. dataType: "json",
  21. success: OnSuccess,
  22. error: OnError
  23. });
  24. function OnSuccess(response) {
  25. var aData = response;
  26. var dataArray = [];
  27. $.each(aData, function (i, item) {
  28. dataArray.push([item.COUNTRY_NAME_P, item.POPULATION_P]);
  29. });
  30. var plot1 = jQuery.jqplot('chartdiv', [dataArray],
  31. {
  32. seriesDefaults: {
  33. // Make this a pie chart.
  34. renderer: jQuery.jqplot.PieRenderer,
  35. rendererOptions: {
  36. sliceMargin: 4,
  37. // Put data labels on the pie slices.
  38. showDataLabels: true
  39. }
  40. },
  41. legend: { show: true, location: 'e' }
  42. }
  43. );
  44. }
  45. function OnError(response) {
  46. alert("Error !");
  47. }
  48. });
  49. </script>
  50. }
Output

Output

For any details related jqPlot, you can use the link below:

URL: jqplot