This sample demonstrates how to filter temporal data in a GeoJSONLayer using the Time Bar component. When the Time Bar’s timeExtent changes, the sample updates the layer’s feature effect and queries statistics for the selected time window.
When the GeoJSONLayer is initialized, the timeInfo property for the layer is specified as shown below:
const layer = new GeoJSONLayer({ ... // specify timeInfo for the layer timeInfo: { startField: "time", // name of the date field interval: { unit: "days", value: 1 } } ...});The Time Bar component is not associated with the arcgis-map component, so changing its timeExtent does not automatically update the map’s timeExtent. This lets you apply custom filtering logic. The pattern is also useful for layers with a date field that are not time-enabled, since custom filtering can use that field without relying on the map’s automatic time filtering. In this sample, the GeoJSONLayer has timeInfo configured, but filtering is still handled explicitly when the Time Bar’s timeExtent changes.
<!-- Initialize the Time Bar with ticks and a locked time window --> <arcgis-time-bar show-play show-next show-previous show-time-extent id="time-bar" play-rate="50"> <arcgis-time-bar-slider slot="time-extent" show-min show-max show-ticks show-lock-time-window lock-time-window popover-display="always" > <arcgis-ticks slot="content-end" mode="value" show-labels> </arcgis-ticks> <div slot="popover" class="time-label"></div> </arcgis-time-bar-slider> </arcgis-time-bar>Lastly, the application listens to the arcgisPropertyChange event on the Time Bar component. When the time extent changes, it updates the layer’s definition expression to exclude earthquakes after the time extent’s end, applies a feature effect to distinguish earthquakes within the selected time window, and queries statistics for that window.
// Hide earthquakes after the selected time window and dim those before it // Show statistics for earthquakes within both the window and the current map extent // Update the slider popover content whenever the time extent changes timeBar.addEventListener("arcgisPropertyChange", async (event) => { try { if (event.detail.name === "timeExtent") { updatePopover(); }
// Exclude earthquakes after the window's end from the layer const date = new Date(timeBar.timeExtent.end).toISOString().replace("T", " ").replace("Z", ""); layer.definitionExpression = `time <= Timestamp '${date}'`;
// Dim earthquakes outside the selected time window or current map extent layerView.featureEffect = { filter: { timeExtent: timeBar.timeExtent, geometry: viewElement.extent, }, excludedEffect: "grayscale(20%) opacity(12%)", };
// Count earthquakes and summarize magnitude and depth within the time window and map extent const statQuery = layerView.featureEffect.filter.createQuery(); statQuery.outStatistics = [magMax, magAvg, magMin, tremorCount, avgDepth];
const result = await layer.queryFeatures(statQuery); statsDiv.textContent = "";
// Display the returned count and summary statistics in the panel if (result.features.length >= 1) { const attributes = result.features[0].attributes;
// Format the magnitude and depth statistics to two decimal places const statisticsHtml = Object.keys(statsFields) .map((name) => { if (attributes[name] != null) { return `<br/>${statsFields[name]}: <span>${attributes[name].toFixed(2)}</span>`; } return ""; }) .join("");
// Pair the earthquake count with the selected start and end dates const timeWindowHtml = ` <span>${attributes.tremor_count}</span> earthquakes were recorded between ${timeBar.timeExtent.start.toLocaleDateString()} - ${timeBar.timeExtent.end.toLocaleDateString()}.<br/> `;
statsDiv.innerHTML = timeWindowHtml + statisticsHtml; } } catch (error) { console.error(error); } });How it works
Explore USGS earthquakes near Pomona, California, recorded between 5/25/2019 and 6/17/2019. Move through hourly stops in a locked 24-hour window to highlight earthquakes and update magnitude and depth statistics.
To start visualizing recorded earthquakes in this region, you can do one of the following:
- Hit the
playbutton to start the time animation. - Hit the
nextorpreviousbutton to move through hourly stops while keeping the 24-hour time window locked. - Drag the thumbs or segment to move through the hourly stops.