查询统计数据客户端

尝试一下在线预览

此示例演示如何在 FeatureLayerView查询统计数据,以及如何在图表中将查询结果显示为 LayerList 面板中的自定义内容。

此应用程序显示墨西哥的教育程度数据。该图层使用 UniqueValueRenderer 中的 Arcade 表达式显示每个城市中所有 12 岁及以上的居民所达到的主要教育水平。在视图范围中所有要素中属于每个类别的总人口将显示在 LayerList 面板内容的图表中。每次用户平移或放大/缩小视图时,此图表都会更新。 

工作原理

视图准备就绪后,创建一个 LayerList 实例,并将 DOM 元素作为自定义内容添加到每个列表项目的面板中。您可以在这些元素中放置任何内容。在此示例中,我们将饼图放置在优势图层的列表项目面板中,并用文本包围。我们还将 LayerList 的容器属性设置为自定义 DOM 元素,以将其放置在视图的默认 UI 之外。

                                  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
let layerList = new LayerList({
  view: view,
  container: document.createElement("div"),
  listItemCreatedFunction: function(event) {
    const item = event.item;

    // add the pie chart to the Predominance layer list item panel
    if (item.title === predominanceLayer.title) {
      item.panel = {
        content: [
          [
            "<b>Educational attainment</b> refers to the highest level of education that an individual has completed. ",
            "This chart categorizes the population living within the current ",
            "view extent by their educational attainment."
          ].join(""),

          document.createElement("canvas"),

          [
            "Notice that while one attainment level appears to dominate certain regions, it doesn't ",
            "necessarily mean it represents the majority of the population. In fact, as ",
            "you explore most areas, you will find the predominant educational attainment makes up ",
            "just a fraction of the population due to the number of categories considered."
          ].join("")
        ],
        className: "esri-icon-pie-chart",
        open: item.visible
      };
    }
  }
});
layerList.container.style = "height: 100%";
let panelDiv = document.getElementById("panel");
panelDiv.appendChild(layerList.container);

FeatureLayerView 上的统计数据查询发生在客户端,由于响应时间较短,因此使您能够创建高度交互式的应用程序。在此示例中,每次用户平移和放大或缩小视图时,都会在图层上查询统计数据。我们通过观察图层视图的静止属性来处理此问题。 

      
1
2
3
4
5
6
// Update the pie chart after the user stops panning or zooming
watchUtils.whenTrue(view, "stationary", function(val) {
  queryLayerViewStats(layerView).then(function(newData) {
    updateChart(newData);
  });
});

创建 StatisticDefinition 对象数组,为要查询其统计数据的每个字段设置字段名称和统计数据类型。在此示例中,我们希望显示每个教育程度类别中的总人数。为此,我们指定每个字段名称并设置 sum 统计数据类型。这将对视图范围内所有要素的每个字段的值求和。

                                                              
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function queryLayerViewStats(layerView) {
  const educationFields = [
    "EDUC01_CY",
    "EDUC02_CY",
    "EDUC03_CY",
    "EDUC04_CY",
    "EDUC05_CY",
    "EDUC06_CY",
    "EDUC07_CY",
    "EDUC08_CY",
    "EDUC09_CY",
    "EDUC10_CY",
    "EDUC11_CY",
    "EDUC12_CY",
    "EDUC13_CY",
    "EDUC14_CY",
    "EDUC15_CY",
    "EDUC16_CY",
    "EDUCA_BASE"
  ];

  // Creates a query object for statistics of each of the fields listed above
  const statDefinitions = educationFields.map(function(fieldName) {
    return {
      onStatisticField: fieldName,
      outStatisticFieldName: fieldName + "_TOTAL",
      statisticType: "sum"
    };
  });

  // query statistics for features only in view extent
  const query = layerView.layer.createQuery();
  query.outStatistics = statDefinitions;
  query.geometry = view.extent;

  // query features within the view's extent on the client
  return layerView.queryFeatures(query).then(function(response) {
    const stats = response[0].attributes;

    const updatedData = [
      stats.EDUC01_CY_TOTAL, // no school
      stats.EDUC02_CY_TOTAL, // preschool
      stats.EDUC03_CY_TOTAL, // some elementary
      stats.EDUC04_CY_TOTAL + stats.EDUC07_CY_TOTAL, // elementary
      stats.EDUC05_CY_TOTAL, // some secondary
      stats.EDUC06_CY_TOTAL + stats.EDUC08_CY_TOTAL, // secondary
      stats.EDUC09_CY_TOTAL + stats.EDUC11_CY_TOTAL, // high school
      stats.EDUC10_CY_TOTAL +
        stats.EDUC12_CY_TOTAL +
        stats.EDUC13_CY_TOTAL +
        stats.EDUC14_CY_TOTAL +
        stats.EDUC15_CY_TOTAL, // college
      stats.EDUC16_CY_TOTAL // not specified
    ];

    // data used to update the pie chart
    return {
      total: stats.EDUCA_BASE_TOTAL,
      values: updatedData
    };
  });
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.