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

Introduction
 
In my previous article, we discussed how to generate a C3 Pie Chart in our ASP.NET MVC5 web application with JSON data using Entity Framework. Now, let us discuss different ways of customization on a Pie Chart using C3 Chart JavaScript Library. The result from the previous article is below,
 
 
 
Now, let's move forward to do customization on the pie chart.
 
Customization 
 
Legend
 
A legend is displayed on the bottom of the pie chart. In the above image, you can see that the legends are displayed as India, UK, and the US. Now, we will try to change the legend position. C3 Chart supports 3 type of positions, such as bottom, right, and inset. Now, we will see how to mention the legend type. By default, the legend position is at the bottom.
  1. legend: {  
  2.    position: 'right'  
  3. },  
 
 
Now, let's hide those(India, Uk, and Us) legends. By default, the legend is enabled on every chart and the value is true. Let's change it to "False" to hide the legend from the respective chart.
  1. legend: {  
  2.     show: false,  
  3. },  
Now, the legends are hidden in the below image.
 
  
Tooltip
 
In the first image, we can see that we are getting the tooltip on each country portion. During hover, it gives the country name and population. Now, we will try to add the country as a caption. So, let's add the title for the tooltip as below,
  1. tooltip: {  
  2.         format: {  
  3.             title: function(x) {  
  4.                 return 'Country ';  
  5.             }  
  6.         }  
The title will be common for every tooltip on our pie chart. If we wanted to show a unique title on every tooltip, we can write that over here.
 
 
 
Interaction
 
We should set true when we want to do some interaction with the chart. If false is set, all of the interactions (showing/hiding tooltip, selection and all kind of events on charts) will be disabled.
 
 
 
Label 
 
In the pie chart, a label is in disabled by default. And now we will change the pie chart label format. Here, I am going to convert it as the number and $(dollar) format.
  1. pie: {  
  2.     label: {  
  3.         show: true,  
  4.         format: function(value, ratio, id) {  
  5.             return d3.format('$')(value);  
  6.         }  
  7.     }  
  8. },  
 
 
Chart type
 
Sometime we will be in the position to change the existing chart layout. Just assume one client asks us to change this pie chart to a donut chart. In this situation, we can easily achieve it without more effort like below,
 
type: 'pie',=> type: 'donut', 
 
 
 
You can see that the above chart has been changed from pie to donut.
 
Complete View 
  1. @ {  
  2.     ViewBag.Title = "Index";  
  3. } < h2 style = "position:center" > Pie Chart < /h2> < div id = "pieChart" > < /div> < script type = "text/javascript" > $(document).ready(function() {  
  4.     var MSiteName = [];  
  5.     var MasterJson;  
  6.     $.ajax({  
  7.         type: "GET",  
  8.         url: "/Chart/BarChart",  
  9.         data: {},  
  10.         contentType: "application/json; charset=utf-8",  
  11.         dataType: "json",  
  12.         success: function(response) {  
  13.             successFunc(response);  
  14.         },  
  15.     });  
  16.   
  17.     function successFunc(jsondata) {  
  18.         var data = {};  
  19.         var countryNames = [];  
  20.         jsondata.forEach(function(e) {  
  21.             countryNames.push(e.CountryName);  
  22.             data[e.CountryName] = e.CountryPopulation;  
  23.         })  
  24.         var chart = c3.generate({  
  25.             bindto: '#pieChart',  
  26.             data: {  
  27.                 json: [data],  
  28.                 keys: {  
  29.                     value: countryNames,  
  30.                 },  
  31.                 type: 'donut',  
  32.             },  
  33.             legend: {  
  34.                 show: true,  
  35.                 position: 'right'  
  36.             },  
  37.             tooltip: {  
  38.                 format: {  
  39.                     title: function(x) {  
  40.                         return 'Country ';  
  41.                     }  
  42.                 }  
  43.             },  
  44.             interaction: {  
  45.                 enabled: false  
  46.             },  
  47.             pie: {  
  48.                 label: {  
  49.                     show: true,  
  50.                     format: function(value, ratio, id) {  
  51.                         return d3.format('$')(value);  
  52.                     }  
  53.                 }  
  54.             },  
  55.             color: {  
  56.                 pattern: ['#1f77b4''#aec7e8''#ff7f0e''#ffbb78''#2ca02c''#98df8a''#d62728''#ff9896''#9467bd''#c5b0d5''#8c564b''#c49c94''#e377c2''#f7b6d2''#7f7f7f''#c7c7c7''#bcbd22''#dbdb8d''#17becf''#9edae5']  
  57.             },  
  58.         });  
  59.     }  
  60. }); < /script>  
Complete Controller 
  1. using Piechart.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7. namespace Piechart.Controllers {  
  8.     public class ChartController: Controller {  
  9.         // GET: Chart  
  10.         public ActionResult Index() {  
  11.             return View();  
  12.         }  
  13.         public ActionResult BarChart() {  
  14.             CSharpCornerEntities entities = new CSharpCornerEntities();  
  15.             return Json(entities.Countries.ToList(), JsonRequestBehavior.AllowGet);  
  16.         }  
  17.     }  
  18. }  
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 way of customization on the pie chart using C3 Chart JavaScript library and chart transmission.
 
I hope you enjoyed this article. Your valuable feedback and comments about this article are always welcome.


Similar Articles