0.5.1 • Published 15 days ago

react-gauge-chart v0.5.1

Weekly downloads
6,912
License
MIT
Repository
github
Last release
15 days ago

react-gauge-chart

React component for displaying a gauge chart, using D3.js

Usage

Install it by running npm install react-gauge-chart. Then to use it:

import GaugeChart from 'react-gauge-chart'

<GaugeChart id="gauge-chart1" />

Examples

Check the demo below for live examples of the charts

To create a default chart

<GaugeChart id="gauge-chart1" />

Chart with 20 levels and pointer at 86%

<GaugeChart id="gauge-chart2" 
  nrOfLevels={20} 
  percent={0.86} 
/>

Chart with custom colors and larger arc width

<GaugeChart id="gauge-chart3" 
  nrOfLevels={30} 
  colors={["#FF5F6D", "#FFC371"]} 
  arcWidth={0.3} 
  percent={0.37} 
/>

Chart with other corner radius and larger padding between arcs

<GaugeChart id="gauge-chart4" 
  nrOfLevels={10} 
  arcPadding={0.1} 
  cornerRadius={3} 
  percent={0.6} 
/>

Chart with custom arcs width

<GaugeChart id="gauge-chart5"
  nrOfLevels={420}
  arcsLength={[0.3, 0.5, 0.2]}
  colors={['#5BE12C', '#F5CD19', '#EA4228']}
  percent={0.37}
  arcPadding={0.02}
/>

Chart with disabled animation

<GaugeChart id="gauge-chart6" 
  animate={false} 
  nrOfLevels={15} 
  percent={0.56} 
  needleColor="#345243" 
/>

Demo

https://martin36.github.io/react-gauge-chart/

API

Warning: Do not use the same id for multiple charts, as it will put multiple charts in the same container

Note: If you do any updates to the props of the chart, it will rerender with a different size (it's a bug). To prevent this set a fixed height for the chart e.g.

const chartStyle = {
  height: 250,
}

<GaugeChart id="gauge-chart1" style={chartStyle} />

The props for the chart:

NamePropTypeDescriptionDefault value
idPropTypes.string.isRequiredUsed for the identification of the div surrounding the chart
classNamePropTypes.stringAdd className to the div container
stylePropTypes.objectAdd style to the div container{ width: '100%' }
marginInPercentPropTypes.numberMargin for the chart inside the containing SVG element0.05
cornerRadiusPropTypes.numberCorner radius for the elements in the chart6
nrOfLevelsPropTypes.numberThe number of elements displayed in the arc3
percentPropTypes.numberThe number where the pointer should point to (between 0 and 1)0.4
arcPaddingPropTypes.numberThe distance between the elements in the arc0.05
arcWidthPropTypes.numberThe thickness of the arc0.2
colorsPropTypes.arrayAn array of colors in HEX format displayed in the arc"#00FF00", "#FF0000"
textColorPropTypes.stringThe color of the text"#FFFFFF"
needleColorPropTypes.stringThe color of the needle triangle"#464A4F"
needleBaseColorPropTypes.stringThe color of the circle at the base of the needle"#464A4F"
hideTextPropTypes.boolWhether or not to hide the percentage displayfalse
arcsLengthPropTypes.arrayAn array specifying the length of each individual arc. If this prop is set, the nrOfLevels prop will have no effectnone
animatePropTypes.boolWhether or not to animate the needle when loadedtrue
animDelayPropTypes.numberDelay in ms before starting the needle animation500
animateDurationPropTypes.numberDuration in ms for the needle animation3000
formatTextValuePropTypes.funcFormat you own text value (example: value => value+'%')null
textComponentPropTypes.elementsCustom text label textComponentnull
textComponentContainerClassNamePropTypes.stringAdd className to the text component container
needleScalePropTypes.numberNeedle arc cornerRadius0.55
customNeedleComponentPropTypes.elementCustom needle component Note: Make sure to rotate the needle as per the percentage valuenull
customNeedleComponentClassNamePropTypes.stringAdd className to the custom needle container
customNeedleStylePropsTypes.objectAdd style to custom needle container div

|

Colors for the chart

The colors could either be specified as an array of hex color values, such as ["#FF0000", "#00FF00", "#0000FF"] where each arc would a color in the array (colors are assigned from left to right). If that is the case, then the length of the array must match the number of levels in the arc. If the number of colors does not match the number of levels, then the first and the last color from the colors array will be selected and the arcs will get colors that are interpolated between those. The interpolation is done using d3.interpolateHsl.