Create a Gauge in AngularJS Using FusionCharts

FusionCharts is a JavaScript charting library that enables you to create interactive charts, gauges, maps and dashboards in JavaScript. We have built a simple and lightweight Angularjs directive which provides bindings for FusionCharts. The angularjs-fusioncharts directive allows you to easily add rich and interactive charts to any Angularjs project.

In this page, we'll see how to install FusionCharts and render a gauge using the angularjs-fusionCharts directive.

Installation

Install FusionCharts and the angularjs-fusioncharts directive using any of the following methods:

To install fusioncharts and the angularjs-fusioncharts directive via npm follow the steps below:
1. Install angularjs core library

    $ npm install angular

2. Install angularjs-fusioncharts

    $ npm install angularjs-fusioncharts

2. Install the fusioncharts package

    $ npm install fusioncharts

To install the FusionCharts package and the angularjs-fusioncharts component follow the steps below:
  1. Include the AngularJS core library.
  2. Include the FusionCharts JavaScript files from CDN.
  3. Include the angularjs-fusioncharts directive.
  4. Include the theme file.
The consolidated code is shown below:

<head>
    <!-- Step 1 - Including AngularJS -->
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.2/angular.min.js"></script>
    <!-- Step 2 - Including the fusioncharts core library -->
    <script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>
    <!-- Step 3 - Including the angularjs-fusioncharts directive-->
    <script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/angularjs-fusioncharts.min.js"></script>
    <!-- Step 4 - Including the fusion theme -->
    <script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script>
</head>

To install the FusionCharts Suite and the angularjs-fusioncharts component follow the steps below:
  1. Include the AngularJS core library.
  2. Include the FusionCharts JavaScript files, which can be downloaded from here.
  3. Include the angularjs-fusioncharts directive.
  4. Include the FusionCharts theme file to apply style to the charts.
The consolidated code is shown below:

<head>
    <!-- Step 1 - Including AngularJS -->
    <script type="text/javascript" src="path/to/local/angular.min.js"></script>
    <!-- Step 2 - Including the fusioncharts core library -->
    <script type="text/javascript" src="path/to/local/fusioncharts.js"></script>
    <!-- Step 3 - Including the angularjs-fusioncharts directive-->
    <script type="text/javascript" src="path/to/local/angularjs-fusioncharts.min.js"></script>
    <!-- Step 4 - Including the fusion theme -->
    <script type="text/javascript" src="path/to/local/themes/fusioncharts.theme.fusion.js"></script>
</head>

That completes the installation of FusionCharts and the angularjs-fusioncharts directive.

Create your first gauge

Gauges are powerful tools that can showcase information using a radial or linear scale to display data.

To start with, we'll build a simple angular gauge showcasing Nordstrom's Customer Satisfaction Score as shown below.

FusionCharts Suite has 95+ chart types for you to explore. Find the complete list of chart types here.

The angular gauge is shown below:

FusionCharts will load here..

Chart data

The thresholds for the above sample is shown in the table below:

Range Color Hex Code
0-50 Red #F2726F
50-75 Yellow #FFC533
75-100 Green #62B58F

So, any score less than 50 is bad and is red. Any score between 50 and 75 is average and is yellow. Any score above 75 means good and are green.

FusionCharts accepts data in JSON format. Following code is the JSON representation of the above table with the required attributes to render the above chart.

{
  // Chart Configuration
  "chart": {
    "caption": "Nordstrom's Customer Satisfaction Score for 2017",
    "lowerLimit": "0",
    "upperLimit": "100",
    "showValue": "1",
    "numberSuffix": "%",
    "theme": "fusion",
    "showToolTip": "0"
  },
  // Chart Data
  "colorRange": {
    "color": [
      {
        "minValue": "0",
        "maxValue": "50",
        "code": "#F2726F"
      },
      {
        "minValue": "50",
        "maxValue": "75",
        "code": "#FFC533"
      },
      {
        "minValue": "75",
        "maxValue": "100",
        "code": "#62B58F"
      }
    ]
  },
  "dials": {
    "dial": [
      {
        "value": "81"
      }
    ]
  }
}

In the above JSON:

  • Create the chart object to define the elements of the gauge.

  • Create the colorRange object to set the color associated with the specific range of values.

  • Specify minValue and maxValue within the color array under the colorRange object.

  • Set the code attribute to specify the hex color of respective ranges.

  • Create the dials object to represent the customer satisfaction score.

  • Create the dial object under dials object to set the value of customer satisfaction score.

The chart object and the respective arrays contain a set of key-value pairs known as attributes. These attributes are used to set the functional and cosmetic properties of the gauge.

Now that you have the data in JSON format, let's see how to render the chart.

Render the gauge

To render the gauge, follow the steps below:

  1. Include angularjs.

  2. Include the fusioncharts library.

  3. Include angularjs-fusioncharts directive.

  4. Include ng-fusioncharts as a dependency in the application. Call angular.module() to add the dependency.

  5. Include all gauges from Widgets.

  6. Include the FusionCharts theme file to apply the style to the charts.

  7. Add the chart and theme as a dependency to the core.

  8. Store the chart configurations in a variable (myApp).

  9. Add the <div> with an fc-chart directive in your HTML, assuming that it is inside a controller named MyController. The the <div>:

  • Set the chart type as angulargauge. Each chart type is represented with a unique chart alias. For Angular Gauge chart, the alias is angulargauge. Find the complete list of gauge types with their respective alias here.
  • Set the width and height (in pixels).
  • Embed the json data as the value of the dataSource.

The consolidated code is shown below:


//  Require AngularJS 
var angular = require('angular');

// Require FusionCharts 
var FusionCharts = require('fusioncharts');

// Require angularjs-fusioncharts 
require('angularjs-fusioncharts');

// Require Chart modules
var Widgets = require('fusioncharts/fusioncharts.widgets');

// Require Fusion theme
var FusionTheme = require('fusioncharts/themes/fusioncharts.theme.fusion');

// Add widgets and themes as dependency
Widgets(FusionCharts);
FusionTheme(FusionCharts);

var myApp = angular.module('myApp', ['ng-fusioncharts']);

myApp.controller('MyController', ['$scope', function($scope) {
    $scope.dataSource = {
    // Chart Configuration
        "chart": {
            "caption": "Nordstrom's Customer Satisfaction Score for 2017",
            "lowerLimit": "0",
            "upperLimit": "100",
            "showValue": "1",
            "numberSuffix": "%",
            "theme": "fusion",
            "showToolTip": "0"
        },
        // Chart Data
        "colorRange": {
            "color": [{
                "minValue": "0",
                "maxValue": "50",
                "code": "#F2726F"
            }, {
                "minValue": "50",
                "maxValue": "75",
                "code": "#FFC533"
            }, {
                "minValue": "75",
                "maxValue": "100",
                "code": "#62B58F"
            }]
        },
        "dials": {
            "dial": [{
                "value": "81"
            }]
        }
    };
}]);
// Render
chartInstance.render()

Create an HTML template as shown below:

<body ng-app="myApp">
    <div ng-controller="MyController">
        <div
        fusioncharts
        width="450"
        height="250"
        type="angulargauge"
        datasource="{{myDataSource}}">
        </div>
    </div>
</body>


<html>
<head>
    <!-- Including AngularJS -->
    <script type="text/javascript" src=" https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.2/angular.min.js"></script>
    <!-- Including the fusioncharts core library -->
    <script type="text/javascript" src=" https://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>
    <!-- Including the angularjs-fusioncharts directive-->
    <script type="text/javascript" src=" https://unpkg.com/[email protected]/dist/angular-fusioncharts.js"></script>
    <!-- Including the fusion theme -->
    <script type="text/javascript" src=" https://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script>
    <script type="text/javascript">
        var myApp = angular.module('myApp', ['ng-fusioncharts']);
        myApp.controller('MyController', ['$scope', function($scope) {
            $scope.myDataSource = {
                // Chart Configuration
                "chart": {
                    "caption": "Nordstrom's Customer Satisfaction Score for 2017",
                    "lowerLimit": "0",
                    "upperLimit": "100",
                    "showValue": "1",
                    "numberSuffix": "%",
                    "theme": "fusion",
                    "showToolTip": "0"
                },
                // Chart Data
                "colorRange": {
                    "color": [{
                        "minValue": "0",
                        "maxValue": "50",
                        "code": "#F2726F"
                    }, {
                        "minValue": "50",
                        "maxValue": "75",
                        "code": "#FFC533"
                    }, {
                        "minValue": "75",
                        "maxValue": "100",
                        "code": "#62B58F"
                    }]
                },
                "dials": {
                    "dial": [{
                        "value": "81"
                    }]
                }
            };
        }]);
    </script>
</head>

<body ng-app="myApp">
<div ng-controller="MyController">
<div fusioncharts id="my-chart-id" width="450" height="250" type="angulargauge" dataSource="{{myDataSource}}">
</div>
</div>
</body>

</html>



<html>
<head>
    <!-- Including AngularJS -->
    <script type="text/javascript" src="path/to/local/angular.min.js"></script>
    <!-- Including the fusioncharts core library -->
    <script type="text/javascript" src="path/to/local/fusioncharts.js"></script>
    <!-- Including the angularjs-fusioncharts directive-->
    <script type="text/javascript" src="path/to/local/angular-fusioncharts.js"></script>
    <!-- Including the fusion theme -->
    <script type="text/javascript" src="path/to/local/themes/fusioncharts.theme.fusion.js"></script>
    <script type="text/javascript">
        var myApp = angular.module('myApp', ['ng-fusioncharts']);
        myApp.controller('MyController', ['$scope', function($scope) {
            $scope.myDataSource = {
                // Chart Configuration
                "chart": {
                    "caption": "Nordstrom's Customer Satisfaction Score for 2017",
                    "lowerLimit": "0",
                    "upperLimit": "100",
                    "showValue": "1",
                    "numberSuffix": "%",
                    "theme": "fusion",
                    "showToolTip": "0"
                },
                // Chart Data
                "colorRange": {
                    "color": [{
                        "minValue": "0",
                        "maxValue": "50",
                        "code": "#F2726F"
                    }, {
                        "minValue": "50",
                        "maxValue": "75",
                        "code": "#FFC533"
                    }, {
                        "minValue": "75",
                        "maxValue": "100",
                        "code": "#62B58F"
                    }]
                },
                "dials": {
                    "dial": [{
                        "value": "81"
                    }]
                }
            };
        }]);
    </script>
</head>

<body ng-app="myApp">
<div ng-controller="MyController">
<div fusioncharts id="my-chart-id" width="450" height="250" type="angulargauge" dataSource="{{myDataSource}}">
</div>
</div>
</body>

</html>


That's it! Your first chart using angularjs-fusioncharts is ready.

Problem rendering the chart?

In case there is an error, and you are unable to see the chart, check for the following:

  • If you are getting a JavaScript error on your page, check your browser console for the exact error and fix accordingly. If you're unable to solve it, click here to get in touch with our support team.

  • If the chart does not show up at all, but there are no JavaScript errors, check if the FusionCharts Suite XT JavaScript library has loaded correctly. You can use developer tools within your browser to see if fusioncharts.js was loaded.

  • If you get a Loading Data or Error in loading data message, check whether your JSON data structure is correct, or there are conflicts related to quotation marks in your code.