Create a Gauge in Angular 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 Angular component which provides bindings for FusionCharts. The angular-fusioncharts component allows you to easily add rich and interactive charts to any Angular project.

In this page, we'll see how to install FusionCharts and render a chart using the angular-fusionCharts component.

Since, Angular2 and onward do not support the direct <script> tags, angular-fusioncharts can only be used via npm.

Installation

To install FusionCharts and the angular-fusioncharts component via npm follow the steps below:

Step 1: Install angular-fusioncharts

$ npm install angular-fusioncharts --save

Step 2: Install fusioncharts core library

$ npm install fusioncharts --save

That completes the installation of FusionCharts and the angular-fusioncharts component.

Create your First gauge

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

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

The angular gauge is shown below:

FusionCharts will load here..

The thresholds for the above sample have 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 are 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. In this example, we will use the JSON format, 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.

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 converted the tabular data to JSON format, let's learn how to render the gauge.

Render the Gauge

To render the gauge follow the steps below:

Step 1: In 'app.module.ts` include the necessary files and add the dependency.

  • Import angular-fusioncharts in the root @NgModule.

  • Import FusionCharts library in the root @NgModule.

  • Import the gauge type.

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

  • Pass the fusioncharts library, gauge module and the theme.

The consolidated code is shown below:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';

// Import angular-fusioncharts
import { FusionChartsModule } from 'angular-fusioncharts';

// Import FusionCharts library and chart modules
import * as FusionCharts from 'fusioncharts';
import * as Widgets from 'fusioncharts/fusioncharts.widgets';

import * as FusionTheme from 'fusioncharts/themes/fusioncharts.theme.fusion';

// Pass the fusioncharts library and chart modules
FusionChartsModule.fcRoot(FusionCharts, Widgets, FusionTheme);

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    FusionChartsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Step 2: Add data to the angular app.component.ts

Set the JSON data within the AppComponent class as shown below:

The code below is same for Angular older and latest versions.

import {Component} from '@angular/core';
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
export class AppComponent {
    dataSource: Object;
    constructor() {
        this.dataSource = {
            "chart": {
            "caption": "Nordstorm's Customer Satisfaction Score for 2017",
            "lowerLimit": "0",
            "upperLimit": "100",
            "showValue": "1",
            "numberSuffix": "%",
            "theme": "fusion",
            "showToolTip": "0"
        },
        // Gauge 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"
            }]
        }
    }; // end of this.dataSource
} // end of constructor
} // end of class AppComponent

Step 3: Now use fusioncharts component in app.component.html

Create the container to render the gauge and set the following:

  • Gauge width and height

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

  • The data source.

<h1>
  {{title}}
</h1>
<fusioncharts
    width="450" 
    height="250" 
    type="AngularGauge" 
    [dataSource]="dataSource">
</fusioncharts> 

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

Was this article helpful to you ?