Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-liquid-gauge

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-liquid-gauge

React Liquid Gauge component

  • 1.2.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.1K
decreased by-3.43%
Maintainers
2
Weekly downloads
 
Created
Source

react-liquid-gauge build status Coverage Status

NPM

React Liquid Gauge component. It's heavily inspired by D3 Liquid Fill Gauge and react-liquidchart.

react-liquid-gauge

Demo: http://trendmicro-frontend.github.io/react-liquid-gauge

The sample code can be found in the examples directory.

Installation

npm install --save react react-dom react-liquid-gauge

Usage

import { color } from 'd3-color';
import { interpolateRgb } from 'd3-interpolate';
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import LiquidFillGauge from 'react-liquid-gauge';

class App extends Component {
    state = {
        value: 50
    };
    startColor = '#6495ed'; // cornflowerblue
    endColor = '#dc143c'; // crimson

    render() {
        const radius = 200;
        const interpolate = interpolateRgb(this.startColor, this.endColor);
        const fillColor = interpolate(this.state.value / 100);
        const gradientStops = [
            {
                key: '0%',
                stopColor: color(fillColor).darker(0.5).toString(),
                stopOpacity: 1,
                offset: '0%'
            },
            {
                key: '50%',
                stopColor: fillColor,
                stopOpacity: 0.75,
                offset: '50%'
            },
            {
                key: '100%',
                stopColor: color(fillColor).brighter(0.5).toString(),
                stopOpacity: 0.5,
                offset: '100%'
            }
        ];

        return (
            <div>
                <LiquidFillGauge
                    style={{ margin: '0 auto' }}
                    width={radius * 2}
                    height={radius * 2}
                    value={this.state.value}
                    percent="%"
                    textSize={1}
                    textOffsetX={0}
                    textOffsetY={0}
                    textRenderer={(props) => {
                        const value = Math.round(props.value);
                        const radius = Math.min(props.height / 2, props.width / 2);
                        const textPixels = (props.textSize * radius / 2);
                        const valueStyle = {
                            fontSize: textPixels
                        };
                        const percentStyle = {
                            fontSize: textPixels * 0.6
                        };

                        return (
                            <tspan>
                                <tspan className="value" style={valueStyle}>{value}</tspan>
                                <tspan style={percentStyle}>{props.percent}</tspan>
                            </tspan>
                        );
                    }}
                    riseAnimation
                    waveAnimation
                    waveFrequency={2}
                    waveAmplitude={1}
                    gradient
                    gradientStops={gradientStops}
                    circleStyle={{
                        fill: fillColor
                    }}
                    waveStyle={{
                        fill: fillColor
                    }}
                    textStyle={{
                        fill: color('#444').toString(),
                        fontFamily: 'Arial'
                    }}
                    waveTextStyle={{
                        fill: color('#fff').toString(),
                        fontFamily: 'Arial'
                    }}
                    onClick={() => {
                        this.setState({ value: Math.random() * 100 });
                    }}
                />
                <div
                    style={{
                        margin: '20px auto',
                        width: 120
                    }}
                >
                    <button
                        type="button"
                        className="btn btn-default btn-block"
                        onClick={() => {
                            this.setState({ value: Math.random() * 100 });
                        }}
                    >
                        Refresh
                    </button>
                </div>
            </div>
        );
    }
}

ReactDOM.render(
    <App />,
    document.getElementById('container')
);

API

Properties

NameTypeDefaultDescription
idStringA unique identifier (ID) to identify the element. Defaults to a unique random string.
widthNumber400The width of the component.
heightNumber400The height of the component.
valueNumber0The percent value (0-100).
percentString|Node'%'The percent string (%) or SVG text element.
textSizeNumber1The relative height of the text to display in the wave circle. A value of 1 equals 50% of the radius of the outer circle.
textOffsetXNumber0
textOffsetYNumber0
textRendererFunction(props)Specifies a custom text renderer for rendering a percent value.
riseAnimationBooleanfalseControls if the wave should rise from 0 to it's full height, or start at it's full height.
riseAnimationTimeNumber2000The amount of time in milliseconds for the wave to rise from 0 to it's final height.
riseAnimationEasingString'cubicInOut'd3-ease options. See the easing explorer for a visual demostration.
riseAnimationOnProgressFunction({ value, container })Progress callback function.
riseAnimationOnCompleteFunction({ value, container })Complete callback function.
waveAnimationBooleanfalseControls if the wave scrolls or is static.
waveAnimationTimeNumber2000The amount of time in milliseconds for a full wave to enter the wave circle.
waveAnimationEasingString'linear'd3-ease options. See the easing explorer for a visual demostration.
waveAmplitudeNumber1The wave height as a percentage of the radius of the wave circle.
waveFrequencyNumber2The number of full waves per width of the wave circle.
gradientBooleanfalseWhether to apply linear gradients to fill the wave circle.
gradientStopsNode|ArrayAn array of the <stop> SVG element defines the ramp of colors to use on a gradient, which is a child element to either the <linearGradient> or the <radialGradient> element.
onClickFunction(event)onClick event handler.
innerRadiusNumber0.9The radius of the inner circle. A value of 0.9 equals 90% of the radius of the outer circle.
outerRadiusNumber1.0The radius of the outer circle. A value of 1 equals 100% of the radius of the outer circle.
marginNumber0.025The size of the gap between the outer circle and wave circle as a percentage of the radius of the outer circle. A value of 0.025 equals 2.5% of the radius of the outer circle.
circleStyleObject
{
  fill: 'rgb(23, 139, 202)'
}
The style of the outer circle.
waveStyleObject
{
  fill: 'rgb(23, 139, 202)'
}
The style of the fill wave.
textStyleObject
{
  fill: 'rgb(0, 0, 0)'
}
The style of the text when the wave does not overlap it.
waveTextStyleObject
{
  fill: 'rgb(255, 255, 255)'
}
The style of the text when the wave overlaps it.

License

MIT

Keywords

FAQs

Package last updated on 28 Nov 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc