This sample demonstrates how to set a visibilityTimeExtent on a GraphicsLayer so that the graphics layers can be animated over time to show additional information about the Kincade fire perimeter at the same time while the user interacts with the TimeSlider widget.
The visibility
specifies a fixed time extent during which a layer should be visible. This property is used to configure a layer that does not have time values stored in an attribute field to work with time. Once configured, the TimeSlider widget will display the layer within the set time extent.
The map displays the daily perimeters of the Kincade fire of 2019.
When the app is loaded, a query is made against all fire perimeters in the Kincade fire feature service. The query result is ordered by the recorded
field, and the app loops through the result, adding the date to dates array. It also creates a GraphicsLayer for each date and sets its visibility
to the date. A new graphic is created with a text symbol and its text property points to notes
attribute from the query result. The graphic is then added to the GraphicsLayer.
// dates array will hold all the unique dates from the query result made to kincade fire perimeters
let dates = new Array;
// create a query object that honors layers settings and order the query results by recordedDate
const query = featureLayer.createQuery();
query.orderByFields = ["recordedDate"];
const queryResult = await featureLayer.queryFeatures(query);
// loop through the query result and add dates to dates array
// create a new GraphicsLayer for each date and set its visibilityTimeExtent to the date
// create a graphic with text symbol containing the notes attribute from the query result
// add the graphic to the GraphicsLayer and set the time slider stops to dates array
queryResult.features.forEach((feature, i) => {
const date = new Date(feature.attributes.recordedDate);
const notes = feature.attributes.Notes;
// create a new graphic with text symbol and set text property
// to notes attribute of the query result
const graphic = new Graphic({
geometry: {
type: "point",
x: feature.geometry.centroid.x,
y: feature.geometry.centroid.y,
spatialReference: {
wkid: 102100
}
},
attributes: {
acres: feature.attributes.gisacres,
start: new Date(feature.attributes.recordedData),
},
symbol: new TextSymbol({
text: notes,
color: "black",
xoffset: 50,
yoffset: 10,
lineWidth: 200,
horizontalAlignment: "left",
backgroundColor: "white",
font: {
size: 12,
family: "sans-serif"
}
})
});
// create a new GraphicsLayer for each date and set its visibilityTimeExtent to the date
const glayer = createGraphicsLayer(date, new Date(feature.attributes.recordedDate), i);
glayer.add(graphic);
map.add(glayer);
// add the date to dates array
if (!dates.some(d => d.getTime() === date.getTime())) {
dates.push(date);
}
});
The create
function creates a new GraphicsLayer and sets its visibilityTimeExtent to match a given date as shown below.
// create a new GraphicsLayer for each date and set its visibilityTimeExtent to the date
// this function is called from the forEach loop above
function createGraphicsLayer(startDate, endDate, index) {
const graphicsLayer = new GraphicsLayer();
graphicsLayer.id = `graphicsLayer${index}`;
graphicsLayer.visibilityTimeExtent = {
start: startDate,
end: startDate
};
return graphicsLayer;
}
The TimeSlider widget stops are set to a dates array so that the user can visualize how the fire perimeters changed daily. The GraphicsLayer will be visible if its visibilityTimeExtent extent overlaps the TimeSlider's currentTimeExtent. The GraphicsLayer will display additional info about the fire perimeter for the given day.
// add a new time slider and set its stops to dates array
const timeSlider = new TimeSlider({
container: "timeSlider",
mode: "instant",
timeVisible: true,
playRate: 2000,
stops: {dates},
loop: true,
fullTimeExtent: new TimeExtent({
start: dates[0],
end: dates[dates.length-1]
}),
view: view
});
view.ui.add(timeSlider, "bottom-left");