按类型可视化要素

尝试一下在线预览

此示例演示如何根据唯一值或类型对要素进行符号化。 数据存储在单个图层中,其中每个要素的可视化取决于一个或多个字段的值。

在完成以下步骤之前,您应该熟悉 视图 ,地图 FeatureLayer 。如果有必要,请先完成以下教程:

此应用程序的基本组件,例如创建  Map 和  MapView  类的实例以及理解 HTML 和 CSS 结构,将不予审查。 如果您需要熟悉此应用程序中的这些组件,请参阅上面列出的教程。 作为一般规则,上述教程中讨论的介绍性原则适用于文档中的大多数示例。

1. 为每种类型创建一个符号

在这个示例中,我们向代表美国主要高速公路和道路的图层添加了一个新的渲染器。 出于此应用程序的目的,我们希望根据它是州际公路、美国高速公路还是其他主要高速公路或道路来可视化每条高速公路。

首先,我们将以编程方式为每种类型定义一个单独的符号。我们将使用 SimpleLineSymbol 来可视化每个类型,因为它们是折线几何图形。

                       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Symbol for freeways
const fwySym = {
  type: "simple-line", // autocasts as new SimpleLineSymbol()
  color: "#30ffea",
  width: 1,
  style: "solid"
};

// Symbol for U.S. Highways
const hwySym = {
  type: "simple-line", // autocasts as new SimpleLineSymbol()
  color: "#ff6207",
  width: 1,
  style: "solid"
};

// Symbol for other major highways
const otherSym = {
  type: "simple-line", // autocasts as new SimpleLineSymbol()
  color: "#ef37ac",
  width: 1,
  style: "solid"
};

2. 创建 UniqueValueRenderer 一个实例

在定义如何根据字段值可视化要素时,我们必须使用 UniqueValueRenderer 。 最多可以使用三个字段来创建各种类型的组合。 在这种情况下,我们仅基于一个字段可视化每个要素:CLASS 。

      
1
2
3
4
5
6
const hwyRenderer = {
  type: "unique-value", // autocasts as new UniqueValueRenderer()
  field: "CLASS",
  defaultSymbol: otherSym, // used to visualize all features not matching specified types
  defaultLabel: "Other major highways" //  used in the legend for all other types not specified
};

3. 与每个符号匹配唯一的值

您可以通过以下两种方式之一将符号与唯一字段值匹配: 在构造函数中使用 uniqueValueInfos ...

                   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const hwyRenderer = {
  type: "unique-value", // autocasts as new UniqueValueRenderer()
  field: "CLASS",
  defaultSymbol: otherSym, // used to visualize all features not matching specified types
  defaultLabel: "Other major highways", //  used in the legend for all other types not specified
  // used for specifying unique values
  uniqueValueInfos: [
    {
      value: "I", // code for interstates/freeways
      symbol: fwySym,
      label: "Interstate" // used in the legend to describe features with this symbol
    },
    {
      value: "U", // code for U.S. highways
      symbol: hwySym,
      label: "US Highway" // used in the legend to describe features with this symbol
    }
  ]
};

或者使用 addUniqueValueInfo()  方法。

 
1
hwyRenderer.addUniqueValueInfo("I", fwySym);

4. 总结

一旦定义了渲染器,你可以在图层上设置它,视图和图例将自动更新。点击下面的沙盒按钮查看应用程序的完整代码。

尝试一下在线预览

5. 其他可视化教程和示例

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