Set font size using an if statement condition for FeatureLayer labelingInfo using arcgis javascript api 4.x

huangapple go评论105阅读模式
英文:

Set font size using an if statement condition for FeatureLayer labelingInfo using arcgis javascript api 4.x

问题

我正在尝试使用ArcGIS JavaScript API 4.x为FeatureLayer的labelingInfo属性使用if语句条件来设置字体大小。

这是FeatureLayer的部分代码:

const myLayer = new FeatureLayer({
    title: "My Layer",
    objectIdField: "ObjectID",
    geometryType: "point",
    source: myFeatures,
    showLabels: true,
    labelingInfo: myLabelingInfo,
    outFields: ['*']
});

这是labelingInfo对象的部分代码:

var myLabelingInfo = [
    {
        symbol: {
            type: "text",
            color: "white",
            font: {
                family: "Arial",
                size: "IIf($feature.customer.length == 1, 12, 8);", // 12,
                weight: "bold",
                decoration: "none"
            }
        }
    }
];

如您所见,我尝试使用if语句设置字体大小:

size: "IIf($feature.customer.length == 1, 12, 8);", // 12,

但这不起作用。只有在硬编码的情况下才有效:

size: 12,

我的问题是,如何以动态/编程的方式更改字体大小?

英文:

I am trying to set font size using an if statement condition for FeatureLayer labelingInfo using arcgis javascript api 4.x.

This is the feature layer:

    const myLayer = new FeatureLayer({
        title: "My Layer",
        objectIdField: "ObjectID",
        geometryType: "point",
        source: myFeatures,
        showLabels: true,
        labelingInfo: myLabelingInfo,
        outFields : ['*']
    });

This is the labelinginfo object:

    var myLabelingInfo = [
        {
            symbol: {
                type: "text",
                color: "white",
                font: {
                    family: "Arial",
                    size: "IIf($feature.customer.length == 1, 12, 8);", // 12,
                    weight: "bold",
                    decoration: "none"
                }
            }
        }
    ];

As you can see, I tried setting the font size with an if statement:

size: "IIf($feature.customer.length == 1, 12, 8);", // 12,

But that does not work. If I set it hard-coded is the only way it works:

size: 12,

My question is, how can I dynamically/programmatically change the font size?

答案1

得分: 1

以下是您要翻译的内容:

"One way of achieving what you want is to have as many LabelClass as symbols you need, and then using the property where to apply correctly to each feature.

In this case, you will have two LabelClass, one for each font size,

const createLabelSymbol = (fontSize) => ({
    symbol: {
        type: "text",
        color: "white",
        font: {
            family: "Arial",
            size: fontSize,
            weight: "bold",
            decoration: "none"
        }
    }
});

const myLayer = new FeatureLayer({
    title: "My Layer",
    objectIdField: "ObjectID",
    geometryType: "point",
    source: myFeatures,
    showLabels: true,
    labelingInfo: [
        {
            labelExpressionInfo: {expression: "EXPRESSION"},
            symbol: createLabelSymbol(12),
            where: "CONDITION"
        },
        {
            labelExpressionInfo: {expression: "EXPRESSION"},
            symbol: createLabelSymbol(8),
            where: "NOT CONDITION"
        }
    ],
    outFields : ['*']
});

Where EXPRESSION is an arcade label expression, and CONDITION is an sql where expression.
Take a look at the example I put for you base on one of ESRI,

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot; /&gt;
    &lt;meta
      name=&quot;viewport&quot;
      content=&quot;initial-scale=1,maximum-scale=1,user-scalable=no&quot;
    /&gt;
    &lt;title&gt;
      Add labels to a FeatureLayer | Sample | ArcGIS Maps SDK for JavaScript
      4.27
    &lt;/title&gt;

    &lt;link
      rel=&quot;stylesheet&quot;
      href=&quot;https://js.arcgis.com/4.27/esri/themes/light/main.css&quot;
    /&gt;

    &lt;style&gt;
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
        background-color: black;
      }
    &lt;/style&gt;

    &lt;script src=&quot;https://js.arcgis.com/4.27/&quot;&gt;&lt;/script&gt;

    &lt;script&gt;
      require([
        &quot;esri/WebMap&quot;,
        &quot;esri/views/MapView&quot;,
        &quot;esri/layers/FeatureLayer&quot;
      ], (WebMap, MapView, FeatureLayer) =&gt; {
        const labelClassOpen = {
          // autocasts as new LabelClass()
          symbol: {
            type: &quot;text&quot;, // autocasts as new TextSymbol()
            color: &quot;green&quot;,
            backgroundColor: [213, 184, 255, 0.75],
            borderLineColor: &quot;green&quot;,
            borderLineSize: 1,
            yoffset: 5,
            font: {
              // autocast as new Font()
              family: &quot;Playfair Display&quot;,
              size: 12,
              weight: &quot;bold&quot;
            }
          },
          labelPlacement: &quot;above-center&quot;,
          labelExpressionInfo: {
            expression: &quot;$feature.MARKER_ACTIVITY&quot;
          },
          where: &quot;OPENSTATUS=&#39;open&#39;&quot;
        };
        const labelClassOther = {
          // autocasts as new LabelClass()
          symbol: {
            type: &quot;text&quot;, // autocasts as new TextSymbol()
            color: &quot;grey&quot;,
            backgroundColor: [213, 184, 255, 0.75],
            borderLineColor: &quot;grey&quot;,
            borderLineSize: 1,
            yoffset: 5,
            font: {
              // autocast as new Font()
              family: &quot;Playfair Display&quot;,
              size: 8,
              weight: &quot;bold&quot;
            }
          },
          labelPlacement: &quot;above-center&quot;,
          labelExpressionInfo: {
            expression: &quot;$feature.MARKER_ACTIVITY&quot;
          },
          where: &quot;NOT OPENSTATUS=&#39;open&#39;&quot;
        }

        const view = new MapView({
          container: &quot;viewDiv&quot;,
          map: new WebMap({
            portalItem: {
              // autocasts as new PortalItem
              id: &quot;372b7caa8fe340b0a6300df93ef18a7e&quot;
            },
            layers: [
              new FeatureLayer({
                portalItem: {
                  // autocasts as new PortalItem
                  id: &quot;6012738cd1c74582a5f98ea30ae9876f&quot;
                },
                labelingInfo: [labelClassOpen, labelClassOther],
                renderer: {
                  type: &quot;simple&quot;, // autocasts as new SimpleRenderer()
                  symbol: {
                    type: &quot;simple-marker&quot;, // autocasts as new SimpleMarkerSymbol()
                    color: &quot;rgba(0,100,0,0.6)&quot;,
                    size: 3,
                    outline: {
                      color: [0, 0, 0, 0.1],
                      width: 0.5
                    }
                  }
                }
              })
            ]
          }),
          center: [-116.925, 34.2501],
          zoom: 12
        });
      });
    &lt;/script&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;div id=&quot;viewDiv&quot;&gt;&lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->"

英文:

One way of achieving what you want is to have as many LabelClass as symbols you need, and then using the property where to apply correctly to each feature.

In this case, you will have two LabelClass, one for each font size,

const createLabelSymbol = (fontSize) =&gt; ({
    symbol: {
        type: &quot;text&quot;,
        color: &quot;white&quot;,
        font: {
            family: &quot;Arial&quot;,
            size: fontSize,
            weight: &quot;bold&quot;,
            decoration: &quot;none&quot;
        }
    }
});

const myLayer = new FeatureLayer({
    title: &quot;My Layer&quot;,
    objectIdField: &quot;ObjectID&quot;,
    geometryType: &quot;point&quot;,
    source: myFeatures,
    showLabels: true,
    labelingInfo: [
        {
            labelExpressionInfo: {expression: &quot;EXPRESSION&quot;},
            symbol: createLabelSymbol(12),
            where: &quot;CONDITION&quot;
        },
        {
            labelExpressionInfo: {expression: &quot;EXPRESSION&quot;},
            symbol: createLabelSymbol(8),
            where: &quot;NOT CONDITION&quot;
        }
    ],
    outFields : [&#39;*&#39;]
});

Where EXPRESSION is an arcade label expression, and CONDITION is an sql where expression.
Take a look at the example I put for you base on one of ESRI,

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot; /&gt;
    &lt;meta
      name=&quot;viewport&quot;
      content=&quot;initial-scale=1,maximum-scale=1,user-scalable=no&quot;
    /&gt;
    &lt;title&gt;
      Add labels to a FeatureLayer | Sample | ArcGIS Maps SDK for JavaScript
      4.27
    &lt;/title&gt;

    &lt;link
      rel=&quot;stylesheet&quot;
      href=&quot;https://js.arcgis.com/4.27/esri/themes/light/main.css&quot;
    /&gt;

    &lt;style&gt;
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
        background-color: black;
      }
    &lt;/style&gt;

    &lt;script src=&quot;https://js.arcgis.com/4.27/&quot;&gt;&lt;/script&gt;

    &lt;script&gt;
      require([
        &quot;esri/WebMap&quot;,
        &quot;esri/views/MapView&quot;,
        &quot;esri/layers/FeatureLayer&quot;
      ], (WebMap, MapView, FeatureLayer) =&gt; {
        const labelClassOpen = {
          // autocasts as new LabelClass()
          symbol: {
            type: &quot;text&quot;, // autocasts as new TextSymbol()
            color: &quot;green&quot;,
            backgroundColor: [213, 184, 255, 0.75],
            borderLineColor: &quot;green&quot;,
            borderLineSize: 1,
            yoffset: 5,
            font: {
              // autocast as new Font()
              family: &quot;Playfair Display&quot;,
              size: 12,
              weight: &quot;bold&quot;
            }
          },
          labelPlacement: &quot;above-center&quot;,
          labelExpressionInfo: {
            expression: &quot;$feature.MARKER_ACTIVITY&quot;
          },
          where: &quot;OPENSTATUS=&#39;open&#39;&quot;
        };
        const labelClassOther = {
          // autocasts as new LabelClass()
          symbol: {
            type: &quot;text&quot;, // autocasts as new TextSymbol()
            color: &quot;grey&quot;,
            backgroundColor: [213, 184, 255, 0.75],
            borderLineColor: &quot;grey&quot;,
            borderLineSize: 1,
            yoffset: 5,
            font: {
              // autocast as new Font()
              family: &quot;Playfair Display&quot;,
              size: 8,
              weight: &quot;bold&quot;
            }
          },
          labelPlacement: &quot;above-center&quot;,
          labelExpressionInfo: {
            expression: &quot;$feature.MARKER_ACTIVITY&quot;
          },
          where: &quot;NOT OPENSTATUS=&#39;open&#39;&quot;
        }

        const view = new MapView({
          container: &quot;viewDiv&quot;,
          map: new WebMap({
            portalItem: {
              // autocasts as new PortalItem
              id: &quot;372b7caa8fe340b0a6300df93ef18a7e&quot;
            },
            layers: [
              new FeatureLayer({
                portalItem: {
                  // autocasts as new PortalItem
                  id: &quot;6012738cd1c74582a5f98ea30ae9876f&quot;
                },
                labelingInfo: [labelClassOpen, labelClassOther],
                renderer: {
                  type: &quot;simple&quot;, // autocasts as new SimpleRenderer()
                  symbol: {
                    type: &quot;simple-marker&quot;, // autocasts as new SimpleMarkerSymbol()
                    color: &quot;rgba(0,100,0,0.6)&quot;,
                    size: 3,
                    outline: {
                      color: [0, 0, 0, 0.1],
                      width: 0.5
                    }
                  }
                }
              })
            ]
          }),
          center: [-116.925, 34.2501],
          zoom: 12
        });
      });
    &lt;/script&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;div id=&quot;viewDiv&quot;&gt;&lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月29日 21:48:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定