Meter

A meter is a graphical display of a numeric value that varies within a defined range. For example, a meter could be used to depict a device's current battery percentage or a car's fuel level

Installation

Installation

Import the component in the script section of your Svelte file:


import Meter from 'svve11/Meter.svelte'
            
Usage

Usage

Creating a Meter

To supply the meter with its contents, a value attribute and an options object are passed as props to the meter. These can be defined in the script section of the .svelte file or imported in from another location.

    The value attribute is required and defined as follows:

  • value (number): sets the current value of the meter. Must be within the minValue to maxValue range. It is recommended to use a reactive variable to allow meter value to change as necessary.
  • The options object has 12 properties:

    Required Props

  • maxValue (number): sets the maximum value for the meter range
  • minValue (number): sets the minimum value for the meter range.
  • meterLabel (string): sets the text label for the meter. The label will be automatically joined with a percentage calculation, unless otherwise specified. See displayDecimal in optional props section below.
  • id (number): sets the id for the meter. Remember to provide different id numbers when instantiating more than one meter on a page as the id should be unique.
  • Optional Props

  • highValue (number): sets the value from which a current value above is considered high. Must be less than maxValue and greater than the minValue and lowValue.
  • lowValue (number): sets the value from which a current value below is considered low. Must be greater than minValue and less than the maxValue and highValue.
  • optimumValue (number): sets the optimal numeric value of the meter. Must be a number between the minValue and maxValue. If the optimal value is set between the minValue and lowValue, or the maxValue and highValue, this range is considered optimal. Different browsers will color the bar differently depending on where the current value falls in relation to the optimal value.
  • valueText (string): used for assistive technologies that read the value of the meter to the users. Most assistive technologies will read value as a percentage by default, thus this props should be provided if a percentage read does not make sense in the context of your meter use.
  • displayDecimal (boolean): this will default to false. If set to true, this indicates to the meter that the value should not be presented as a percentage. This prop must be accompanied by the units prop described next.
  • units (string): sets the units to be displayed in the meter label should the percentage appearance not be relevant.
  • meterstyle (string): sets the style for the meter for any custom styles.
  • labelStyle (string): sets the style for the meter label for any custom styles.

Example Options Object:


const meterOptions = {
	maxValue: 100,
	minValue: 0,
	meterLabel: 'Demo meter',
	id: 1,
	lowValue: 20,
	highValue: 85,
	optimumValue: 80
};
					

Instantiating a Meter


<Meter value={reactiveValue} options={meterOptions}/>
              

Example meter with only required props:

Example meter with highValue=85, lowValue=20 and optimalValue=80 props:

Example meter with displayDecimal=false and units props:

Example meter with style strings:

Styling

Styling

The meter is made of 2 sub-components that can have styles applied to them using the pre-assigned classes and a globally scoped CSS stylesheet. The classes are:

  • sv-meter: This applies styling to the meter itself
  • sv-meter-label: This applies styling to the label for the meter
Component API

Component API

This table describes the props that should be passed to the meter in the options object
PropTypeRequiredDefault Value
value number true N/A
maxValue number true N/A
minValue number true N/A
meterLabel string true N/A
lowValue number false N/A
highValue number false N/A
optimumValue number false N/A
valueText string false N/A
displayDecimal boolean false false
units string false N/A
meterStyle string false N/A
labelStyle string false N/A