.NET Highcharts With ASP.NET MVC

This article will demonstrate how to implement .NET Highcharts with ASP.NET MVC application.

Highcharts is a JavaScript library to implement charting functionality like a line chart, bar chart, column chart etc. We can create different types of charts using Highcharts. Today, with this article, I will try to show you how to create Highcharts in ASP.NET MVC from the server side. Here, server-side means that everything will be created on the server and only the displaying part will happen at the client side.

ASP.NET

Just follow the below steps to complete this demonstration.

STEP 1 Create a project in ASP.NET MVC

Open Visual Studio 2015 and click on "New Project". From the "New Project" window, choose web from the Installed Templates and select ASP.NET Web Application. Next, provide a suitable name like “HighchartsWithMVC”, choose location, and click OK.

Next, the Windows will ask to select project template, so we just need to select MVC and change authentication as “No Authentication” and click the OK button. It will take a few seconds to create the application.

Now, our ASP.NET MVC application is ready.

STEP 2 Install .NET Highcharts

To install DotNet.Highcharts in your application, just right click your project from Solution Explorer and choose "Manage NuGet Packages". It will open the NuGet Package Manager from where we can search the required packages. Search for “Dotnet.Highcharts” from the search textbox as following and install it with the latest version.

ASP.NET

We can also install it from Package Manager console. Go to "Tools: menu and select NuGet Package Manager and choose “Package Manager Console”.

ASP.NET

To install this package, just type the following command.

Install-Package DotNet.Highcharts

ASP.NET

It will fetch all the dependencies and install it.

If you open the Solution Explorer, you will find the Highcharts script in Scripts folder. You can also find the reference of DotNet.Highcharts in the References section.

ASP.NET

STEP 3 Create Highcharts

Now, it is time to create a chart using Highcharts. Therefore, for this demonstration, I will create a column chart that shows the comparison of runs made in respective years by Sachin and Dhoni. This example is only for explaining.

First, I'm going to create a chart to initialize with basic configuration, such as the type of chart, background color, border etc., and provide a suitable name for it.

  1. Highcharts columnChart = new Highcharts("columnchart");  
  2.   
  3.         columnChart.InitChart(new Chart()  
  4.         {  
  5.             Type = DotNet.Highcharts.Enums.ChartTypes.Column,  
  6.             BackgroundColor = new BackColorOrGradient(System.Drawing.Color.AliceBlue),  
  7.             Style = "fontWeight: 'bold', fontSize: '17px'",  
  8.             BorderColor = System.Drawing.Color.LightBlue,  
  9.             BorderRadius = 0,  
  10.             BorderWidth = 2  
  11.   
  12.         });  

To set title and sub title, we have to define text for chart.

  1. columnChart.SetTitle(new Title()  
  2.        {  
  3.            Text = "Sachin Vs Dhoni"  
  4.        });  
  5.   
  6.        columnChart.SetSubtitle(new Subtitle()  
  7.        {  
  8.            Text = "Played 9 Years Together From 2004 To 2012"  
  9.        });  

We can also define the axis of the chart like what should render on xAxis and yAxis. Using SetXAxis and SetYAxis method, we can define the category of the axis, type, and title as well.

  1. columnChart.SetXAxis(new XAxis()  
  2.         {  
  3.             Type = AxisTypes.Category,  
  4.             Title = new XAxisTitle() { Text = "Years", Style = "fontWeight: 'bold', fontSize: '17px'" },  
  5.             Categories = new[] { "2004""2005""2006""2007""2008""2009""2010""2011""2012" }  
  6.         });  
  7.   
  8.         columnChart.SetYAxis(new YAxis()  
  9.         {  
  10.             Title = new YAxisTitle()  
  11.             {  
  12.                 Text = "Runs",  
  13.                 Style = "fontWeight: 'bold', fontSize: '17px'"  
  14.             },  
  15.             ShowFirstLabel = true,  
  16.             ShowLastLabel = true,  
  17.             Min = 0  
  18.         });  

To set the data with series, we can use SetSeries() method where we need to provide a name for series and data which will bind with series.

  1. columnChart.SetSeries(new Series[]  
  2.  {  
  3.      new Series{  
  4.   
  5.          Name = "Sachin Tendulkar",  
  6.          Data = new Data(new object[] { 812, 412, 628, 1425, 460, 972, 204, 513, 315 })  
  7.      },  
  8.      new Series()  
  9.      {  
  10.          Name = "M S Dhoni",  
  11.          Data = new Data(new object[] { 19, 895, 821, 1103, 1097, 1198, 600, 764, 524, })  
  12.      }  
  13.  }  

The whole code for Index action for Home controller is as following.

  1. using DotNet.Highcharts;  
  2. using DotNet.Highcharts.Enums;  
  3. using DotNet.Highcharts.Helpers;  
  4. using DotNet.Highcharts.Options;  
  5. using System;  
  6. using System.Collections.Generic;  
  7. using System.Drawing;  
  8. using System.Linq;  
  9. using System.Web;  
  10. using System.Web.Mvc;  
  11.   
  12. namespace HighchartsWithMVC.Controllers  
  13. {  
  14.     public class HomeController : Controller  
  15.     {  
  16.         public ActionResult Index()  
  17.         {  
  18.             Highcharts columnChart = new Highcharts("columnchart");  
  19.   
  20.             columnChart.InitChart(new Chart()  
  21.             {  
  22.                 Type = DotNet.Highcharts.Enums.ChartTypes.Column,  
  23.                 BackgroundColor = new BackColorOrGradient(System.Drawing.Color.AliceBlue),  
  24.                 Style = "fontWeight: 'bold', fontSize: '17px'",  
  25.                 BorderColor = System.Drawing.Color.LightBlue,  
  26.                 BorderRadius = 0,  
  27.                 BorderWidth = 2  
  28.   
  29.             });  
  30.   
  31.             columnChart.SetTitle(new Title()  
  32.             {  
  33.                 Text = "Sachin Vs Dhoni"  
  34.             });  
  35.   
  36.             columnChart.SetSubtitle(new Subtitle()  
  37.             {  
  38.                 Text = "Played 9 Years Together From 2004 To 2012"  
  39.             });  
  40.   
  41.             columnChart.SetXAxis(new XAxis()  
  42.             {  
  43.                 Type = AxisTypes.Category,  
  44.                 Title = new XAxisTitle() { Text = "Years", Style = "fontWeight: 'bold', fontSize: '17px'" },  
  45.                 Categories = new[] { "2004""2005""2006""2007""2008""2009""2010""2011""2012" }  
  46.             });  
  47.   
  48.             columnChart.SetYAxis(new YAxis()  
  49.             {  
  50.                 Title = new YAxisTitle()  
  51.                 {  
  52.                     Text = "Runs",  
  53.                     Style = "fontWeight: 'bold', fontSize: '17px'"  
  54.                 },  
  55.                 ShowFirstLabel = true,  
  56.                 ShowLastLabel = true,  
  57.                 Min = 0  
  58.             });  
  59.   
  60.             columnChart.SetLegend(new Legend  
  61.             {  
  62.                 Enabled = true,  
  63.                 BorderColor = System.Drawing.Color.CornflowerBlue,  
  64.                 BorderRadius = 6,  
  65.                 BackgroundColor = new BackColorOrGradient(ColorTranslator.FromHtml("#FFADD8E6"))  
  66.             });  
  67.   
  68.             columnChart.SetSeries(new Series[]  
  69.             {  
  70.                 new Series{  
  71.   
  72.                     Name = "Sachin Tendulkar",  
  73.                     Data = new Data(new object[] { 812, 412, 628, 1425, 460, 972, 204, 513, 315 })  
  74.                 },  
  75.                 new Series()  
  76.                 {  
  77.                     Name = "M S Dhoni",  
  78.                     Data = new Data(new object[] { 19, 895, 821, 1103, 1097, 1198, 600, 764, 524, })  
  79.                 }  
  80.             }  
  81.             );  
  82.   
  83.             return View(columnChart);  
  84.         }  
  85. }  

STEP 4 Rendering Highcharts on UI

Just move to Index.cshtml of home controller and add the following code that will render this column chart on UI.

  1. @model DotNet.Highcharts.Highcharts  
  2.   
  3. @{  
  4.     ViewBag.Title = "Highcharts Examples";  
  5. }  
  6.   
  7. <div class="jumbotron">  
  8.     <h3>DotNet Highcharts Example in Asp.Net MVC</h3>  
  9.     <p><a href="http://dotnet.highcharts.com/" class="btn btn-primary btn-lg">Learn more »</a></p>  
  10. </div>  
  11.   
  12. <div class="row">  
  13.     <div>  
  14.   
  15.         <div class="col-md-12 col-md-6">  
  16.             @(Model)  
  17.         </div>  
  18.     </div>  
  19. </div>  

To render Highchart on client side, it requires Highcharts.js. So, just add the Highchart.js on _Layout.cshtml file as following.

Note - Please make sure that the jQuery is initialized before body tag otherwise it will throw an error.

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <meta charset="utf-8" />  
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">  
  6.     <title>@ViewBag.Title - My ASP.NET Application</title>  
  7.     @Styles.Render("~/Content/css")  
  8.     @Scripts.Render("~/bundles/modernizr")  
  9.     @Scripts.Render("~/bundles/jquery")  
  10. </head>  
  11. <body>  
  12.     <div class="navbar navbar-inverse navbar-fixed-top">  
  13.         <div class="container">  
  14.             <div class="navbar-header">  
  15.                 <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">  
  16.                     <span class="icon-bar"></span>  
  17.                     <span class="icon-bar"></span>  
  18.                     <span class="icon-bar"></span>  
  19.                 </button>  
  20.                 @Html.ActionLink("Application name""Index""Home"new { area = "" }, new { @class = "navbar-brand" })  
  21.             </div>  
  22.             <div class="navbar-collapse collapse">  
  23.                 <ul class="nav navbar-nav">  
  24.                     <li>@Html.ActionLink("Home""Index""Home")</li>  
  25.                     <li>@Html.ActionLink("About""About""Home")</li>  
  26.                     <li>@Html.ActionLink("Contact""Contact""Home")</li>  
  27.                 </ul>  
  28.             </div>  
  29.         </div>  
  30.     </div>  
  31.     <div class="container body-content">  
  32.         @RenderBody()  
  33.         <hr />  
  34.         <footer>  
  35.             <p>© @DateTime.Now.Year - My ASP.NET Application</p>  
  36.         </footer>  
  37.     </div>  
  38.   
  39.       
  40.     @Scripts.Render("~/bundles/bootstrap")  
  41.     <script src="~/Scripts/Highcharts-4.0.1/js/highcharts.js"></script>  
  42.     @RenderSection("scripts", required: false)  
  43. </body>  
  44. </html>  

Once everything is completed, run the application. It will render a column comparison chart as following.

ASP.NET

Conclusion

Today, we have seen server side implementation of dotnet highcharts with asp.net mvc application.

I hope this post helps you. Please put your feedback using comments, which will help me improve for the next post. If you have any doubts, please ask in the comments section.


Similar Articles