Introduction

In our previous article we have seen in detail about how to draw Bar Chart in MVC web Application. In this article we will see how to draw Line Chart for MVC application using HTML5 Canvas, JQuery, WEB API, and AngularJS.
In this series we will see one-by-one in detail starting from:
- MVC Dynamic Bar Chart using WEB API, AngularJS and JQuery
- MVC Dynamic Line Chart using WEB API, AngularJS and JQuery
- MVC Dynamic Pie Chart using WEB API, AngularJS and JQuery
- MVC Dynamic Line&Bar Chart using WEB API, AngularJS and JQuery
- MVC Dynamic Donut Chart using WEB API, AngularJS and JQuery
- MVC Dynamic Bubble Chart using WEB API, AngularJS and JQuery
Our Chart Features

- Chart Source Data: Using WEB API and AngularJS we will be loading chart data from database to a Combobox. In our JQuery we will be plotting chart details from the Combobox.
- Chart Number of Category: Chart Items will be dynamically loaded from database. Here we will plot all the Items in Combobox. It’s good to plot fewer than 12 items per chart.
- Chart Title Text: User can add their own Chart Title and dynamically change the titles if required. Here in our example we will draw the Title Textbox text at the Bottom of the Chart. (User can redesign and customize as per your requirements if needed).
- Chart Water Mark Text: In some cases we need to add our Company name as Water Mark to our Chart. Here in our example we will draw the Water mark Textbox text at the center of the Chart. (User can redesign and customize as per your requirements if needed).
- Chart Company LOGO: User can add their own Company logo to the Chart.(Here for sample we have added my own image as a Logo at the Top left corner.( User can redesign and customize as per your requirements if needed.).
- Chart Alert Image Display: If the “Alert On” radio button is checked we will display the Alert Image. If the “Alert Off” radio button is clicked then the Alert Image will not be displayed. In JQuery we have declared alertCheckValue = 90; and we check the plot data with this aleartcheckValue and if the plot value is greater than this check value then we will display the alert image in the legend.
What is the use of Alert Image?
Let’s consider a real time project. For example we need to display the chart for a Manufacturing factory with production result as Good and Bad. For example if production result for each quality value is above 90 we need to display the Alert green Image and if the quality value is below 90 then we need to display the Red Image with label bars.
This Alert Image will be easy to identify each quality result with good or bad. (Here for a sample we have used for quality check and display green and red image, but users can customize as per your requirement and add your own image and logics.)
- Save Chart as Image: User can save the chart as Image.
- Chart Theme: Here we have created 2 themes; Blue and Green for our Chart. We can see both theme outputs here. User can also add any numbers of theme as they require.
Blue Theme

Green Theme

In this Article we have two parts:
- Chart Item and Value Insert/Update to database, Select Chart Item and Value to Combobox from database using WEB API, AngularJS.
- Using JQuery Draw our own Chart to HTML5 Canvas tag in our MVC page.
Prerequisites
- Visual Studio 2015: You can download it from here.
Code Part
In Code part we can see three steps:
Step 1: Explains about how to create a sample Database, Table, Stored procedure to select, Insert and Update Chart data to SQL Server.
Step 2: Explains about how to create a WEB API to get the data and bind the result to MVC page using AngularJS.
Step 3: Explains about how to draw our own Chart to our MVC Web application using JQuery.
Step 1: Script to create Table and Stored Procedure
We will create a ItemMaster table under the Database ‘ItemsDB. The following is the script to create a database, table and sample insert query. Run this script in your SQL Server. I have used SQL Server 2014.
- USEMASTER
- GO
- -- 1) Check for the Database Exists .If the database is exist then drop and create new DB
- IFEXISTS(SELECT [name] FROMsys.databasesWHERE [name] ='ItemsDB')
- DROPDATABASE ItemsDB
- GO
- CREATEDATABASE ItemsDB
- GO
- USE ItemsDB
- GO
- -- 1) //////////// Item Masters
- IFEXISTS(SELECT [name] FROMsys.tablesWHERE [name] ='ItemMaster')
- DROPTABLE ItemMaster
- GO
- CREATETABLE [dbo].[ItemMaster](
- [ItemID] INTIDENTITYPRIMARYKEY,
- [ItemName] [varchar](100)NOTNULL,
- [SaleCount] [varchar](10)NOTNULL
- )
- -- insert sample data to Item Master table
- INSERTINTO ItemMaster([ItemName],[SaleCount])
- VALUES ('Item1','100')
- INSERTINTO ItemMaster([ItemName],[SaleCount])
- VALUES ('Item2','82')
- INSERTINTO ItemMaster([ItemName],[SaleCount])
- VALUES ('Item3','98')
- INSERTINTO ItemMaster([ItemName],[SaleCount])
- VALUES ('Item4','34')
- INSERTINTO ItemMaster([ItemName],[SaleCount])
- VALUES ('Item5','68')
- select*from ItemMaster
- -- 1)To Select Item Details
- -- Author : Shanu
- -- Create date : 2016-03-15
- -- Description :To Select Item Details
- -- Tables used : ItemMaster
- -- Modifier : Shanu
- -- Modify date : 2016-03-15
- -- =============================================
- -- To Select Item Details
- -- EXEC USP_Item_Select ''
- -- =============================================
- CREATEPROCEDURE [dbo].[USP_Item_Select]
- (
- @ItemName VARCHAR(100)=''
- )
- AS
- BEGIN
- SELECT ItemName,
- SaleCount
- FROM ItemMaster
- WHERE
- ItemName like @ItemName +'%'
- OrderBY ItemName
- END
- GO
- -- 2) To Insert/Update Item Details
- -- Author : Shanu
- -- Create date : 2016-03-15
- -- Description :To Insert/Update Item Details
- -- Tables used : ItemMaster
- -- Modifier : Shanu
- -- Modify date : 2016-03-15
- -- =============================================
- -- To Insert/Update Item Details
- -- EXEC USP_Item_Edit ''
- -- =============================================
- CREATEPROCEDURE [dbo].[USP_Item_Edit]
- (
- @ItemName VARCHAR(100)='',
- @SaleCount VARCHAR(10)=''
- )
- AS
- BEGIN
- IFNOTEXISTS(SELECT*FROM ItemMaster WHERE ItemName=@ItemName)
- BEGIN
- INSERTINTO ItemMaster([ItemName],[SaleCount])
- VALUES (@ItemName,@SaleCount)
- Select'Inserted'as results
- return;
- END
- ELSE
- BEGIN
- Update ItemMaster SET
- SaleCount=@SaleCount
- WHERE ItemName=@ItemName
- Select'Updated'as results
- return;
- END
- Select'Error'as results
- END
- -- 3)To Max and Min Value
- -- Author : Shanu
- -- Create date : 2016-03-15
- -- Description :To Max and Min Value
- -- Tables used : ItemMaster
- -- Modifier : Shanu
- -- Modify date : 2016-03-15
- -- =============================================
- -- To Max and Min Value
- -- EXEC USP_ItemMaxMin_Select ''
- -- =============================================
- CREATEPROCEDURE [dbo].[USP_ItemMaxMin_Select]
- (
- @ItemName VARCHAR(100)=''
- )
- AS
- BEGIN
- SELECTMIN(convert(int,SaleCount))as MinValue,
- MAX(convert(int,SaleCount))as MaxValue
- FROM ItemMaster
- WHERE
- ItemName like @ItemName +'%'
- END
- GO
After installing our Visual Studio 2015 click Start, then Programs and select Visual Studio 2015 - Click Visual Studio 2015. Click New, then Project, select Web and then select ASP.NET Web Application. Enter your project name and click OK.

Select MVC, WEB API and click OK.

Now we have created our MVC Application and as a next step we add our SQL server database as Entity Data Model to our application.
Add Database using ADO.NET Entity Data Model
Right click our project and click Add -> New Item.
Select Data->Select ADO.NET Entity Data Model> Give the name for our EF and click Add.

Select EF Designer from database and click next.

Here click New Connection and provide your SQL-Server Server Name and connect to your database.
Here we can see we have given our SQL server name, Id and PWD and after it connected we have selected the data base as ItemsDB as we have created the Database using my SQL Script.
Click next and select our tables that need to be used and click finish.

Here we can see we have selected our table ItemMasters and all needed Stored Procedures, after selecting click Finish.

Once Entity has been created, in the next step we add WEB API to our controller and write function to select/Insert/Update and Delete.
Steps to add our WEB API Controller
Right Click Controllers folder-> Click Add-> Click Controller.
As we are going to create our WEB API Controller. Select Controller and Add Empty WEB API 2 Controller. Give your Name to Web API controller and click ok. Here for my Web API Controller I have given name as “StudentsController”.

As we all know Web API is a simple and easy-to-build HTTP Service for Browsers and Mobiles.
Web API has four methods as Get/Post/Put and Delete where,
- Get is to request for the data. (Select)
- Post is to create a data. (Insert)
- Put is to update the data.
- Delete is to delete data.
In our example we will use both Get and Post as we need to get all image name and descriptions from database and to insert new Image Name and Image Description to database.
Get Method
In our example I have used only the Get method as I am using only Stored Procedure. We need to create object for our Entity and write our Get Method to perform Select/Insert/Update and Delete operations.
Select Operation
We use get Method to get all the details of itemMaster table using entity object and we return the result as IEnumerable .We use this method in our AngularJS and bind the result in ComboBox and insert the new chart Item to Database using the Insert Method.
- publicclassItemAPIController: ApiController
- {
- ItemsDBEntities objapi = newItemsDBEntities();
- // To get all Item chart detaiuls
- [HttpGet]
- publicIEnumerable < USP_Item_Select_Result > getItemDetails(string ItemName)
- {
- if (ItemName == null) ItemName = "";
- return objapi.USP_Item_Select(ItemName).AsEnumerable();
- }
- // To get maximum and Minimum value
- [HttpGet]
- publicIEnumerable < USP_ItemMaxMin_Select_Result > getItemMaxMinDetails(string ItemNM)
- {
- if (ItemNM == null) ItemNM = "";
- return objapi.USP_ItemMaxMin_Select(ItemNM).AsEnumerable();
- }
- // To Insert/Update Item Details
- [HttpGet]
- publicIEnumerable < string > insertItem(string itemName, string SaleCount)
- {
- return objapi.USP_Item_Edit(itemName, SaleCount).AsEnumerable();
- }
- }
Creating AngularJS Controller
First create a folder inside the Script Folder and we give the folder name as “MyAngular”.
First create a folder inside the Script Folder and I given the folder name as “MyAngular”
Now add your Angular Controller inside the folder.
Right Click the MyAngular Folder and click Add and New Item > Select Web > Select AngularJS Controller and give name to Controller.We have given my AngularJS Controller as “Controller.js”

If the Angular JS package is missing then add the package to your project.
Right Click your MVC project and Click-> Manage NuGet Packages. Search for AngularJS and click Install.
Modules.js: Here we will add the reference to the AngularJS JavaScript and create an Angular Module named “AngularJS_Module”.
- // <reference path="../angular.js" />
- /// <reference path="../angular.min.js" />
- /// <reference path="../angular-animate.js" />
- /// <reference path="../angular-animate.min.js" />
- var app;
- (function () {
- app = angular.module("AngularJS_Module", ['ngAnimate']);
- })();



Gowtham RajamanickamPosted Apr 7, 2016, 3:04 AM
good one..
Gowtham KPosted Apr 6, 2016, 1:25 PM
Great Atricle
Mohammed IbrahimPosted Apr 6, 2016, 12:14 PM
nice
Debasis SahaPosted Apr 6, 2016, 10:08 AM
Good One..
Mohsin AzamPosted Apr 6, 2016, 8:00 AM
Nice share
Rahul Kumar SaxenaPosted Apr 6, 2016, 7:10 AM
Awesome one.. Very Interesting..
Ankur MistryPosted Mar 25, 2016, 7:38 AM
Nice and helpful
Syed ShanuPosted Mar 24, 2016, 2:27 AM
Thank You ALL
Humayun Kabir MamunPosted Mar 24, 2016, 12:40 AM
Nice...
Jaipal ReddyPosted Mar 23, 2016, 11:52 PM
Thanks for share. .
Kumaresh RajalingamPosted Mar 23, 2016, 9:08 PM
Nice one
Syed ShanuPosted Mar 23, 2016, 6:25 PM
Thank you all
sreenivasa kPosted Mar 23, 2016, 3:06 PM
good
Debasis SahaPosted Mar 23, 2016, 1:19 PM
Nice one..
Vignesh ManiPosted Mar 23, 2016, 12:51 PM
Nice one