Introduction

In this post, I will explain to you how to create charts with ChartistJS plugin with ASP.NET MVC 4, using C#, Entity framework and JSON.

What is CHARTIST?

Let’s start to create our first responsive chart, using ChartistJS in ASP.NET MVC.

Download ChartistJS library and CSS files

You can download the library included in JS and CSS files directly from the Chartist-js Website.

  1. <!-- CSS -->
  2. <link href="~/Content/chartist.min.css" rel="stylesheet" />
  3. <!-- Scripts JS -->
  4. <script src="~/Scripts/chartist.min.js"></script>
  5. <script src="~/Scripts/jquery-1.7.1.min.js"></script>
SQL database part

Create Table

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

Create Table

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

Create Table

Create your MVC Application

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

MVC application

MVC application

Creating ADO.NET Entity Data Model

Now, we need to create an Entity Data Model, which allows us to retrieve the data from the 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.

server name

Finally, we see that EDMX model will generate Populations class.

class

Create a controller

We proceed to create a controller. Right click on the controller folder > Add > Controller > enter Controller name (‘Home Controller’).

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 ChartistJSMVC4.Controllers
  7. {
  8. public class HomeController : Controller
  9. {
  10. //DbContext
  11. private Db_PersonEntities db = new Db_PersonEntities();
  12. //
  13. // GET: /Home/
  14. public ActionResult Index()
  15. {
  16. return View();
  17. }
  18. public ActionResult GetPopulation()
  19. {
  20. var DbResult = from d in db.Populations
  21. select new
  22. {
  23. d.COUNTRY_NAME_P,
  24. d.POPULATION_P
  25. };
  26. return Json(DbResult, JsonRequestBehavior.AllowGet);
  27. }
  28. }
  29. }
As you can see, I am creating GetPopulation () action to retrieve the data from Populations table in JSON format.

Creating a strongly typed view

We are going to create a strongly typed view.

strongly typed view

Index.cshtml
  1. @model ChartistJSMVC4.Populations
  2. @{
  3. ViewBag.Title = "Chartist MVC 4";
  4. }
  5. <h2>Chartist MVC 4 </h2>
  6. <div class="ct-chart ct-golden-section"></div>
  7. @section scripts{
  8. <!-- CSS -->
  9. <link href="~/Content/chartist.min.css" rel="stylesheet" />
  10. <!-- Scripts JS -->
  11. <script src="~/Scripts/chartist.min.js"></script>
  12. <script src="~/Scripts/jquery-1.7.1.min.js"></script>
  13. <script type="text/javascript">
  14. $(document).ready(function () {
  15. $.ajax({
  16. type: "POST",
  17. url: "Home/GetPopulation",
  18. contentType: "application/json; charset=utf-8",
  19. dataType: "json",
  20. success: OnSuccess,
  21. error: OnError
  22. });
  23. function OnSuccess(response) {
  24. var result = response;
  25. var arrayLabels = [], arraySeries = [];
  26. $.map(result, function (item, index) {
  27. arrayLabels.push(item.COUNTRY_NAME_P);
  28. arraySeries.push(item.POPULATION_P);
  29. });
  30. var data = {
  31. labels: arrayLabels,
  32. series: arraySeries
  33. }
  34. new Chartist.Pie('.ct-chart', data, { donut: true });
  35. }
  36. function OnError(response) {
  37. alert("Error !");
  38. }
  39. });
  40. </script>
  41. }
Output

Output

For any details related to ChartistJS, you can use the link, given below:

URL: Chartist-js