Introduction
In this article, we shall learn how to generate charts like Bar charts, Line charts, and Pie charts in an MVC web application dynamically using Chart.js which is an important JavaScript library for generating charts. Chart.js is a simple and flexible charting option that provides easy implementation to web developers and designers. We can generate almost every type of chart using chart.js. We shall implement it step by step for generating the chart, as shown below.
Let's get started now.
Create MVC Web Application
To create an MVC web application, first, open Visual Studio and click on File >> New project, as shown in the below image.

It will open a new window as shown below. Here, you have to click on the web option. Now, give the name of the project and set the location of the project where you want to save the file.

Now, to select the project template, you have to click on the MVC template and I have used "No Authentication" as highlighted below.

Now, the application has already been created and all the files as per the MVC web template are also created in the application. It looks like below.

Adding Controller to the Application
To add a controller, right-click on the Controllers folder available in the Solution Explorer and select the "Add Controller" option.
Add >> Controller.

The new next window will open where choose the option: MVC 5 Controller - Empty and click on the "Add" button, as shown below.

Now finally, you have to give the name of the Controller and click on the "Add" button. I have given the name of the controller as ChartInMVCRuntimeController. Here, you do not have to remove the word Controller from the controller name as it is the default.

Now, the Controller has been added and we can see it in the Controllers folder available in Solution Explorer.

Adding View to the Application
First, right-click on the Views folder and select the "Add New Folder" option as shown below. Here, give the name of the folder as RuntimeChart as I have given in my project.

Now, right-click on the newly created folder RuntimeChart and select the "Add View" option, as shown below.

Now, give the name of the View as below.

Now, the new View has been added which can be shown in Solution Explorer, as shown below.

Calling Action To Return View
Now, modify the Index action as shown below to return the view which will display on the browser.
public ActionResult Index()
{
return View("~/Views/RuntimeChart/ChartInMVC.cshtml");
}
Adding Table and TextBox to View
To add a simple HTML table and a textbox for entering the value, just copy the below HTML code and paste it to the new View that you have just added.
The below HTML code also contains three separate divs which have been used to generate the chart dynamically later.
@{
ViewBag.Title = "ChartInMVC";
}
<style type="text/css">
label {
font-weight: normal;
font-size: 11px;
vertical-align: middle;
}
body {
color: black;
}
textbox {
font-weight: normal;
font-size: 11px;
vertical-align: middle;
color: black;
}
.form-control, select {
font-weight: normal;
font-size: 11px;
}
.container {
width: 80%;
margin: 15px auto;
color: black;
}
</style>
@using (Html.BeginForm("", "", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-horizontal">
<div class="row">
<div class="col-md-12">
<div class="form-horizontal">
<div class="row">
<div class="col-md-12">
<div class="col-md-4">
<div class="form-group">
<div class="col-md-8">
<label class="control-label">Enter Your Expenses Below</label>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="col-md-12">
<div class="form-group">
<div class="col-md-12">
<table class="table table-bordered table-responsive">
<thead>
<tr>
<th scope="col">
<label class="control-label">Education</label>
</th>
<th scope="col">
<label class="control-label">Lodging</label>
</th>
<th scope="col">
<label class="control-label">Fooding</label>
</th>
<th scope="col">
<label class="control-label">Travelling</label>
</th>
<th scope="col">
<label class="control-label">Communication</label>
</th>
<th scope="col">
<label class="control-label">Others</label>
</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">
<input type="text" class="form-control" id="txtEducation" />
</th>
<th scope="row">
<input type="text" class="form-control" id="txtLodging" />
</th>
<th scope="row">
<input type="text" class="form-control" id="txtFooding" />
</th>
<th scope="row">
<input type="text" class="form-control" id="txtTravelling" />
</th>
<th scope="row">
<input type="text" class="form-control" id="txtCommunication" />
</th>
<th scope="row">
<input type="text" class="form-control" id="txtOthers" />
</th>
</tr>
<tr>
<th scope="row" colspan="2">
<div id="chartContainerPie" class="containerPie">
<canvas id="myChart"></canvas>
</div>
</th>
<th scope="row" colspan="2">
<div id="chartContainer" class="container">
<canvas id="myChartPie"></canvas>
</div>
</th>
<th scope="row" colspan="2">
<div id="chartContainerLine" class="containerLine">
<canvas id="myChartLine"></canvas>
</div>
</th>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
}



IMAD AYOUBPosted Apr 2, 2021, 9:48 AM
Perfect article. Thanks a lot.
Laxmidhar SahooPosted Nov 26, 2018, 10:45 PM
Thanks This will hepl me
Former memberPosted Nov 2, 2018, 3:51 AM
Nice article ..Thanks for sharing