Query statistics by geometry
Note: Support for 3D on mobile devices may vary, view the system requirements for more information.
This sample demonstrates how to query for statistics in a FeatureLayerView by geometry and display the results of the query in a chart.
This application displays 2010 population density by census tracts. The population is queried by age and gender among census tracts intersecting a buffer, and is displayed in a population pyramid chart.
How it works
The population pyramid chart is updated as the user drags the center point of the buffer or resizes the buffer by moving its edge handler point. As the user moves one of the two points, a geodesic buffer is recalculated and used to query the statistics of all features in the layer view that intersect the buffer. The center and edge handler points of the buffer graphic are updated by listening to the move
events on SketchViewModel.
sketchViewModel.on("move-start", onMove);
sketchViewModel.on("move", onMove);
sketchViewModel.on("move-complete", onMove);
function onMove(event) {
if (activeGraphic === centerGraphic) {
centerGraphic.geometry = event.geometry;
const edgeScreenPoint = view.toScreen(edgeGraphic.geometry);
const edgeX = edgeScreenPoint.x + event.dx;
const edgeY = edgeScreenPoint.y + event.dy;
edgeGraphic.geometry = view.toMap(edgeX, edgeY);
}
else if (activeGraphic === edgeGraphic) {
edgeGraphic.geometry = event.geometry;
}
const vertices = [
[centerGraphic.geometry.x, centerGraphic.geometry.y],
[edgeGraphic.geometry.x, edgeGraphic.geometry.y]
];
calculateBuffer(vertices);
}
The updated buffer polygon is then used to query statistics to get total population belonging to different age categories by gender among census tracts. Once the statistics query results are returned, the information is displayed in the population pyramid chart.
function queryLayerViewAgeStats(buffer) {
let femaleAgeData = [],
maleAgeData = [];
const query = featureLayerView.layer.createQuery();
query.outStatistics = statDefinitions;
query.geometry = buffer;
return featureLayerView.queryFeatures(query).then(function(results) {
const attributes = results.features[0].attributes;
for (var key in attributes) {
if (key.includes("FEM")) {
femaleAgeData.push(attributes[key]);
} else {
maleAgeData.push(-Math.abs(attributes[key]));
}
}
return [femaleAgeData, maleAgeData];
})
.catch(function(error) {
console.log(error);
});
}