ColorSliderViewModel

AMD: require(["esri/widgets/smartMapping/ColorSlider/ColorSliderViewModel"], (ColorSliderVM) => { /* code goes here */ });
ESM: import ColorSliderViewModel from "@arcgis/core/widgets/smartMapping/ColorSlider/ColorSliderViewModel";
Class: esri/widgets/smartMapping/ColorSlider/ColorSliderViewModel
Since: ArcGIS API for JavaScript 4.12

Provides the logic for the ColorSlider widget.

See also:

Constructors

new ColorSliderViewModel(properties)
Parameter:
properties Object
optional

See the properties for a list of all the properties that may be passed into the constructor.

Property Overview

Any properties can be set, retrieved or listened to. See the Working with Properties topic.
Show inherited properties Hide inherited properties
Name Type Summary Class
Stringmore details

The name of the class.

more detailsAccessor
Booleanmore details

Only applicable when three thumbs (i.e.

more detailsSmartMappingPrimaryHandleSliderViewModel
LabelFormattermore details

A function used to format user inputs.

more detailsSliderViewModel
InputParsermore details

Function used to parse slider inputs formatted by the inputFormatFunction.

more detailsSliderViewModel
LabelFormattermore details

A function used to format labels.

more detailsSliderViewModel
String[]more details

An array of strings associated with values generated using an internal label formatter or the values returned from labelFormatFunction.

more detailsSmartMappingSliderViewModel
Numbermore details

Defines how slider values should be rounded.

more detailsSliderViewModel
Booleanmore details

When true, the slider will render a third handle between the two handles already provided by default.

more detailsSmartMappingPrimaryHandleSliderViewModel
Stringmore details

The state of the view model.

more detailsSmartMappingSliderViewModel
ColorStop[]more details

The color stops from the ColorVariable to link to the slider.

more detailsColorSliderViewModel
Booleanmore details

When false, the user can freely move any slider thumb to any position along the track.

more detailsSliderViewModel
Number[]more details

The data values associated with the thumb locations of the slider.

more detailsSmartMappingSliderViewModel
Booleanmore details

Enables zooming on the slider.

more detailsSmartMappingSliderViewModel
Objectmore details

Zooms the slider track to the bounds provided in this property.

more detailsSmartMappingSliderViewModel

Property Details

declaredClass Stringreadonly inherited

The name of the class. The declared class name is formatted as esri.folder.className.

handlesSyncedToPrimary Boolean inherited

Only applicable when three thumbs (i.e. handles) are set on the slider (i.e. when primaryHandleEnabled is true). This property indicates whether the position of the outside handles are synced with the middle, or primary, handle. When enabled, if the primary handle is moved then the outside handle positions are updated while maintaining a fixed distance from the primary handle.

Only applicable when primaryHandleEnabled is true.

Default Value:true
See also:
Example:
// enables the primary handles and syncs it with the others
slider.viewModel.primaryHandleEnabled = true;
slider.viewModel.handlesSyncedToPrimary = true;
inputFormatFunction LabelFormatter inherited

A function used to format user inputs. As opposed to labelFormatFunction, which formats thumb labels, the inputFormatFunction formats thumb values in the input element when the user begins to edit them.

The image below demonstrates how slider input values resemble corresponding slider values by default and won't match the formatting set in labelFormatFunction.

Slider without input formatter

If you want to format slider input values so they match thumb labels, you can pass the same function set in labelFormatFunction to inputFormatFunction for consistent formatting.

Slider with input formatter

However, if an inputFormatFunction is specified, you must also write a corresponding inputParseFunction to parse user inputs to understandable slider values. In most cases, if you specify an inputFormatFunction, you should set the labelFormatFunction to the same value for consistency between labels and inputs.

This property overrides the default input formatter, which formats by calling toString() on the input value.

See also:
Example:
// Formats the slider input to abbreviated numbers with units
// e.g. a thumb at position 1500 will render with an input label of 1.5k
slider.inputFormatFunction = function(value, type){
  if(value >= 1000000){
    return (value / 1000000).toPrecision(3) + "m"
  }
  if(value >= 100000){
    return (value / 1000).toPrecision(3) + "k"
  }
  if(value >= 1000){
    return (value / 1000).toPrecision(2) + "k"
  }
  return value.toFixed(0);
}
inputParseFunction InputParser inherited

Function used to parse slider inputs formatted by the inputFormatFunction. This property must be set if an inputFormatFunction is set. Otherwise the slider values will likely not update to their expected positions.

Overrides the default input parses, which is a parsed floating point number.

See also:
Example:
// Parses the slider input (a string value) to a number value understandable to the slider
// This assumes the slider was already configured with an inputFormatFunction
// For example, if the input is 1.5k this function will parse
// it to a value of 1500
slider.inputParseFunction = function(value, type, index){
  let charLength = value.length;
  let valuePrefix = parseFloat(value.substring(0,charLength-1));
  let finalChar = value.substring(charLength-1);

  if(parseFloat(finalChar) >= 0){
    return parseFloat(value);
  }
  if(finalChar === "k"){
    return valuePrefix * 1000;
  }
  if(finalChar === "m"){
    return valuePrefix * 1000000;
  }
  return value;
}
labelFormatFunction LabelFormatter inherited

A function used to format labels. Overrides the default label formatter.

By default labels are formatted in the following way:

  • When the data range is less than 10 ((max - min) < 10), labels are rounded based on the value set in the precision property.
  • When the data range is larger than 10, labels display with a precision of no more than two decimal places, though actual slider thumb values will maintain the precision specified in precision.

Use this property to override the behavior defined above.

Example:
// For thumb values, rounds each label to whole numbers.
// Note the actual value of the thumb continues to be stored
// based on the indicated data `precision` despite this formatting
sliderViewModel.labelFormatFunction = function(value, type) {
  return (type === "value") ? value.toFixed(0) : value;
}
labels String[]readonly inherited

An array of strings associated with values generated using an internal label formatter or the values returned from labelFormatFunction.

precision Number inherited

Defines how slider values should be rounded. This number indicates the number of decimal places slider values should round to when they have been moved.

This value also indicates the precision of thumb labels when the data range (max - min) is less than 10.

When the data range is larger than 10, labels display with a precision of no more than two decimal places, though actual slider thumb values will maintain the precision specified in this property.

For example, given the default precision of 4, and the following slider configuration, The label of the thumb will display two decimal places, but the precision of the actual thumb value will not be lost even when the user slides or moves the thumb.

const slider = new SliderVM({
  min: 20,
  max: 100,  // data range of 80
  values: [50.4331],
  // thumb label will display 50.43
  // thumb value will maintain precision, so
  // value will remain at 50.4331
});

If the user manually enters a value that has a higher precision than what's indicated by this property, the precision of that thumb value will be maintained until the thumb is moved by the user. At that point, the value will be rounded according to the indicated precision.

If thumb labels aren't visible, they must be enabled with labelInputsEnabled.

Keep in mind this property rounds thumb values and shouldn't be used exclusively for formatting purposes. To format thumb labels, use the labelFormatFunction property.

Default Value:4
Example:
sliderViewModel.precision = 7;
primaryHandleEnabled Boolean inherited

When true, the slider will render a third handle between the two handles already provided by default. This is the primary handle. Three or five stops must be defined for the primary handle to render. The primary handle represents the middle stop.

When handlesSyncedToPrimary is true, then this handle will control the position of the others when moved.

Default Value:false
See also:
Example:
// enables the primary handles and syncs it with the others
slider.viewModel.primaryHandleEnabled = true;
slider.viewModel.handlesSyncedToPrimary = true;
state Stringreadonly inherited

The state of the view model.

Possible Values:"ready"|"disabled"

stops ColorStop[]

The color stops from the ColorVariable to link to the slider. The colors in these stops will be used to render the color gradient on the slider's track. They should match the colors rendered in the associated layer's renderer.

Example:
colorRendererCreator.createContinuousRenderer({
  layer: featureLayer,
  field: "fieldName",
  basemap: "gray"
}).then(function(colorResponse){
  const slider = new ColorSlider({
    viewModel: new ColorSliderVM({
      stops: colorResponse.visualVariable.stops,
      min: colorResponse.statistics.min,
      max: colorResponse.statistics.max,
    }),
    container: "sliderDiv"
  });
});
thumbsConstrained Boolean inherited

When false, the user can freely move any slider thumb to any position along the track. By default, a thumb's position is constrained to the positions of neighboring thumbs so you cannot move one thumb past another. Set this property to false to disable this constraining behavior.

Default Value:true
Example:
// allows the user to freely move slider
// thumbs to any position along the track
slider.viewModel.thumbsConstrained = false;
values Number[]readonly inherited

The data values associated with the thumb locations of the slider. These are computed from the stops. Actual thumb locations depend on the number of stops provided.

See also:
zoomingEnabled Boolean inherited

Enables zooming on the slider. See zoomOptions for more information on zooming along the slider track.

See also:
Example:
// disables zooming on the slider
slider.viewModel.zoomingEnabled = false;
zoomOptions Object inherited

Zooms the slider track to the bounds provided in this property. When min and/or max zoom values are provided, the absolute min and max slider values are preserved and rendered at their typical positions on the slider. However, the slider track itself is zoomed so that thumbs cannot be moved above or below the provided min and max zoomed values.

When a slider is in a zoomed state, the zoomed ends of the track will appear jagged. In the image below, notice how the top thumb cannot be moved past the zoom max of 31 even though the slider max is 200.

slider-zoom

To exit a zoomed state, the user can click the jagged line or the developer can set the zoomOptions to null. It is up to the developer to provide a UI option for end users to enable zooming on the slider.

Setting the zoomOptions is useful when the slider is tied to heavily skewed datasets where the histogram renders only one or two bars because of outliers.

slider-not-zoomed

You can remove the influence of outliers by zooming the slider and regenerating a histogram based on the zoomed min and max. This will provide a better view of the data and make the slider more useful to the end user.

Properties:
min Number
optional

The lower bound of the zoom.

max Number
optional

The upper bound of the zoom.

Examples:
// zooms the slider to so thumbs can only be moved
// to positions between the values of 10 and 25 while
// maintaining the slider's absolute minimum and
// maximum values
slider.viewModel.zoomOptions = {
  min: 10,
  max: 25
};
// disables zooming on the slider
slider.viewModel.zoomOptions = null;
// zooms the slider to so thumbs can only be moved
// to positions above the value of 10, while maintaining
// the slider's absolute minimum value
slider.viewModel.zoomOptions = {
  min: 10
};
// zooms the slider to so thumbs can only be moved
// to positions below the value of 25, while maintaining
// the slider's absolute maximum value
slider.viewModel.zoomOptions = {
  max: 25
};
// zooms the slider to the handle positions
// with some padding
document.getElementById("zoomInButton").onclick = function() {
  const lowerThumb = slider.values[0];
  const upperThumb = slider.values[1];

  const range = upperThumb - lowerThumb;
  const padding = range * 0.3;

  const zoomMin = (lowerThumb - padding) > slider.min ? (lowerThumb - padding) : slider.min;
  const zoomMax = (upperThumb + padding) < slider.max ? (upperThumb + padding) : slider.max;

  slider.viewModel.set({ zoomOptions: { min: zoomMin, max: zoomMax } });
};

Method Overview

Show inherited methods Hide inherited methods
Name Return Type Summary Class
Stringmore details

The default input format function available for use as a fallback in custom formatting implementations.

more detailsSliderViewModel
Numbermore details

The default input parsing function available for use as a fallback in custom parsing implementations.

more detailsSliderViewModel
Stringmore details

The default label format function, available for use as a fallback in custom formatting implementations.

more detailsSliderViewModel
Booleanmore details

Emits an event on the instance.

more detailsSliderViewModel
Objectmore details

Returns the min and max bounds for a 'value' at the provided index.

more detailsSliderViewModel
Stringmore details

Returns the formatted label for a provided value.

more detailsSliderViewModel
StopInfo[]more details

Generates the color ramp gradient rendered on the slider track.

more detailsColorSliderViewModel
more details

Gets the max value of the slider.

more detailsSmartMappingSliderViewModel
more details

Gets the min value of the slider.

more detailsSmartMappingSliderViewModel
Booleanmore details

Indicates whether there is an event listener on the instance that matches the provided event name.

more detailsSliderViewModel
Objectmore details

Registers an event handler on the instance.

more detailsSliderViewModel
more details

Updates a thumb value based on the provided index.

more detailsSliderViewModel
Stringmore details

Rounds the given value to the number of decimal places specified in the precision property set on the view model.

more detailsSliderViewModel

Method Details

defaultInputFormatFunction(value){String}inherited

The default input format function available for use as a fallback in custom formatting implementations.

Parameter:
value Number

The input value to format.

Returns:
Type Description
String The formatted input value.
defaultInputParseFunction(value){Number}inherited

The default input parsing function available for use as a fallback in custom parsing implementations.

Parameter:
value String

The thumb value to parse.

Returns:
Type Description
Number The parsed thumb value.
defaultLabelFormatFunction(value){String}inherited

The default label format function, available for use as a fallback in custom formatting implementations.

Parameter:
value Number

The thumb value to format.

Returns:
Type Description
String The formatted thumb value.
emit(type, event){Boolean}inherited

Emits an event on the instance. This method should only be used when creating subclasses of this class.

Parameters:
type String

The name of the event.

event Object
optional

The event payload.

Returns:
Type Description
Boolean true if a listener was notified
getBoundsForValueAtIndex(index){Object}inherited

Returns the min and max bounds for a 'value' at the provided index. Also used internally to provide accessibility information via HTMLElement properties

Parameter:
index Number

The index of the associated value.

Returns:
Type Description
Object Returns a simple object with bound information.
getLabelForValue(value, type, index){String}inherited

Returns the formatted label for a provided value.

Parameters:
value Number

The value from which to retrieve a formatted label.

type String
optional

The label type.

Possible Values:"min"|"max"|"tick"|"values"

index Number
optional

The index of the label.

Returns:
Type Description
String Returns the formatted label.
getStopInfo(){StopInfo[]}

Generates the color ramp gradient rendered on the slider track.

Returns:
Type Description
StopInfo[]
getUnzoomedMax()inherited

Gets the max value of the slider. This is useful for when the user wants to change the slider max when it is not visible in the zoomed state.

See also:
getUnzoomedMin()inherited

Gets the min value of the slider. This is useful for when the user wants to change the slider min when it is not visible in the zoomed state.

See also:
hasEventListener(type){Boolean}inherited

Indicates whether there is an event listener on the instance that matches the provided event name.

Parameter:
type String

The name of the event.

Returns:
Type Description
Boolean Returns true if the class supports the input event.
on(type, listener){Object}inherited

Registers an event handler on the instance. Call this method to hook an event with a listener. See the Events summary table for a list of supported events.

Parameters:

An event or an array of events to listen for.

listener Function

The function to call when the event fires.

Returns:
Type Description
Object Returns an event handler with a remove() method that should be called to stop listening for the event(s).
Property Type Description
remove Function When called, removes the listener from the event.
Example:
view.on("click", function(event){
  // event is the event handle returned after the event fires.
  console.log(event.mapPoint);
});
setValue(index, value)inherited

Updates a thumb value based on the provided index. The provided value must differ from the previous value. The provided value must also be between the min and max.

Parameters:
index Number

The index of the thumb value in the associated values array.

value Number

The new value to replace with the old value.

toPrecision(value){String}inherited
Since: ArcGIS API for JavaScript 4.14

Rounds the given value to the number of decimal places specified in the precision property set on the view model.

Parameter:
value Number

The thumb value to format.

Returns:
Type Description
String The input value rounded to the number of places specified in precision.
See also:

Type Definitions

StopInfo

The return object of the getStopInfo() method.

Properties:
color Color

The color of the stop.

offset Number

The offset of the stop.

Event Overview

Show inherited events Hide inherited events
Name Type Summary Class
{oldValue: Number,type: "max",value: Number}
more details

Fires when a user changes the max value of the slider.

more details SliderViewModel
{oldValue: Number,type: "min",value: Number}
more details

Fires when a user changes the min value of the slider.

more details SliderViewModel

Event Details

max-changeinherited

Fires when a user changes the max value of the slider.

Properties:
oldValue Number

The former max value (or bound) of the slider.

type String

The type of the event.

The value is always "max".

value Number

The value of the max value (or bound) of the slider when the event is emitted.

min-changeinherited

Fires when a user changes the min value of the slider.

Properties:
oldValue Number

The former min value (or bound) of the slider.

type String

The type of the event.

The value is always "min".

value Number

The value of the min value (or bound) of the slider when the event is emitted.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.