react-gauge-component

repository·main·Indexed 42 days ago

https://github.com/antoniolago/react-gauge-component

A React-based gauge chart component for data visualization featuring multiple visual styles (semicircle, radial, grafana), customizable arcs, and multi-pointer support. Version 1.1.30 includes advanced configuration for tick labels, visual effects like glows and shadows, and interactive input modes via onValueChange.

Tokens
10K
Snippets
35
Records
51
Agent score
51%

What's inside react-gauge-component

  1. Apply Visual Effects (Glow and Shadows)

    main

    The library provides several 'Effects' interfaces to add visual depth:

    • PointerEffects: Supports glow (with glowColor, glowBlur, glowSpread) and dropShadow (with dx, dy, blur, color, opacity).
    • LabelEffects: Supports glow and textShadow for enhanced readability.
    • TickEffects: Supports glow for tick lines.
    • SubArcEffects: Allows overriding inherited effects from the parent Arc using the inherit: false flag.
  2. Use colorArray for arc color interpolation

    main

    The colorArray prop allows you to define colors for the gauge arcs. You can pass an array of hex color values (e.g., ['#FF0000', '#00FF00']).

    Behavior:

    • Exact Match: If the array length matches the number of levels in the arc, each segment receives its corresponding color.
    • Interpolation: If the array length does not match the number of levels, colors are interpolated sequentially across all segments using d3.interpolateHsl. For example, a 3-color array applied to 9 segments will create a smooth transition from the first color to the last.
  3. Use GaugeComponent in Next.js

    main

    Because the component may rely on browser-specific APIs, you must use dynamic imports with ssr: false when using it in a Next.js environment to prevent Server-Side Rendering errors.

    import dynamic from "next/dynamic";
    
    const GaugeComponent = dynamic(
      () => import('react-gauge-component'),
      { ssr: false }
    );
    
    // Component with default values
    <GaugeComponent />
  4. Use GaugeComponent in a React application

    main

    Import the GaugeComponent and render it within your application. You can use either a default import or a named import depending on your environment setup.

    import GaugeComponent from 'react-gauge-component';
    //or
    import { GaugeComponent } from 'react-gauge-component';
    
    // Component with default values
    <GaugeComponent />
  5. Use the GaugeComponent React component

    main

    The GaugeComponent is the primary entry point for creating responsive gauge charts. It is built with D3 and supports multiple gauge types (semicircle, radial, grafana). It automatically handles resizing via ResizeObserver and supports customizable arcs, pointers, labels, and tooltips.

    To use it, import the component and provide it with a configuration object matching GaugeComponentProps.

    import GaugeComponent from 'react-gauge-component';
    
    const MyGauge = () => (
      <div style={{ width: '300px', height: '300px' }}>
        <GaugeComponent 
          value={50} 
          minValue={0} 
          maxValue={100} 
          type="semicircle" 
        />
      </div>
    );
  6. Use multi-pointer mode

    main

    To display multiple pointers simultaneously, use the pointers prop instead of the single value and pointer props. When pointers is provided, it takes precedence.

    Each object in the pointers array can define its own value, color, and label.

    <GaugeComponent
      pointers={[
        { value: 15, color: '#ff0000', label: 'Back Pressure' },
        { value: 25, color: '#00ff00', label: 'Turbo 1' },
        { value: 35, color: '#0000ff', label: 'Turbo 2' },
      ]}
    />

    To handle interactions in multi-pointer mode, use the onPointerChange callback, which provides the index of the pointer and its new value.

  7. Customize label content with renderContent

    main

    You can override the default rendering of labels (like the value label) using the renderContent function within the labels prop. This allows you to inject custom JSX, such as adding units or custom styling.

    Signature: renderContent: (value: number, color: string) => React.ReactNode

    <GaugeComponent
      value={42}
      labels={{
        valueLabel: {
          renderContent: (value, color) => (
            <div style={{ textAlign: 'center', color }}>
              <span style={{ fontSize: '2rem' }}>{value}</span>
              <span style={{ fontSize: '0.8rem' }}>km/h</span>
            </div>
          )
        }
      }}
    />
  8. Configure SubArc segments

    main

    Use the subArcs array within the arc prop to define specific segments of the gauge. Each SubArc object allows you to control color, length, and limits.

    Properties:

    • limit (number): The upper limit of the subArc based on gauge value units.
    • color (string | number): The color of the segment.
    • length (number): The ratio of the arc (0-1, e.g., 0.5 is 50% of the arc).
    • showTick (boolean): Whether to display a tick for this segment.
    • tooltip (Tooltip): Configuration for a tooltip appearing on hover:
      • style (React.CSSProperties): CSS styles for the tooltip.
      • text (string): The text content.
    • onClick, onMouseMove, onMouseLeave (() => void): Event handlers for mouse interactions.
    • effects (SubArcEffects): Visual effects specific to this subArc (inherits from parent arc.effects if not provided).
    <GaugeComponent
      value={75}
      arc={{
        subArcs: [
          { limit: 30, color: '#EA4228' },
          { limit: 70, color: '#F5CD19' },
          { color: '#5BE12C' }
        ]
      }}
      pointer={{ type: "needle", elastic: true }}
    />
  9. Configure the single pointer

    main

    The pointer prop configures the visual and behavioral aspects of the pointer in single-pointer mode.

    Pointer Appearance

    • type ("needle" | "blob" | "arrow"): The shape of the pointer.
    • color (string): The color of the pointer.
    • length (number): Length as a ratio of the radius (0-1).
    • width (number): Width multiplier (default: 20).
    • arrowOffset (number): Radial position for arrow type (0-1).
    • blobOffset (number): Radial position for blob type (0-1).

    Animation and Performance

    • animate (boolean): Enables transitions between values.
    • elastic (boolean): Provides an elastic transition effect.
    • animationDuration (number): Duration in milliseconds (default: 3000).
    • maxFps (number): Maximum frames per second (default: 60). Use 30 for balanced or 15 for low-power.
    • animationThreshold (number): Minimum progress change (ratio) required to trigger a DOM update. Range: 0.0001 to 0.01.
  10. Apply visual effects to Arcs, Pointers, Labels, and Ticks

    main

    The component supports various visual effects including glows and shadows to enhance the UI. These are categorized by the element they affect:

    ArcEffects

    Applied to the gauge arcs.

    • glow (boolean): Enable glow.
    • glowColor (string): Color of the glow (defaults to arc color).
    • glowBlur (number): Blur radius in pixels (default: 10).
    • glowSpread (number): Spread in pixels (default: 3).
    • filterUrl (string): Custom SVG filter ID.
    • dropShadow (DropShadowConfig): Shadow settings (dx, dy, blur, color, opacity).
    • innerShadow (boolean): Enables an inset/3D look.

    PointerEffects

    Applied to the pointer.

    • glow (boolean): Enable glow.
    • glowColor (string): Color of the glow (defaults to pointer color).
    • glowBlur (number): Blur radius in pixels (default: 8).
    • glowSpread (number): Spread in pixels (default: 2).
    • dropShadow (object): Shadow settings (dx, dy, blur, color, opacity).

    LabelEffects

    Applied to the text labels.

    • glow (boolean): Enable glow.
    • glowColor (string): Color of the glow (defaults to label color).
    • glowBlur (number): Blur radius in pixels (default: 6).
    • glowSpread (number): Spread in pixels (default: 2).
    • textShadow (string): CSS text shadow.

    TickEffects

    Applied to the tick lines.

    • glow (boolean): Enable glow.
    • glowColor (string): Color of the glow (defaults to tick color).
    • glowBlur (number): Blur radius in pixels (default: 4).
    • glowSpread (number): Spread in pixels (default: 2).