Working With Telerik Charts In ASP.NET MVC

In my previous articles, I explained the following things in MVC:
  1. Creating And Calling A Web API From A .NET Client In ASP.NET Web API 2
  2. Creating Cascading DropDownList In MVC Using Entity Framework And ADO.NET 
  3. Displaying Image In WebGrid And Changing Background Color Of Webgrid Row On Mouse Hover
  4. Using DHTMLX Scheduler in MVC
  5. ASP.NET MVC Drag and Drop Multiple Image Upload Using DropZone
  6. Working With Telerik Grid In MVC
  7. How to Upload Image and Save Image in Project Folder in MVC
In this tutorial, I will explain how to work with the different Telerik Charts.

For this, I have created an MVC project. Now, I will add the Telerik Extension for MVC from NuGet Package.



Now, it will add the following CSS and DLL to the Project.



Here is the DLL.



Now, create the following Controller in the Project.
  1. public ActionResult chat()  
  2. {  
  3.     List<Sales> li = new List<Sales>();  
  4.     li.Add(new Sales { Year = 2004, production = 8000 });  
  5.     li.Add(new Sales { Year = 2005, production = 6000 });  
  6.     li.Add(new Sales { Year = 2006, production = 10000 });  
  7.     li.Add(new Sales { Year = 2008, production = 8000 });  
  8.     li.Add(new Sales { Year = 2010, production = 12000 });  
  9.     return View(li);  
  10. }  
Here is my Sale Model.
  1. using System.Collections.Generic;  
  2. using System.Linq;  
  3. using System.Web;  
  4.   
  5. namespace Tellerik_grid.Models  
  6. {  
  7.     public class Sales  
  8.     {  
  9.         public int Year { getset; }  
  10.         public int production { getset; }  
  11.     }  
  12. }  
Now, I will generate a Line Chart in the View, using:
  1. @model IEnumerable<Tellerik_grid.Models.Sales>  
  2.            @using Telerik.Web.Mvc.UI;  
  3. @using System.Collections;  
  4.   
  5. @{  
  6.     ViewBag.Title = "chat";  
  7. }  
  8.   
  9. <link href="~/fonts/ss1.css" rel="stylesheet" />  
  10. <h2>chart</h2>  
  11.   
  12. @{  
  13.     ArrayList alyear1 = new ArrayList();  
  14.     ArrayList alprod1 = new ArrayList();  
  15.   
  16.   
  17.     foreach (var x in Model)  
  18.     {  
  19.   
  20.         alyear1.Add(Convert.ToInt32(x.Year));  
  21.         alprod1.Add(Convert.ToInt32(x.production));  
  22.     }  
  23.   
  24.     Html.Telerik().Chart()  
  25.         .Name("mychart")  
  26.         .Title("Production Chart")  
  27.         .Legend(legend => legend  
  28.             .Position(ChartLegendPosition.Bottom))  
  29.              .Series(series =>  
  30.              {  
  31.                  series.Line(alyear1).Name("Years");  
  32.                  series.Line(alprod1).Name("Total production");  
  33.              })  
  34.              .ChartArea(area => area  
  35.         .Background("#00FFFF")  
  36.           
  37.     )  
  38.                
  39.         .CategoryAxis(axis => axis  
  40.             .Categories(alyear1)  
  41.        )  
  42.         .Render();  
  43.   
  44.         @(Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("telerik.common.css").Add("telerik.windows7.css").Add("telerik.hay.min.css").Combined(true).Compress(true)))  
  45.         @(Html.Telerik().ScriptRegistrar())  
  46.   
  47. }  
Now, it will form the graph, shown below:


Now, the code is given below to generate Bar Graph:
  1. @model IEnumerable<Tellerik_grid.Models.Sales>  
  2.  @using Telerik.Web.Mvc.UI;  
  3. @using System.Collections;  
  4.   
  5. @{  
  6.     ViewBag.Title = "chat";  
  7. }  
  8.   
  9. <link href="~/fonts/ss1.css" rel="stylesheet" />  
  10. <h2>chart</h2>  
  11. @{  
  12.         ArrayList alyear = new ArrayList();  
  13.         ArrayList alprod = new ArrayList();  
  14.   
  15.   
  16.         foreach(var x in Model)  
  17.         {  
  18.   
  19.             alyear.Add(Convert.ToInt32(x.Year));  
  20.             alprod.Add(Convert.ToInt32(x.production));  
  21.         }  
  22.   
  23.         Html.Telerik().Chart()  
  24.             .Name("mychart")  
  25.             .Title("Production Chart")  
  26.          
  27.   
  28.              .Tooltip(true)  
  29.             .Legend(legend => legend  
  30.                 .Position(ChartLegendPosition.Bottom))  
  31.                  .Series(series =>  
  32.                  {  
  33.                      series.Bar(alyear).Name("Years");  
  34.                      series.Bar(alprod).Name("Total production");  
  35.                  })  
  36.             .CategoryAxis(axis => axis  
  37.                 .Categories(alyear)  
  38.            )  
  39.             .Render();  
  40.   
  41.              @(Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("telerik.common.css").Add("telerik.windows7.css").Add("telerik.hay.min.css").Combined(true).Compress(true)))  
  42.         @(Html.Telerik().ScriptRegistrar())  
  43.   
  44.     }  

Similarly, if you want to do an Area Chart, change the series to area for the following graph.
  1. @{  
  2.     ArrayList alyear1 = new ArrayList();  
  3.     ArrayList alprod1 = new ArrayList();  
  4.   
  5.   
  6.     foreach (var x in Model)  
  7.     {  
  8.   
  9.         alyear1.Add(Convert.ToInt32(x.Year));  
  10.         alprod1.Add(Convert.ToInt32(x.production));  
  11.     }  
  12.   
  13.     Html.Telerik().Chart()  
  14.         .Name("mychart")  
  15.         .Title("Production Chart")  
  16.         .Legend(legend => legend  
  17.             .Position(ChartLegendPosition.Bottom))  
  18.              .Series(series =>  
  19.              {  
  20.                  series.Area(alyear1).Name("Years");  
  21.                  series.Area(alprod1).Name("Total production");  
  22.              })  
  23.              .Tooltip(true)  
  24.                
  25.              
  26.       
  27.   
  28.         .CategoryAxis(axis => axis  
  29.             .Categories(alyear1)  
  30.        )  
  31.         .Render();  
  32.   
  33.         @(Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("telerik.common.css").Add("telerik.windows7.css").Add("telerik.hay.min.css").Combined(true).Compress(true)))  
  34.         @(Html.Telerik().ScriptRegistrar())  
  35.   
  36. }  

Thus, in this way, we can work with various Types of Telerik chat controls. I hope you liked this tutorial.


Similar Articles