ASP.NET MVC 5 - Customizing Bar Chart Using JavaScript C3 Chart Library

Introduction
 
In my previous article, we discussed how to generate a C3 Bar Chart in our ASP.NET MVC5 web application with JSON data using Entity Framework. Now, let us discuss different ways of customization on a Bar Chart using C3 Chart JavaScript Library. The result from the previous article is below,
 
We have seen how to generate a bar chart and a stacked bar chart. Now, let's move forward to do customization on the Bar chart.
 
Customization
 
In the above result, we can see that the x-axis named as auto generates numbers 0,1,3, etc. But here we need to give the proper name for each bar, right? Now, let's give a name to each bar. Here, I planned to generate the stacked bar chart by month and year. So, we need to write the logic to group the data before rendering. So, first let's create a stored procedure with this logic.
 
Stored Procedure  
  1. CREATE PROCEDURE GenderPopulation  
  2. AS  
  3. BEGIN  
  4. -- SET NOCOUNT ON added to prevent extra result sets from  
  5. -- interfering with SELECT statements.  
  6. SET NOCOUNT ON;  
  7. Select Country,SUM(Male) as Male,SUM(Female) as Female,SUM(Others) as Others,FORMAT(MonthAndYear,'MMM-yyyy'AS MonthYr from [dbo].[Population]group by FORMAT(MonthAndYear,'MMM-yyyy'), Country  
  8. END  
  9. GO  
Here, I am grouping the data by month and year, in the below image you can see the result. Now, we will give the MonthYr column data as x-axis name for each bar.
 
 
 
Execute the stored procedure from our MVC controller using entity framework SQL Query as below,
  1. public ActionResult BarChart() {  
  2.     CSharpCornerEntities1 entities = new CSharpCornerEntities1();  
  3.     var test = entities.Database.SqlQuery < GenderPopulation > ("exec GenderPopulation").ToList();  
  4.     return Json(test, JsonRequestBehavior.AllowGet);  
  5. }  
We are binding the returned data to our custom model, the custom model named it as  "GenderPopulation" and it's created as below,
  1. public class GenderPopulation {  
  2.     public Nullable < double > Male {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public Nullable < double > Female {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public Nullable < double > Others {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public string Country {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public string MonthYr {  
  19.         get;  
  20.         set;  
  21.     }  
  22. }  
In the script, we will put this date in array  catagories in each bar. Here, the jsondata represents a success response from our controller,
  1. var MonthYr = [];  
  2. jsondata.forEach(function(e) {  
  3.     MonthYr.push(e.MonthYr);  
  4. })  
Now, the MonthYr array holds a list of grouped dates. Now, we have to catagories the chart in the x-axis. In the below code type 'category' is the predefined keyword and the next parameter categories value is our array.
  1. axis: {  
  2.     x: {  
  3.         type: 'category',  
  4.         categories: MonthYr,  
  5.     }  
  6. },  
Now, run the application, In the below image we can see that we have got the required name for each bar.
 
 
Types 
 
Now, we can check the male population from start date to the last date, so we can use the type param to see that. Here, I wanted to see the the male population. 
  1. types: {  
  2.    Male: 'area',  
  3. },   
Now, run your application, take a look at the below image, the male population is clear to see from the start date to the last date.
 
 
Now, we shall add the female in this type with 'area-spline' type.
  1. types: {  
  2.    Male: 'area',  
  3.    Female: 'area-spline'  
  4. },   
Run your application, now we can can see who has a higher  count.
 
 
axis.x.tick.rotate
 
Our x-axis name is not a single line, right? Now, we are going to rotate those x-axis catagory, as per our need so we increase or decrease the rotation length, 
  1. axis: {  
  2.     x: {  
  3.         type: 'category',  
  4.         categories: MonthYr,  
  5.         tick: {  
  6.             rotate: 50  
  7.         },  
  8.     }  
  9. },  
Run your application. In the below result we can see that x-axis catagory has been rotated,
 
 
Complete controller
  1. using Piechart.Models;  
  2. using System;  
  3. using System.Linq;  
  4. using System.Web.Mvc;  
  5. namespace Barchart.Controllers {  
  6.     public class ChartController: Controller {  
  7.         // GET: Chart  
  8.         public ActionResult Index() {  
  9.             return View();  
  10.         }  
  11.         public ActionResult BarChart() {  
  12.             CSharpCornerEntities1 entities = new CSharpCornerEntities1();  
  13.             var test = entities.Database.SqlQuery < GenderPopulation > ("exec GenderPopulation").ToList();  
  14.             return Json(test, JsonRequestBehavior.AllowGet);  
  15.         }  
  16.         public class GenderPopulation {  
  17.             public Nullable < double > Male {  
  18.                 get;  
  19.                 set;  
  20.             }  
  21.             public Nullable < double > Female {  
  22.                 get;  
  23.                 set;  
  24.             }  
  25.             public Nullable < double > Others {  
  26.                 get;  
  27.                 set;  
  28.             }  
  29.             public string Country {  
  30.                 get;  
  31.                 set;  
  32.             }  
  33.             public string MonthYr {  
  34.                 get;  
  35.                 set;  
  36.             }  
  37.         }  
  38.     }  
View and Script
  1. @ {  
  2.     ViewBag.Title = "Index";  
  3. } < br / > < br / > < div id = "Barchart" > < /div> < script type = "text/javascript" > $(document).ready(function() {  
  4.     $.ajax({  
  5.         type: "GET",  
  6.         url: "/Chart/BarChart",  
  7.         data: {},  
  8.         contentType: "application/json; charset=utf-8",  
  9.         dataType: "json",  
  10.         success: function(response) {  
  11.             successFunc(response);  
  12.         },  
  13.     });  
  14.   
  15.     function successFunc(jsondata) {  
  16.         var MonthYr = [];  
  17.         jsondata.forEach(function(e) {  
  18.             MonthYr.push(e.MonthYr);  
  19.         })  
  20.         var chart = c3.generate({  
  21.             bindto: '#Barchart',  
  22.             data: {  
  23.                 json: jsondata,  
  24.                 keys: {  
  25.                     value: ['Male''Female''Others'],  
  26.                 },  
  27.                 columns: ['Male''Female''Others'],  
  28.                 type: 'bar',  
  29.                 groups: [  
  30.                     ['Male''Female''Others']  
  31.                 ],  
  32.                 types: {  
  33.                     Male: 'area',  
  34.                     Female: 'area-spline'  
  35.                 },  
  36.             },  
  37.             axis: {  
  38.                 x: {  
  39.                     type: 'category',  
  40.                     categories: MonthYr,  
  41.                     tick: {  
  42.                         rotate: 50  
  43.                     },  
  44.                 }  
  45.             },  
  46.             color: {  
  47.                 pattern: ['#1f77b4''#aec7e8''#ff7f0e''#ffbb78''#2ca02c''#98df8a''#d62728''#ff9896''#9467bd''#c5b0d5''#8c564b''#c49c94''#e377c2''#f7b6d2''#7f7f7f''#c7c7c7''#bcbd22''#dbdb8d''#17becf''#9edae5']  
  48.             },  
  49.         });  
  50.     }  
  51. }); < /script> 
Stored Procedure 
  1. CREATE PROCEDURE GenderPopulation  
  2. AS  
  3. BEGIN  
  4. -- SET NOCOUNT ON added to prevent extra result sets from  
  5. -- interfering with SELECT statements.  
  6. SET NOCOUNT ON;  
  7. Select Country,SUM(Male) as Male,SUM(Female) as Female,SUM(Others) as Others,FORMAT(MonthAndYear,'MMM-yyyy'AS MonthYr from [dbo].[Population]group by FORMAT(MonthAndYear,'MMM-yyyy'), Country  
  8. END  
  9. GO  
  10.    
  11. exec GenderPopulation 
Refer to the attached project for reference and I did attach the demonstrated project without a package due to the size limit.
 
Summary

In this article, we have seen the different ways of customization on a Bar chart using C3 Chart JavaScript library and chart transmission.

I hope you enjoyed the article. Your valuable feedback and comments about this article are always welcome.


Similar Articles