参考 PopupTemplate 中的 Arcade 表达式

尝试一下在线预览

这个例子演示了如何在一个 FeatureLayer 上定义的 PopupTemplate 中显示一个 Arcade 表达式 的返回值。Arcade 对于基于从客户端执行的表达式计算出的值在 FeatureLayer 中创建可视化效果非常有用。PopupTemplates 可以引用渲染器中使用的相同表达式,以有效地传达数据驱动的可视化。

在此示例中,在地图中添加代表美国县的图层。 每个要素都包含与县内劳动力统计相关的属性。 这些字段包括失业率、人口和总就业人数。 Arcade 用于计算每个县的劳动力参与率。

在应用程序中使用四个 arcade 表达式:

  • 一种是根据大多数劳动年龄人口是否参与劳动力来对县进行分类。 这是确定一组值中的主要值的表达式。
                              
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
<script type="text/plain" id="predominance-arcade">
  // The fields from which to calculate predominance
  // The expression will return the alias of the predominant field

  var fields = [
    { value: $feature.NOT_LABORFORCE_16, alias: "NOT participating in the labor force" },
    { value: $feature.CIVLBFR_CY, alias: "participating in labor the force" }
  ];

  // Returns the predominant category as the alias
  // defined in the fields array. If there is a tie,
  // then both names are concatenated and used to
  // indicate the tie

  function getPredominantCategory(fieldsArray){
    var maxValue = -Infinity;
    var maxCategory = "";
    for(var k in fieldsArray){
      if(fieldsArray[k].value > maxValue){
        maxValue = fieldsArray[k].value;
        maxCategory = fieldsArray[k].alias;
      } else if (fieldsArray[k].value == maxValue){
        maxCategory = maxCategory + "/" + fieldsArray[k].alias;
      }
    }
    return maxCategory;
  }

  getPredominantCategory(fields);
</script>
  • 另一个用于返回多类别的值。
       
1
2
3
4
5
6
7
<script type="text/plain" id="strength-arcade">
  // Returns the share of the dominant demographic as a percentage
  var fieldValues = [ $feature.NOT_LABORFORCE_16, $feature.CIVLBFR_CY ];
  var winner = Max(fieldValues);
  var total = Sum(fieldValues);
  return (winner/total) * 100;
</script>
  • 一种返回未就业或劳动力的总人口。
 
1
$feature.POP_16UP - $feature.EMP_CY;
  • 最后,另一个是未就业的劳动年龄人口的百分比。
 
1
Round((($feature.POP_16UP - $feature.EMP_CY) / $feature.POP_16UP) * 100) + "%";

每个 Arcade 表达式都必须在 PopupTemplate 的  expressionInfos  属性中引用。expressionInfos  是一个对象数组,它为每个表达式分配名称和标题。

                          
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
const arcadeExpressionInfos = [
  {
    name: "predominance-arcade",
    title: "Is the majority (>50%) of the population 16+ participating in the labor force?",
    // defined in separate script element
    expression: document.getElementById("predominance-arcade").text
  },
  {
    name: "strength-arcade",
    title: "% of population belonging to majority category",
    // defined in separate script element
    expression: document.getElementById("strength-arcade").text
  },
  {
    name: "not-working-arcade",
    title: "Total population 16+ not employed or in labor force",
    expression: "$feature.POP_16UP - $feature.EMP_CY"
  },
  {
    name: "%-not-working-arcade",
    title: "% of population 16+ not employed or in labor force",
    expression: "Round((($feature.POP_16UP - $feature.EMP_CY)/$feature.POP_16UP)*100,2) + '%'"
  }
];

popupTemplate.expressionInfos = arcadeExpressionInfos;

该 名称  用于引用 popupTemplate 内容中的表达式值。 名称必须以 表达式/ 开头。

                            
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
popupTemplate.content = [
  {
    type: "text",
    text:
      "In this county, {UNEMPRT_CY}% of the labor force is unemployed. " +
      " {expression/strength-arcade}% of the {POP_16UP} people ages 16+" +
      " living here are {expression/predominance-arcade}."
  },
  {
    type: "fields",
    fieldInfos: [
      {
        fieldName: "expression/not-working-arcade",
        format: {
          digitSeparator: true,
          places: 0
        }
      },
      {
        fieldName: "expression/%-not-working-arcade",
        format: {
          digitSeparator: true,
          places: 0
        }
      }
    ]
  }
];

用红色圈出的值是从上面引用的 Arcade 表达式返回的值。

popup-arcade

有关更多信息,请参阅 Arcade 表达式指南的 Popup profile 

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