2.0.1 • Published 2 years ago

gauge-chart-js v2.0.1

Weekly downloads
72
License
ISC
Repository
github
Last release
2 years ago

gauge-chart-js NPM version Build Status code style: prettier

A tiny (<2kb gzipped) library for rendering gauge charts. Supports conical/polar gradients, animation timing functions, custom labels/tooltips. No external dependencies required.

Examples

Conical / Polar gradientMultiple gaugesCountdown circle

These are GIFs, therefore FPS is low.

Installation

npm install gauge-chart-js

Basic usage

import { cubicBezier, Gauge } from 'gauge-chart-js';

const gauge = new Gauge({
  container: document.querySelector('.root'),
  color: '#0f0'
});

gauge.setValue(50);

Customise easing

import { cubicBezier, Gauge } from 'gauge-chart-js';

const easeIn = cubicBezier(0, 0, 0.2, 1);
const gauge = new Gauge({
  ...
  easing: easeIn
})

Easing functions cheat sheet: https://cubic-bezier.com/#.17,.67,.83,.67

Options

NameDescriptionRequiredDefault valueType
containerThe HTML element that act as a container for the gaugeYes-HTMLElement
fromAngleGauge start angle in degreesNo220number
toAngleGauge end angle in degreesNo500number
animationDurationAnimation duration in millisecondsNo600number
animationDelayAnimation delay in milliseconds. Pass 0 for no animation.No0number
lineWidthThickness of the gaugeNo3.5number
easingThe easing function that will be used when animatingNolinear(timeFraction: number) => number
gaugeRadiusGauge radiusNo35number
colorGauge color supported by SVG's fill attributeNo-string
colorsGauge colors supported by SVG's fill attributeNo-string[]

Programmatic API

Available methods:

  • setValue(value) Sets chart value.
  • getElementAtValue(value) Returns SVG element for given value with additional information: angle and relative position to its parent container.
  • insertAdjacentToRoot(where, html) Inserts HTML to SVG root. Can be used for custom labels.
  • dispose() Disposes chart.

How to integrate it with framework X?

The library is framework-agnostic and do not require any framework-specific integration. If you are using Angular make sure chart is rendered outside zone.js:

class ExampleComponent implements OnInit {
  constructor(private ngZone: NgZone) {}

  ngOnInit() {
    const gauge = new Gauge({ ... });
    this.ngZone.runOutsideAngular(() => {
      gauge.draw();
    });
  }
}

Run examples:

  • cd examples
  • npm run example conical-gradient/conical-gradient.html
  • npm run example countdown-gauge/countdown-gauge.html
  • npm run example multiple-gauges/multiple-gauges.html