Skip to content

Gauge

A customizable analog gauge component that displays a value within a specified range using a needle and scale.

  • no dependencies required

See the Example by Serhii Pimenov on CodePen.

<div data-role="gauge" data-min="0" data-max="100" data-label="Value"></div>
<!-- Gauge with custom labels and theme -->
<div data-role="gauge"
data-label-min="Low"
data-label-max="High"
data-min="0"
data-max="100"
data-label="Energy"
data-suffix="%"
data-segments="10"
data-theme="battery"
data-values="11">
</div>
<!-- Gauge with custom angles and text values -->
<div data-role="gauge"
data-start-angle="270"
data-range="220"
data-min="0"
data-max="100"
data-label="Humidity"
data-suffix="%"
data-segments="100"
data-theme="sky"
data-values="Arid,Dry,Crisp,Low,Fair,Mild,Moist,Damp,Wet,Soggy">
</div>
ParameterTypeDefaultDescription
minnumber0Minimum value of the gauge
maxnumber100Maximum value of the gauge
valuenumber0Current value of the gauge
sizenumber0Size of the gauge in pixels (0 means auto)
labelMinstring""Label for the minimum value
labelMaxstring""Label for the maximum value
labelstring""Main label for the gauge
suffixstring""Suffix to append to the value (e.g., ”%”, ”°“)
valuesnumber/string10Number of value marks or comma-separated list of custom values
segmentsnumber10Number of segments in the gauge
startAnglenumber235Start angle of the gauge in degrees
rangenumber250Range of the gauge in degrees
themestring""Theme for the gauge (see available themes below)
  • load - Green to red gradient for load monitoring
  • sky - Blue gradient for atmospheric conditions
  • temp - Blue to red gradient for temperature readings
  • speed - Blue to green gradient for speed measurements
  • battery - Red to green gradient for battery levels
  • rainbow - Multi-color segments for a vibrant display
  • val(value) - Get the current value if no argument is provided, or set a new value
  • update() - Update the gauge display
  • destroy() - Remove the gauge element
const gauge = Metro.getPlugin('#myGauge', 'gauge');
gauge.val(75); // Set value to 75
const currentValue = gauge.val(); // Get current value
EventDescription
onGaugeCreateTriggered when the gauge is created
VariableDefault (Light)Dark ModeDescription
--analog-gauge-start-angle235deg-Start angle of the gauge
--analog-gauge-range250deg-Range of the gauge in degrees
--analog-gauge-needle-height10cqi-Height of the needle
--analog-gauge-needle-color#4b4b4b#e3e3e3Color of the needle
--analog-gauge-segment-color#ddd#222Color of the segments
--analog-gauge-marks-color#191919#cfcfcfColor of the value marks
--analog-gauge-value-color#191919#FFFColor of the value text
--analog-gauge-label-color#191919#FFFColor of the labels
--analog-gauge-values-bgtransparent-Background color of the value marks
--analog-gauge-bdw10cqi-Width of the gauge border
--analog-gauge-value-mark-w6ch-Width of the value marks
--analog-gauge-segments-w1deg-Width of the segments
--analog-gauge-segments10-Number of segments
#myCustomGauge {
--analog-gauge-needle-color: #ff5722;
--analog-gauge-segment-color: #e0e0e0;
--analog-gauge-marks-color: #2196f3;
--analog-gauge-value-color: #4caf50;
--analog-gauge-label-color: #9c27b0;
--analog-gauge-bdw: 15cqi;
}
  • .analog-gauge - Main container class
  • .gauge - The gauge background
  • .needle - The gauge needle
  • .value-marks - Container for value marks
  • .mark - Individual value mark
  • .value - The current value display
  • .label - The main label
  • .label-min - The minimum value label
  • .label-max - The maximum value label

The Gauge component comes with several predefined themes that can be applied using the data-theme attribute or the theme option:

  • load: A gradient from green to red, suitable for load indicators
  • sky: A blue gradient theme
  • temp: A blue to red gradient, suitable for temperature gauges
  • speed: A blue to green gradient that fills based on the current value
  • battery: A red to green gradient with thicker border, suitable for battery indicators
  • rainbow: A full-spectrum rainbow gradient with thick border
  1. Choose an appropriate theme based on the type of data you’re displaying
  2. Use clear and concise labels to indicate what the gauge is measuring
  3. Set appropriate min and max values to ensure the gauge accurately represents your data
  4. Consider using the suffix parameter to add units to your displayed value
  5. For better readability, limit the number of value marks to avoid overcrowding