MultiBar Horizontal Chart Using AngularJS (nvd3.js)

Hi everyone. In this tutorial, I am going to explain how to implement "Multi Bar Horizontal Chart", using Angular JS-nvd3 (based on D3) directive.This directive is designed to make it easier to work with nvd3.js re-usable charting library. This directive allows you to easily customize your charts via JSON API.This chart library is feature rich with the amazing user interface.The official page for this library is available here:

Angular-nvd3

The key feature is that the original hierarchical structure of nvd3 models is completely preserved in directive JSON structure. This means that, while you create a complex chart that contains multiple elementary chart models (such as line, bar, axis, ...) you can in turn customize the properties of every internal elementary model as well as the global charting properties, the way you want. This can be done as usual, but it becomes quite easy to customize, while applying JSON approach.

In this tutorial, I am going to explain about basic multi bar horizontal charts with the static data. In the next tutorial, I will explain, how to feed the chart with the dynamic data from the Server in JSON format.

Let's see how I implement multi bar horizonthal chart in my ASP.NET MVC Application.

Multi bar horizontal chart in ASP.NET MVC 5 is as follows:

  1. Create a Web Aplication, using empty MVC template in Visual Studio 2013.
  2. Add a controller to Controllers folder --> right click on Controllers --> Add --> Controller --> select Empty Controller --> Add --> Name the controller(HomeController)--> OK.
  3. Create an Index action method --> Add View to an Index action.

    HomeController.cs
    1. public class HomeController : Controller  
    2.    {  
    3.        public ActionResult Index()  
    4.        {  
    5.            return View();  
    6.        }  
    7.     }  
  4. Replace the Index.cshtml code with the code, given below:
    1. @{  
    2.     ViewBag.Title = "www.mitechdev.com";  
    3. }  
    4.   
    5. <h2>multiBarHorizontalChart</h2>  
    6. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.1/nv.d3.min.css" />  
    7. <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>  
    8. <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>  
    9. <script src="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.1/nv.d3.min.js"></script>  
    10. <script src="https://rawgit.com/krispo/angular-nvd3/v1.0.4/dist/angular-nvd3.js"></script>  
    11. <script>  
    12.     var app = angular.module('myapp', ['nvd3']);  //must add the nvd3 as dependency  
    13.   
    14.     app.controller('MainCtrl', function ($scope) {  
    15.         //chart configuration options  
    16.         $scope.options = {  
    17.             chart: {  
    18.                 type: 'multiBarHorizontalChart',  
    19.                 height: 450,  
    20.                 x: function (d) { return d.label; },  
    21.                 y: function (d) { return d.value; },  
    22.                 showControls: true,  
    23.                 showValues: true,  
    24.                 duration: 500,  
    25.                 xAxis: {  
    26.                     showMaxMin: false  
    27.                 },  
    28.                 yAxis: {  
    29.                     axisLabel: 'Values',  
    30.                     tickFormat: function (d) {  
    31.                         return d3.format(',.2f')(d);  
    32.                     }  
    33.                 }  
    34.             }  
    35.         };  
    36.         //data source for chart  
    37.         $scope.data = [  
    38.             {  
    39.                 "key": "Series1",  
    40.                 "color": "#d62728",  
    41.                 "values": [  
    42.                     { "label": "Group A","value": -1.8746444827653},  
    43.                     {"label": "Group B","value": -8.0961543492239},  
    44.                     {"label": "Group C", "value": -0.57072943117674},  
    45.                     {"label": "Group D", "value": -2.4174010336624 },  
    46.                     {"label": "Group E","value": -0.72009071426284},  
    47.                     {"label": "Group F", "value": -0.77154485523777 },  
    48.                     {"label": "Group G","value": -0.90152097798131},  
    49.                     {"label": "Group H", "value": -0.91445417330854},  
    50.                     { "label": "Group I","value": -0.055746319141851}  
    51.                 ]  
    52.             },  
    53.             {  
    54.                 "key": "Series2",  
    55.                 "color": "#1f77b4",  
    56.                 "values": [  
    57.                     { "label": "Group A", "value": 25.307646510375 },  
    58.                     {"label": "Group B", "value": 16.756779544553},  
    59.                     {"label": "Group C", "value": 18.451534877007 },  
    60.                     {"label": "Group D","value": 8.6142352811805 },  
    61.                     {"label": "Group E","value": 7.8082472075876},  
    62.                     {"label": "Group F","value": 5.259101026956 },  
    63.                     { "label": "Group G", "value": 0.30947953487127 },  
    64.                     {"label": "Group H","value": 0},  
    65.                     { "label": "Group I","value": 0}  
    66.                 ]  
    67.             }  
    68.         ]  
    69.     });  
    70. </script>  
    71. <div ng-app="myapp" ng-controller="MainCtrl">  
    72.      
    73.     <nvd3 options="options" data="data"></nvd3>  
    74.   
    75. </div>  
  5. Now, run the Application and see the output, and how amazing it is:

    application

To see the live demo and source code, click here for the live demo.

Conclusion: I hope this article is helpful for many readers.

Up Next
    Ebook Download
    View all
    Learn
    View all