This sample demonstrates how to create a HistogramRangeSlider widget and use it to filter a layer's features based on a numeric attribute. The histogram can be generated with the histogram() statistics function.
view.whenLayerView(layer).then((layerView) => {
// temperature in Celsius
const field = "temp";
const min = 0;
const max = 30;
// generate 100-bin histogram
histogram({
layer: layer,
field: field,
numBins: 100,
minValue: min,
maxValue: max
}).then((histogramResponse) => {
// histogram response contains the histogram bins
// the code block shown below will be placed here
});
});
Then you can construct the slider using the output bins, and min/max values. You also must indicate a rangeType. This property serves two purposes:
- It determines how histogram bars are rendered, indicating which bins are included and excluded from the selected range.
- It determines the SQL where clause used for executing queries, highlighting, selecting, or filtering features.
In this case, the rangeType is between
so two thumbs will render on the slider track and all values between the two thumbs will be included in the selection/query/filter.
const slider = new HistogramRangeSlider({
bins: histogramResponse.bins,
min: min,
max: max,
values: [min, max],
excludedBarColor: "#524e4e",
rangeType: "between",
container: document.getElementById("slider-container")
});
slider.on(["thumb-change", "thumb-drag", "segment-drag"], (event) => {
layerView.filter = {
// generates the where clause for filtering features
where: slider.generateWhereClause(field)
};
});