Once you complete these steps, you will get the screen as below.
Configure Entity Framework with database and application
Here, I have already discussed how to configure and implement a database-first approach. In the meantime, choose your created table with Entity Framework. Once we do our configuration with SQL table "Population" from CSharpCorner database and with our application, we will get the below screen as the succeeding configuration,
Create a Controller and View
Now, create an empty Controller and View. Here, I created a Controller with the name of "ChartController". Whenever we create an empty Controller, it is created with an empty Index action method. And create an empty View of this action method "Index".
Install C3 chart from NuGet and enable it in our application
Generate a Chart Controller
Now, write a logic in your Controller to fetch the data from the database and return that data as JSON to the View. In the below code, you can see that I have created a "BarChart" action to fetch the data from the database using Entity Framework.
- using Piechart.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace Barchart.Controllers {
- public class ChartController: Controller {
-
- public ActionResult Index() {
- return View();
- }
- public ActionResult BarChart() {
- CSharpCornerEntities1 entities = new CSharpCornerEntities1();
- return Json(entities.Populations.ToList(), JsonRequestBehavior.AllowGet);
- }
- }
- }
View
In View, add a Controller which will act as a bar chart in our application. For that, I have added one div with the name of Barchart.
- <div id="Barchart"></div>
Script
Now, write the logic in jQuery AJAX to get the JSON data from your controller action. In the below code, you can see that we are trying to retrieve the data from BarChart action under chart controller.
- $.ajax({
- type: "GET",
- url: "/Chart/BarChart",
- data: {},
- contentType: "application/json; charset=utf-8",
- dataType: "json",
- success: function(response) {
- successFunc(response);
- },
- });
After getting JSON data from our controller, bind the JSON data to chart as below,
- c3.generate({ -> This is the predefined keyword c3 charts to generate charts on respective controls
- bindto: '#Barchart', -> Changing our div control to Bar chart control or all bar chart features will bind to this control
- json: jsondata,-> We metioned to pass all Genderwise population data in json format
- keys: { value: Male, Female, Others-> We are getting values of males, females, and others from json data; that means population data
- type: 'bar' -> It's to mention the type of chart wanted to generate
- color: {pattern: -> Combination color will be applied to our chart
- function successFunc(jsondata) {
- var chart = c3.generate({
- bindto: '#Barchart',
- data: {
- json: jsondata,
- keys: {
- value: ['Male', 'Female', 'Others'],
- },
- columns: ['Male', 'Female', 'Others'],
- type: 'bar',
- },
- color: {
- pattern: ['#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c', '#98df8a', '#d62728', '#ff9896', '#9467bd', '#c5b0d5', '#8c564b', '#c49c94', '#e377c2', '#f7b6d2', '#7f7f7f', '#c7c7c7', '#bcbd22', '#dbdb8d', '#17becf', '#9edae5']
- },
- });
- }
Now Run your application,
In the result, we can see that we got the chart as per the data. Now let's do some customization on this chart.
Customize Chart
We have many records for a year, so now we will try to show the record in a single bar. We achieve that using a stacked bar chart. We have to use the groups and need to mention the columns to merge in a single bar.
- groups: [
- ['Male', 'Female','Others']
- ]
Now run your application,
The grouping will happen by mentioning the column name, if you want to do two column groupings, you can remove the respective column from that column array. We will do more customization on the bar chart in the next article.
Complete View
- @ {
- ViewBag.Title = "Index";
- } < br / > < div id = "Barchart" > < /div> < script type = "text/javascript" > $(document).ready(function() {
- $.ajax({
- type: "GET",
- url: "/Chart/BarChart",
- data: {},
- contentType: "application/json; charset=utf-8",
- dataType: "json",
- success: function(response) {
- successFunc(response);
- },
- });
-
- function successFunc(jsondata) {
- var chart = c3.generate({
- bindto: '#Barchart',
- data: {
- json: jsondata,
- keys: {
- value: ['Male', 'Female', 'Others'],
- },
- columns: ['Male', 'Female', 'Others'],
- type: 'bar',
- groups: [
- ['Male', 'Female', 'Others']
- ]
- },
- color: {
- pattern: ['#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c', '#98df8a', '#d62728', '#ff9896', '#9467bd', '#c5b0d5', '#8c564b', '#c49c94', '#e377c2', '#f7b6d2', '#7f7f7f', '#c7c7c7', '#bcbd22', '#dbdb8d', '#17becf', '#9edae5']
- },
- });
- }
- }); < /script>
Controller
- using Piechart.Models;
- using System.Linq;
- using System.Web.Mvc;
- namespace Barchart.Controllers {
- public class ChartController: Controller {
-
- public ActionResult Index() {
- return View();
- }
- public ActionResult BarChart() {
- CSharpCornerEntities1 entities = new CSharpCornerEntities1();
- return Json(entities.Populations.ToList(), JsonRequestBehavior.AllowGet);
- }
- }
- }
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 how to generate a C3 Bar Chart in our ASP.NET MVC5 web application with JSON data using Entity Framework.
I hope you enjoyed this article. Your valuable feedback and comments about this article are always welcome.