1.1.1 • Published 4 months ago

react-circular-gauge v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

npm.io

React Circular Gauge

A React gauge component for visualizing numbers.

  • Rendered using SVG
  • Out of the box support for animation using react-spring
  • Expressive and consistent API with sensible defaults
  • Written in TypeScript

Demo

Installation

npm i react-circular-gauge

Usage

import { Gauge } from 'react-circular-gauge'
import chroma from 'chroma-js'

const Speedometer = () => (
  <Gauge
    value={25}
    minValue={0}
    maxValue={100}
    renderBottomLabel="km/h"
    arcColor={({ normValue }) => chroma.scale(['green', 'red'])(normValue).css()}
  />
)

Check out the demo source code for additional usage examples.

Reference

Gauge

Gauge accepts the following props, all of which are optional.

NameTypeDefaultDescription
valuenumber0Gauge value, clamped between minValue and maxValue
minValuenumber0Minimum possible value
maxValuenumber100Maximum possible value
startValuenumberminValueValue between minValue and maxValue where the arc should be drawn from. Arc will draw in reverse from this point if value < startValue
startAnglenumber0Angle corresponding to minValue, measured in degrees clockwise from the positive y axis
endAnglenumber0Angle corresponding to maxValue, measured in degrees clockwise from the positive y axis
direction'cw' \| 'ccw''cw'Direction of the arc from startAngle to endAngle, 'cw' for clockwise or 'ccw' for counterclockwise
renderValueRenderableString({ fmtValue }) => fmtValueA string or a function from RenderableStringArgs to a string for displaying the value
renderTopLabelRenderableStringundefinedA string or a function from RenderableStringArgs to a string for displaying a label above the value
renderBottomLabelRenderableStringundefinedA string or a function from RenderableStringArgs to a string for displaying a label below the value
renderContentRenderableNodeundefinedReactNode or a function from RenderableNodeArgs to ReactNode for displaying the gauge content, overrides the render props above if passed
roundDigitsnumber0Number of decimal places to round fmtValue to as provided by RenderableStringArgs and RenderableNodeArgs
arcWidthnumber0.1Arc width as a fraction of the gauge radius
trackWidthnumber0.1Track width as a fraction of the gauge radius
arcCornerRadiusnumber0.5Arc corner radius as a fraction of the arc width
trackCornerRadiusnumber0.5Track corner radius as a fraction of the track width
arcColorRenderableString'black'A CSS color value or a function from RenderableStringArgs to a CSS color value
trackColorstring'transparent'A CSS color value
valueStyleCSSPropertiesundefinedStyles applied to the value
topLabelStyleCSSPropertiesundefinedStyles applied to the top label
bottomLabelStyleCSSPropertiesundefinedStyles applied to the bottom label
animatedbooleantrueWhether to animate changes in value
springConfigPartial<AnimationConfig>undefinedSpring configuration passed to react-spring

RenderableString

RenderableString is defined as string | ((args: RenderableStringArgs) => string). The function form should be used for a string that animates based on the gauge value. RenderableStringArgs is an object with the following properties.

NameTypeDescription
valuenumberThe current value of the animation as interpolated between the previous and current gauge value
fmtValuestringvalue converted to a string and with rounding applied
normValuenumbervalue normalized to the range [0, 1]
rawValuenumberThe current value provided to the gauge, not animated

RenderableNode

RenderableNode is defined as ReactNode | ((args: RenderableNodeArgs) => ReactNode). The function form should be used for a ReactNode that animates based on the gauge value. RenderableNodeArgs is an object with the following properties.

NameTypeDescription
valueSpringValue<number>The current value of the animation as interpolated between the previous and current gauge value
fmtValueInterpolation<number, string>value converted to a string and with rounding applied
normValueInterpolation<number, number>value normalized to the range [0, 1]
rawValuenumberThe current value provided to the gauge, not animated