Create a Gauge in jQuery Using FusionCharts

Overview

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 jQuery plugin which provides bindings for FusionCharts. The jquery-fusioncharts plugin allows you to easily add rich and interactive charts to any jQuery project.

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

Installation

Install FusionCharts and the jquery-fusioncharts plugin using any of the following methods:

To install FusionCharts and the jquery-fusioncharts plugin via npm follow the steps below:
Install the jquery-fusioncharts plugin

$ npm install jquery-fusioncharts --save

2. Install the fusioncharts package

$ npm install fusioncharts --save

To install the FusionCharts package and the jquery-fusioncharts component follow the steps below:
1. Include the jQuery core library.
2. Include the jquery-fusioncharts module.
3. Include the FusionCharts JavaScript files.
4. Include the FusionCharts theme file to apply style to the charts.

<head>
    <!-- jQuery -->
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <!-- FusionCharts -->
    <script type="text/javascript" src="http://cdn.fusioncharts.com/fusioncharts/fusioncharts.js"></script>
    <!-- jQuery-FusionCharts -->
    <script type="text/javascript" src="https://rawgit.com/fusioncharts/fusioncharts-jquery-plugin/develop/dist/fusioncharts.jqueryplugin.min.js"></script>
    <!-- Fusion Theme -->
    <script type="text/javascript" src="http://cdn.fusioncharts.com/fusioncharts/themes/fusioncharts.theme.fusion.js"></script>
</head>

To install the FusionCharts package and the jquery-fusioncharts component follow the steps below:
1. Include the jQuery core library.
2. Include the jquery-fusioncharts module.
3. Include the FusionCharts JavaScript files which can be downloaded from here.
4. Include the FusionCharts theme file to apply style to the charts.

<head>
        <!-- jQuery -->
        <script type="text/javascript" src="path/to/local/jquery.min.js"></script>
        <!-- FusionCharts -->
        <script type="text/javascript" src="path/to/local/fusioncharts.js"></script>
        <!-- jQuery-FusionCharts -->
        <script type="text/javascript" src="path/to/local/jquery-fusioncharts.js"></script>
        <!-- Fusion Theme -->
        <script type="text/javascript" src="path/to/local/fusioncharts.theme.fusion.js"></script>
    </head>

That completes the installation of FusionCharts and the jquery-fusioncharts plugin.

Create your first gauge

Gauges are powerful tools that can showcase information using a radial scale to display data, and a dial to indicate the value. In this section, we will create an Angular Gauge.

To start with, we'll build a simple "Nordstorm Customer Satisfaction Index" gauge as shown below:

FusionCharts will load here..

The thresholds for his customer satisfaction score has been defined using the following range.

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 is green.

Convert tabular data into JSON format

Now that you have the tabular data ready, it's time to convert it into JSON format, as FusionCharts accepts data in JSON or XML format. The converted format will look as shown below:

{
    // Chart Configuration
    "chart": {
        "caption": "Nordstorm'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 array to set the color associated with the specific range of values.

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

  • Specify the hex code of the color within the color array.

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

  • Create the dial object under dials object to set the value of the dial in an array.

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

Render the Gauge

To render the gauge, follow the steps below:

  1. Include jQuery.

  2. Include jquery-fusioncharts plugin.

  3. Include fusioncharts core library.

  4. Include all gauges from Widgets.

  5. Include the FusionCharts theme file to apply the style to the gauge.

  6. Add the widgets and the theme as a dependency to the core.

  7. Store the chart configurations in a JSON object. In this JSON object:

    • Set the chart type as angulargauge. Each gauge type is represented with a unique gauge alias. For Angular gauge, the alias is angulargauge. Find the complete list of gauge types with their respective alias here.

    • Set the width and height (in pixels).

    • Set the dataFormat as JSON.

    • Embed the json data as the value of the dataSource.

  8. Add a container (instance) for the chart.

The consolidated code is given below:


var $ = require('jquery');
var FusionCharts = require('fusioncharts');
var jQueryFusionCharts = require('jquery-fusioncharts');
var Widgets = require('fusioncharts/fusioncharts.widgets');
var FusionTheme = require('fusioncharts/themes/fusioncharts.theme.fusion');
Widgets(FusionCharts);
FusionTheme(FusionCharts);
// Render the chart using insertFusionCharts method
$('document').ready(function () {

  $("#chart-container").insertFusionCharts({

    type: 'angulargauge', // Chart type
    renderAt: 'chart-container', // Container
    width: '450', // Width of the chart
    height: '250', // Height of the chart
    dataFormat: 'json', // Data Type
    dataSource: {
      // Chart Configuration
      "chart": {
        "caption": "Nordstorm's Customer Satisfaction Score for 2017",
        "lowerLimit": "0",
        "upperLimit": "100",
        "showValue": "1",
        "numberSuffix": "%",
        "theme": "fusion",
        "showToolTip": "0",
        "theme": "fusion"
      },
      // 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"
        }]
      }
    }
  })
});


<html>
    <head>
        <!-- Include jQuery -->
        <script type="text/javascript" src=" https://code.jquery.com/jquery-3.3.1.min.js"></script>
        <!-- Include fusioncharts core library file -->
        <script type="text/javascript" src=" http://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>
        <!-- Include fusion theme file -->
        <script type="text/javascript" src=" http://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script>
        <!-- Include fusioncharts jquery plugin -->
        <script type="text/javascript" src=" https://rawgit.com/fusioncharts/fusioncharts-jquery-plugin/develop/dist/fusioncharts.jqueryplugin.min.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            $('document').ready(function () {
                $("#chart-container").insertFusionCharts({
                    type: "angulargauge",
                    width: "450",
                    height: "250",
                    dataFormat: "json",
                    dataSource: {
                        // Chart Configuration
                        "chart": {
                            "caption": "Nordstorm'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>
        <div id="chart-container">FusionCharts will render here...</div>
    </body>
</html>


<html>
    <head>
        <!-- Include jQuery -->
        <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
        <!-- Include fusioncharts core library file -->
        <script type="text/javascript" src="path/to/local/fusioncharts.js"></script>
        <!-- Include fusion theme file -->
        <script type="text/javascript" src="path/to/local/themes/fusioncharts.theme.fusion.js"></script>
        <!-- Include fusioncharts jquery plugin -->
        <script type="text/javascript" src="https://rawgit.com/fusioncharts/fusioncharts-jquery-plugin/develop/dist/fusioncharts.jqueryplugin.min.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            $('document').ready(function () {
                $("#chart-container").insertFusionCharts({
                    type: "angulargauge",
                    width: "450",
                    height: "250",
                    dataFormat: "json",
                    dataSource: {
                        // Chart Configuration
                        "chart": {
                            "caption": "Nordstorm'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>
        <div id="chart-container">FusionCharts will render here</div>
    </body>
</html>

That's it! Your first gauge using jquery-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.