This sample uses the LegendModel to create a custom, interactive legend that allows users to select legend symbols to filter the layer’s features.

How it works

Create the model and wait for legend content

After the map view is ready, create a LegendModel to access each layer’s legend elements and layer view through ActiveLayerInfo.

const legendModel = new LegendModel({
view: viewElement.view,
});

Legend elements are generated asynchronously, so the sample watches the active layer infos and renders each layer when its legend is ready.

// Re-render legend when layer readiness changes; renderLegend adds each ready layer once.
reactiveUtils.watch(
() => legendModel.activeLayerInfos.map((activeLayerInfo) => activeLayerInfo.ready),
renderLegend,
{ initial: true },
);

The sample creates a Calcite List for each layer, using the model’s labels and symbol previews.

Filter features by selection

The sample matches each category using the renderer’s field and the legend item’s stored value. It assumes each layer uses a single-field UniqueValueRenderer; other renderer configurations need different filtering logic. Selected categories are combined with OR and applied through the layer view’s filter.

function updateFilter(activeLayerInfo, selectedClauses) {
const layerId = activeLayerInfo.layer.uid;
const layerView = activeLayerInfo.layerView;
if (!selectedClauses.size) {
// With no categories selected, show all features in this layer.
layerView.filter = null;
layerSelections.delete(layerId);
return;
}
// A feature is included when it matches any selected category.
const where = Array.from(selectedClauses, (clause) => `(${clause})`).join(" OR ");
layerView.filter = new FeatureFilter({ where });
layerSelections.set(layerId, selectedClauses);
}
2D5.2WebMapitem-idLegendModelActiveLayerInfolegendElementsFeatureLayerViewFeatureFilterfilterUniqueValueRenderercategorieslegend itemsselection-modeselection-appearanceviewOnReadyarcgis-maparcgis-zoommap-componentscalcite-blockcalcite-buttoncalcite-listcalcite-list-itemcalcite-noticecalcite-panelcalcite-shellcalcite-shell-panelcalcite-components