数据驱动的连续颜色

尝试一下在线预览

此示例演示如何基于数字字段中的数据沿着连续的颜色渐变可视化图层中的要素。

在这种情况下,我们将使用带有 visual variables  的  SimpleRenderer ,根据生活在贫困线以下的人口百分比来改变每个要素的颜色。

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

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

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

当创建一个带有连续颜色渐变的渲染器时,所需要的是一个带有 visual variables 的 SimpleRenderer 

在这个应用程序中,我们在渲染器的 symbol  属性中设置了一个默认的符号。我们不需要在符号上定义color ,因为每个要素的颜色将由 visual variables 决定。

           
1
2
3
4
5
6
7
8
9
10
11
const renderer = {
  type: "simple", // autocasts as new SimpleRenderer()
  symbol: {
    type: "simple-fill", // autocasts as new SimpleFillSymbol()
    outline: {
      // makes the outlines of all features consistently light gray
      color: "lightgray",
      width: 0.5
    }
  }
};

2. 在渲染器上设置一个颜色视觉变量

设置颜色可视变量需要一个field 名称,该名称指示可视化的数据。 您还可以指定一个 normalizationField  来规范化 field中指定的数据值。 在此示例中,我们将字段指向  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
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
      stops: [
        {
          value: 0.1, // features where < 10% of the pop in poverty
          color: "#FFFCD4", // will be assigned this color (beige)
          label: "10% or lower" // label to display in the legend
        },
        {
          value: 0.3, // features where > 30% of the pop in poverty
          color: "#350242", // will be assigned this color (purple)
          label: "30% or higher" // label to display in the legend
        }
      ]

      // features with values between 0.1 - 0.3 will be assigned
      // a color on a ramp between beige and purple proportional
      // to where the value falls between 0.1 and 0.3
    }
  ]
};

3. 总结

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

尝试一下在线预览

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

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