数据驱动的连续大小

尝试一下在线预览

此示例演示如何基于数字字段中的数据,沿连续大小斜坡可视化图层中的要素。

在这种情况下,我们将使用带有 视觉变量SimpleRenderer ,根据生活在贫困线以下的人口百分比来改变每个要素的大小。 使用的图层包含县的多边形要素。 我们可以为渲染器分配一个标记符号,以便每个县的质心覆盖一个标记,该标记的大小可以根据数字属性进行更改

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

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

1. 创建一个 SimpleRenderer 并给它分配一个默认符号

在创建具有连续大小斜坡的渲染器时,所需要的是一个带有 视觉变量 SimpleRenderer

在这个应用程序中,我们在渲染器的 symbol 属性中设置了一个默认的符号。我们不需要在符号上定义 size ,因为每个要素的大小将由 视觉变量 决定。

           
1
2
3
4
5
6
7
8
9
10
11
const renderer = {
  type: "simple", // autocasts as new SimpleRenderer()
  symbol: {
    type: "simple-marker", // autocasts as new SimpleMarkerSymbol()
    color: "palegreen",
    outline: {
      color: "seagreen",
      width: 0.5
    }
  }
};

2. 在渲染器上设置一个大小可视变量

设置任何可视变量都需要一个 字段 名称,该名称指示可视化所基于的数据。 您还可以指定一个 normalizationField 来规范化 字段 中指定的数据值。 在此示例中,我们将字段指向 POP_POVERTY,它存储了要素边界内生活在贫困中的总人数。 我们将使用TOTPOP_CY 字段根据总人口进行标准化。

然后,您可以使用 stops 数组中的一系列 stops 来设置大小渐变...

                              
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
const renderer = {
  type: "simple", // autocasts as new SimpleRenderer()
  symbol: defaultSym, // the default symbol defined in step 1
  label: "% population in poverty by county", // label for the legend
  visualVariables: [
    {
      type: "size", // indicates this is a size visual variable
      field: "POP_POVERTY", // total population in poverty
      normalizationField: "TOTPOP_CY", // total population
      stops: [
        {
          value: 0.15, // features where < 15% of the pop is in poverty
          size: 4, // will be assigned a marker with this size in pts
          label: "less than 15%" // label to display in the legend
        },
        {
          value: 0.35, // features where > 35% of the pop is in poverty
          size: 24, // will be assigned a marker with this size in pts
          label: "more than 35%" // label to display in the legend
        }
      ]

      // features with values between 0.15 - 0.35 will be assigned
      // a size between 4 pts and 24 pts proportional
      // to where the value falls between 0.15 and 0.35
      // e.g. if a county has a pov rate of 0.25, then it will
      // have a marker sized at 14 pts
    }
  ]
};

...或在将基于  minDataValuea  和 maxDataValue  应用的 minSize  和  maxSize  属性中。

                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const renderer = {
  type: "simple", // autocasts as new SimpleRenderer()
  symbol: defaultSym, // the default symbol defined in step 1
  label: "% population in poverty by county", // label for the legend
  visualVariables: [
    {
      type: "color", // indicates this is a color visual variable
      field: "POP_POVERTY", // total population in poverty
      normalizationField: "TOTPOP_CY", // total population
      minDataValue: 0.1, // features where < 10% of the pop in poverty
      maxDataValue: 0.3, // features where > 30% of the pop in poverty
      minSize: 4, // size of marker in pts
      maxSize: 24 // size of marker in pts
    }
  ]
};

在第一种情况下,您可以指定两个以上的  stops ,并将数据值分配给每个 stops 的特定大小。 您还可以在 minSizemaxSize 属性上设置尺寸视觉变量,以定义各种比例范围的要素的屏幕尺寸。 有关更多详细信息,请参阅 尺寸视觉变量 的文档。 使用 GeoJSON 数据示例创建 FeatureLayer 还演示了如何执行此操作。

3. 总结

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

尝试一下在线预览

5.其他可视化示例和资源

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