Slider and Label

Markup: {{slider}}{{label}}

Quick start

The example below, running in the Mappia calculator - click it to run it live. Full-size picture

  1. Run it

    Click the picture: the map opens right here and runs the example. Nothing is saved, and nothing to install.

  2. Try it
    • Drag the slider to 250 and let go: after a short countdown, only the forest storing more than 250 tC/ha stays green.
    • Drag it down to 50: much more forest turns green, and the legend changes to Above 50 tC/ha.
  3. Make it yours

    Copy the query, change it - the key parameters are below - and run it again in the playground, or paste it into the Mappia editor to save it as your map.

  4. More ways to use it

Key parameters

ParameterExampleWhat it does
idthresholdNames the value: read it as inputs.id['threshold'] in beforeCalc, and by position (inputs[0] here) inside expression.
value150Where the thumb starts. Without it the slider starts at 100.
minValue0Lowest value the slider can reach (default 0).
maxValue300Highest value the slider can reach (default 100).
increment25Step the thumb snaps to while the user drags it.
textMinimum carbon stock (tC/ha)Text of the label placed before the slider. It is shown as plain text; use html for bold or links.

Every parameter, with its type and default, is in the reference at the end of this page.

Complete example

The query 24 lines · runs as is
// Slider and Label: show only the forest carbon stock above the value chosen on the slider.
// beforeCalc runs on the page (inputs.id works there); expression runs in a worker
// (inputs is a plain array there: inputs[0] is the first input widget, the slider).
[
  {
    title: "Carbon stock above a threshold",
    name: "CSR:biomassa_inv_br_redd",
    source: "calculate",
    opacity: 0.85,
    visibility: true,
    paramsButtonConfig: [{ type: "query", pressed: true }],
    descriptionHtml:
      "{{label|text=Minimum carbon stock (tC/ha)}}" +
      "{{slider|id=threshold|minValue=0|maxValue=300|increment=25|value=150|width=250}}",
    beforeCalc: function (inputs) {
      this.setCalculateLegend([
        { color: [30, 132, 73], value: 1, title: "Above " + inputs.id["threshold"] + " tC/ha" },
      ]);
    },
    expression: function (layersVals, inputs) {
      return layersVals[0] >= inputs[0] ? 1 : this.nullValue;
    },
  },
];

More examples

Other ways to use Slider and Label, each a complete query that runs as is - like the one above.

Coloured track

The slider’s track is painted with colours by value, so the reader sees what each position means before moving it.

Click the picture to run it live. Full-size picture

The query 25 lines · runs as is
// Coloured track: the slider's track is painted with colours by value, so the reader sees what each position means before moving it.
// backgroundColors paints the track from each startValue on, and gradient=true blends one colour
// into the next. The layer keeps the land higher than the chosen elevation.
[
  {
    title: "Land above an elevation",
    name: "CSR:altimetria",
    source: "calculate",
    opacity: 0.85,
    visibility: true,
    paramsButtonConfig: [{ type: "query", pressed: true }],
    descriptionHtml:
      "{{label|text=Minimum elevation (m)}}" +
      "{{slider|id=minimum|minValue=0|maxValue=2000|increment=100|value=800|width=250|gradient=true" +
      '|backgroundColors=[{color: "#2e7d32", startValue: 0}, {color: "#f9a825", startValue: 700}, {color: "#8d4925", startValue: 1400}]}}',
    beforeCalc: function (inputs) {
      this.setCalculateLegend([
        { color: [141, 73, 37], value: 1, title: "Above " + inputs.id["minimum"] + " m" },
      ]);
    },
    expression: function (layersVals, inputs) {
      return layersVals[0] >= inputs[0] ? 1 : this.nullValue;
    },
  },
];

Two sliders, two maps

One slider per map, and the layer keeps the places that pass both - here land that is both high and steep, in the Cerrado.

Click the picture to run it live. Full-size picture

The query 30 lines · runs as is
// Two sliders, two maps: one slider per map, and the layer keeps the places that pass both - here land that is both high and steep, in the Cerrado.
// name lists both maps, so each pixel brings two values: layersVals[0] is the elevation and
// layersVals[1] the slope. The sliders are inputs[0] and inputs[1], in the order they appear.
[
  {
    title: "High and steep land",
    name: "CSR:altimetria,CSR:bioma_cerrado_declividade",
    source: "calculate",
    opacity: 0.9,
    visibility: true,
    paramsButtonConfig: [{ type: "query", pressed: true }],
    descriptionHtml:
      "{{label|text=Elevation above (m)}}" +
      "{{slider|id=min_elevation|minValue=0|maxValue=1600|increment=100|value=800|width=250}}" +
      "{{label|text=Slope above (degrees)}}" +
      "{{slider|id=min_slope|minValue=0|maxValue=40|increment=5|value=10|width=250}}",
    beforeCalc: function (inputs) {
      this.setCalculateLegend([
        {
          color: [192, 57, 43],
          value: 1,
          title: "Above " + inputs.id["min_elevation"] + " m, steeper than " + inputs.id["min_slope"] + " degrees",
        },
      ]);
    },
    expression: function (layersVals, inputs) {
      return layersVals[0] >= inputs[0] && layersVals[1] >= inputs[1] ? 1 : this.nullValue;
    },
  },
];

Customize it

Show the value while dragging

Add plugins=tip=<text> to show a tooltip above the thumb while the user drags it. {0} is replaced by the thumb’s value:

descriptionHtml:
  "{{label|text=Minimum carbon stock (tC/ha)}}" +
  "{{slider|id=threshold|minValue=0|maxValue=300|increment=25|value=150|plugins=tip={0} tC/ha|width=250}}",

To keep a label in step after each recalculation, give the label an id and set its text in beforeCalc: Ext.getCmp("threshold_label").setText("Above " + inputs.id["threshold"] + " tC/ha").

Colour the track

backgroundColors paints the filled part of the track by value, and gradient=true blends the colours into each other. Each colour starts at its startValue; the order of the entries does not matter. Here CSS colours are fine:

'{{slider|id=threshold|minValue=0|maxValue=300|value=150|backgroundColors=[{color: "khaki", startValue: 0}, {color: "darkgreen", startValue: 150}]|gradient=true|width=250}}'

More options

  • fieldLabel puts a label at the left of the slider; hideLabel=true removes it and the space kept for it.
  • disabled=true shows the slider greyed out. Enable it later with Ext.getCmp(id).enable().
  • cls and thumbStyle add CSS classes to the slider and to its thumb.
  • Any other Ext JS slider option, such as keyIncrement, is passed through unchanged.

Move it from code

Ext.getCmp(id).setValue(thumbIndex, value, false, true) moves the thumb and fires the same change as a drag, so the layer recalculates. A plain slider has one thumb, index 0. Ext.getCmp(id).getValue() reads the current value.

descriptionHtml:
  "{{slider|id=threshold|minValue=0|maxValue=300|increment=25|value=150}}" +
  "{{button|id=reset|text=Back to 150 tC/ha|handler=resetThreshold}}",
functions: {
  resetThreshold: function () {
    Ext.getCmp("threshold").setValue(0, 150, false, true);
  },
},

Recalculate without the countdown

After the user releases the thumb, the layer waits about 2.5 seconds, showing a countdown button, so several changes end in one recalculation. Set updateAutomatically: true on the layer to recalculate right away.

Pitfalls

  • Inside expression there is no inputs.id. Read inputs[0], inputs[1]… in the order the input widgets appear in descriptionHtml; labels and buttons take no position. The execution model explains why.
  • setCalculateLegend takes colours as [R, G, B] arrays. A CSS string such as "#c0392b" throws color.join is not a function. Only the slider’s own backgroundColors take CSS colours.
  • A variable defined outside expression is undefined inside it. Pass the value through an input instead.

Real maps that use it

Simple Slider Example

Simple Slider Example

Simple Slider Example

Economic valuation

Economic valuation

AMAZONES - Timber product

AMAZONES - Timber product

AMAZONES - Biodiversity

AMAZONES - Biodiversity

AMAZONES - Carbon stocks and CO2 emissions

AMAZONES - Carbon stocks and CO2 emissions

AMAZONES - Hydrologic services

AMAZONES - Hydrologic services

Reference: every parameter

Generated from the platform source. Every entry, searchable, is in the API reference; the raw data is api.json. Open an entry for its description, parameters and example; # links to it.

slider · number or range (input)

Slider17 entries

Written in a layer's descriptionHtml as {{slider|parameter=value|...}}

Tool that allows users to create a slider that the user can drag and change its value as map input.
View the complete Slider API here.

backgroundColors

Array.<String># Array of colors of background slider values, to define background slider color based in slider value.

Array of colors of background slider values, to define background slider color based in slider value. The values are defined from most to minimum with two properties each:

  • color: {String} CSS color definition for the current interval. i.e. 'red','black','#FF0000', '#000000'.
  • startValue: {Numeric} If defined define the initial value which above will apply this color, otherwise use theminimum slider value as default.

Ex.:
 backgroundColors: [
     {
         // Define background color to red when slider has value above 75.
         color: "red",
         startValue: 75
     },
     {
         // Define background color starting from value 0.
         // That results two intervals, from 0 to 75 as blue, and from 76 to 100 as red.
         color: "blue",
         startValue: 0
     }
 ]

cls

String# Extra CSS class(es) added to the slider element (appended to the default clickable).

Extra CSS class(es) added to the slider element (appended to the default clickable).

|cls=my_slider|

disabled

Boolean= false# Set true to render the slider disabled (Ext disabled); enable it later with Ext.getCmp(id).enable().

Set true to render the slider disabled (Ext disabled); enable it later with Ext.getCmp(id).enable().

|disabled=true|

fieldLabel

String# Defines the label shown at the left of the slider (Ext fieldLabel).

Defines the label shown at the left of the slider (Ext fieldLabel).

|fieldLabel=Deforestation (%)|

getValue

function() : Number# Returns the current value of a single-thumb slider.

Returns the current value of a single-thumb slider. Call it on the component (Ext.getCmp(id) or a getid= reference), not on inputs.id[ID], which already holds the plain value.

Returns The current slider value.

var v = Ext.getCmp('deforestation_slider').getValue();

getValues

function() : Array.<Number># Returns the value of every thumb; use it for range sliders created with values=[lo, hi].

Returns the value of every thumb; use it for range sliders created with values=[lo, hi].

Returns One value per thumb, in thumb order.

var range = Ext.getCmp('interval_slider').getValues(); // [lo, hi]

gradient

Boolean= false# Set true to blend the backgroundColors into a continuous gradient along the filled part of the slider (each colour fading into the next from its startValue).

Set true to blend the backgroundColors into a continuous gradient along the filled part of the slider (each colour fading into the next from its startValue). With the default false each interval is a solid colour, with a short blend only around the thumb. Has no effect without backgroundColors.

|backgroundColors=[{color: "green", startValue: 0}, {color: "red", startValue: 50}]|gradient=true|

hideLabel

Boolean= false# Set true to hide the label and the space reserved for it (Ext hideLabel).

Set true to hide the label and the space reserved for it (Ext hideLabel).

|hideLabel=true|

id

String# Defines the id to identify the object.

Defines the id to identify the object.

|id=example_slider|

increment

Number# Defines the step of each increment or decrement in the actual value of the slider when being dragged.

Defines the step of each increment or decrement in the actual value of the slider when being dragged.

|increment = 10|

maxValue

Number# Defines the maximum value of the slider.

Defines the maximum value of the slider.

|maxValue = 100|

minValue

Number# Defines the minimum value of the slider.

Defines the minimum value of the slider.

|minValue = 0|

setValue

function(value, animate)# Sets the slider value from code.

Sets the slider value from code. Firing the change event (the default) also updates inputs.id[ID] and recalculates the layer. For a range slider pass the thumb index first: setValue(index, value).

value Number
New value (clamped to minValue/maxValue).
animate Boolean
Set false to move the thumb without animation.
Ext.getCmp('deforestation_slider').setValue(20);

thumbStyle

String= null# Defines extra CSS class(es) added to the slider thumb (the draggable handle), to restyle it.

Defines extra CSS class(es) added to the slider thumb (the draggable handle), to restyle it.

|thumbStyle=x-slider-thumb-cut|

value

Number# Defines the initial value of the slider (default 100).

Defines the initial value of the slider (default 100).

At runtime the same value is what inputs.id[ID] (and inputs[i]) holds in beforeCalc/expression: a number, or an array [lower, upper] when the values range form is used. The layer recalculates on the slider change event (thumb released or value set from code).

|value = 100|
beforeCalc: function(inputs) {
    var threshold = inputs.id['deforestation_slider']; // number
}

values

Array.<Number># Defines the slider interval limits.

Defines the slider interval limits. If defined, the slider will be displayed as a range slider. It's return at the 'inputs' parameter will be a array of two values.

|values = [0, 250]|
|beforeCalc: function(inputs) {
   let lowerValue = inputs[0][0]; // The value of the left drag
   let upperValue = inputs[0][1]; // The value of the right drag
}|

width

Number# Defines the slider width in pixels.

Defines the slider width in pixels. Any other Ext.slider.SingleSlider config (cls, fieldLabel, hideLabel, disabled, style, keyIncrement...) is also passed through unchanged.

|width=200|

label · text

Label6 entries

Written in a layer's descriptionHtml as {{label|parameter=value|...}}

Create a simple label element to display some text.
Usage: {{label|}}
This label is created from Ext.form.Label.
Only some properties are listed here.

cls

String# Extra CSS class(es) added to the <label> element.

Extra CSS class(es) added to the <label> element.

{{label|cls=slider_label|text=0%}}

forId

String# Defines the id of the form field the label is for (rendered as the for attribute of the <label>), so clicking the label focuses that field.

Defines the id of the form field the label is for (rendered as the for attribute of the <label>), so clicking the label focuses that field.

{{label|text=Price (US$)|forId=soy_value}}{{textfield|id=soy_value|isnumeric}}

html

String# Defines the content of the label as raw HTML (not escaped).

Defines the content of the label as raw HTML (not escaped). Everything after html= up to the next | is the value, so it may contain = characters. text wins when both are given.

{{label|cls=slider_label|html=0% <b>...</b> 100%|id=scale_label}}

id

String# Defines the id of the label component (Ext.getCmp(id)), needed to change its text from code or to reference it with getid= in another tag.

Defines the id of the label component (Ext.getCmp(id)), needed to change its text from code or to reference it with getid= in another tag. Generated when omitted.

{{label|id=deforestation_label|text=20%}}

style

String# Inline CSS applied to the <label> element.

Inline CSS applied to the <label> element. Any other Ext.form.Label/Ext.Component config (hidden, width...) is passed through unchanged.

{{label|text=Total|style=font-weight: bold; color: #336699;}}

text

String# Defines the text of the label.

Defines the text of the label. It is HTML-escaped, so use html when markup is needed. Update it later with Ext.getCmp(id).setText(text) (typically from an on_change= listener of a slider or textfield through getid=).

{{label|id=deforestation_label|text=Deforestation: 20%}}