Home

Awesome

ngx-gauge

<!-- [![Build Status](https://travis-ci.org/ashish-chopra/ngx-gauge.png?branch=master)](https://travis-ci.org/ashish-chopra/ngx-gauge) -->

npm npm contributions welcome NPM

A highly customizable Gauge component for Angular apps and dashboards. It provides many configurationable options to customize according to your needs. Checkout the live demo here. (For older versions of Angular, check out the compatibility matrix below).

alt text

In version v5.0.0, we introduced markers, ticks and background opacity for gauge as shown below. Checkout the documentation below. <img src="https://raw.githubusercontent.com/ashish-chopra/ashish-chopra.github.io/master/src/assets/demo2.png" alt="markers demo" width="300px" /> <img src="https://raw.githubusercontent.com/ashish-chopra/ashish-chopra.github.io/master/src/assets/demo1.png" alt="markers demo" width="300px" />

Getting Started

<a name="compatibility"></a>Angular Version Compatibility Table

Angular Versionngx-gauge Version
6.0-
7.0-
8.01.0.0-beta.12
9.01.1.0
10.x.x2.0.0
11.x.x3.0.0
12.x.x4.0.0
13.x.x5.0.0
14.x.x6.0.0
15.x.x7.0.0
16.x.x8.0.0
17.x.x9.0.0
18.x.x10.0.0

Step 1: Install npm module

For latest version use the command:

npm install ngx-gauge

For a specific version for an older Angular version as per Compatibility table above, use command:

npm install --save ngx-gauge@<version>

Step 2: Import the NgxGaugeModule

import { NgxGaugeModule } from 'ngx-gauge';

@NgModule({
    ...
    imports: [NgxGaugeModule],
    ...
})
export class AppModule { }

Be sure to import NgxGaugeModule after Angular's BrowserModule, as the import order matters for NgModules.

Step 3: Use gauge component in HTML

NgxGaugeModule provides a <ngx-gauge> component which can be used in any angular template to render a gauge. It's configuration properties can be bind to a typescript variable as shown below:

import { Component } from '@angular/core';

@Component({
    selector: 'app-component',
    templateUrl: 'app.html'
})
export class AppComponent {
    
    gaugeType = "semi";
    gaugeValue = 28.3;
    gaugeLabel = "Speed";
    gaugeAppendText = "km/hr";
}
<ngx-gauge [type]="gaugeType" 
           [value]="gaugeValue" 
           [label]="gaugeLabel"  
           [append]="gaugeAppendText">
</ngx-gauge>

Checkout the extensive list of configuration properites given below and try them to acheive your desired customization. In case you face any problem, then raise an issue here.

Configuration Properties

There are plenty of configurable properties available to tune the gauge as per your needs.

NameDescriptionRequiredDefault valuePossible values
sizeSpecifies the size of the canvas in which Gauge will be drawn. It is used as width and height both.No200Positive Integer
typeSpecifies the gauge's type.No"full""full", "semi", "arch"
minSpecifies the minimum numeric value for gauge's scale.No0Any numeric value
maxSpecified the maximum numeric value for gauge's scale.No100Any numeric value
valueSpecifies the current value of the Gauge in the range specified by min and max. It is a required attribute.YesundefinedAny numeric value
capThe style of line ending at the gauge's end.No"butt""round", "butt"
thickSpecified the thickness of the gauge's bar.No6Any Positive Integer
labelSpecifies the text to display below the Gauge's reading.NoundefinedAny String
foregroundColorSpecifies the foreground color of the Gauge's scale.Norgba(0, 150, 136, 1)Any color value string
backgroundColorSpecifies the background color of the Gauge's scale.Norgba(0, 0, 0, 0.1)Any color value string
appendSpecifies a string appended to the Gauge's reading. For example "%" most commonly used.NoundefinedAny string
prependSpecifies a string prepended to the Gauge's reading. For example "$" in case of financial data displayed in Gauge.NoundefinedAny String
durationSpecifies the duration (in milliseconds) of the Gauge's animationNo1500Positive Integer
thresholdsSpecifies an object of threshold values at which the gauge's color changes. Checkout an example here.Nonone{}
markersSpecifies an object of marker values at which value to place a marker. Can be a line or triangle and optionally may specify color, size and label. Note if you use labels you should add to margin value to the gauge using the margin optionNoA special object of the format shown. Currently type supports "line" or "triangle" marker. { "50": { color: "#555", type: "triangle", size: 8, label: "Goal", font: "12px arial" } , ... }{}
marginSpecifies an optional margin for the gauge.No0Positive Integer
animatetoggles the gauge animation.Notrueboolean
aria-labelSpecifies the label used by screen readersNoundefinedAny String
aria-labelledbySpecifies the ID of any external element to be used as label by screen readersNonullAny ID String

Configure Threshold Color Ranges

You can customize the colors of the gauge based on the current value being shown. In order to show different colors when gauge crosses certain value, use property thresholds. It takes an object with the threshold value as key and an object with color property as value. For example:

@Component({ ... })
export class AppComponent {
    ...

    thresholdConfig = {
        '0': {color: 'green'},
        '40': {color: 'orange'},
        '75.5': {color: 'red'}
    };

    ...
}
<ngx-gauge ...  [thresholds]="thresholdConfig"></ngx-gauge>

The keys in the threshold object signifies the minimum value at which the color will be applied. For instance, if the gauge's current value is 53.2, then orange color will be applied because after point 40 every value will be displayed as orange, until next threshold is encountered. In this example 75.5 is the next threshold.

Custom Directives for display text

Sometimes setting a property value on <ngx-gauge> does not solve your purpose. You may want to add custom HTML for displaying value,append, prepend and label texts. In order to provide custom templates for these properties display following directives can be used.

<ngx-gauge [value]="currentValue">
    <ngx-gauge-append>
        <!-- custom append text or HTML goes here -->
    </ngx-gauge-append>
    <ngx-gauge-label>
        <!-- custom label text or HTML goes here -->
    </ngx-gauge-label>
    <ngx-gauge-prepend>
        <!-- custom prepend text or HTML goes here -->
    </ngx-gauge-prepend>
    <ngx-gauge-value>
        {{ currentValue * Math.PI | number }}
    </ngx-gauge-value>
</ngx-gauge>

Note that value attribute is still required on <ngx-gauge> even when you are providing custom template using <ngx-gauge-value>. Because value attribute is responsible for calculating the gauge's drawing on a scale.

<!-- # Playground The examples section is redesigned as a playground where you can play with Gauge by tuning its different parameters. And, you can see the result live on-screen. It is good start to get familiar with Gauge. ![alt text](https://raw.githubusercontent.com/ashish-chopra/angular-gauge/master/examples/playground.png) -->

Contribution Welcome!

The project is continously evolving with every new release. Give it a star, if you like it. For contribution, setup the development environment as follows:

  1. clone and setup the project dependencies
$> git clone https://github.com/ashish-chopra/ngx-gauge.git
$> npm install
  1. Use following commands based on what you'd like to do:
$> npm start             # builds the project and watch for changes. 
$> npm test              # runs test suite once and exit.
$> npm run test:watch    # starts the test framework and watch for changes in code.
$> npm run build         # triggers a manual build for library, outputs at `/dist` directory.
  1. To add a new feature or fix a bug, make sure to create a new branch from master.

First thing first, explore the issue tracker to find something to contribute. There are tons of other project setup related issues and activities in which you can help. Your feedback could also be a great contribution.

If you face any problem, then raise an issue here.

License

MIT License