Select to view content in your preferred language

Control layer transparency by scale using Arcade

1951
3
Jump to solution
04-19-2023 12:01 PM
Labels (2)
erica_poisson
Frequent Contributor

Hi -

I've seen many posts about scale-based symbology using Arcade (e.g. change the point size). I was wondering if it was possible to control a layer's transparency via Arcade so that a layer becomes more transparent as a user zooms in on a map. 

Currently, I have two copies of the same layer in my map - one is opaque and the other is semi-transparent. As the user zooms in on the map, the opaque version disappears and is 'replaced' by the semi-transparent version. 

Ideally, I'd like to use Arcade to change the transparency of a single layer as the user zooms in. This would help immensely as I am using my map in Experience Builder and the filter widget can only be applied to one layer. 

Any advice would be appreciated,

Erica
0 Kudos
1 Solution

Accepted Solutions
erica_poisson
Frequent Contributor

I wanted to post my solution to this in case it is useful to others. I reached out to our local Esri contacts and was informed that while this is possible using Arcade, there is a bug in the map viewer which prevents it from working properly. Below is a workaround which I tested and was successfully able to implement in my map:

  1. Open your web map and select the layer > Styles.
  2. Set symbology and then toggle on "Transparency by attribute" at the bottom of the Styles dialog.
  3. Click the </> and add a new expression.
  4. Leave expression title as "New expression".
  5. In body, type $view.scale
  6. Click Save.
  7. Select a numeric field for transparency normalization (I selected "Shape_Length").
  8. Open AGO Assistant and view the JSON of your web map.
  9. In the JSON code, locate the code that looks like this:
erica_poisson_0-1684155397994.jpeg

 

 
10. Replace it with something like this. Note that where I have "36111.909643" should be the scale at which you want the transparency to change.
erica_poisson_1-1684155397995.jpeg

 

I tested this and it worked. I would recommend testing in a copy of your web map and copying/pasting default JSON into Notepad just in case. 

Erica

View solution in original post

3 Replies
erica_poisson
Frequent Contributor

I wanted to post my solution to this in case it is useful to others. I reached out to our local Esri contacts and was informed that while this is possible using Arcade, there is a bug in the map viewer which prevents it from working properly. Below is a workaround which I tested and was successfully able to implement in my map:

  1. Open your web map and select the layer > Styles.
  2. Set symbology and then toggle on "Transparency by attribute" at the bottom of the Styles dialog.
  3. Click the </> and add a new expression.
  4. Leave expression title as "New expression".
  5. In body, type $view.scale
  6. Click Save.
  7. Select a numeric field for transparency normalization (I selected "Shape_Length").
  8. Open AGO Assistant and view the JSON of your web map.
  9. In the JSON code, locate the code that looks like this:
erica_poisson_0-1684155397994.jpeg

 

 
10. Replace it with something like this. Note that where I have "36111.909643" should be the scale at which you want the transparency to change.
erica_poisson_1-1684155397995.jpeg

 

I tested this and it worked. I would recommend testing in a copy of your web map and copying/pasting default JSON into Notepad just in case. 

Erica
JoshuaReyling
Occasional Contributor

Hi, Erica! 

I was just trying to do the same thing, with more added hurdle - I needed to filter only certain features within a layer based on view scale.  I ended up working out a solution that might be easier than editing the JSON.

JoshuaReyling_0-1686153509213.png

 


In an expression, I mapped binary threshold values in variables with an IIf(), and set numeric values based on those thresholds.  Then I simply multiplied those variables together.  In my case, any feature with a program type of 'adaptive management' has a value of 0, and all features have a value of 100 when zoomed in beyond 1:50,000.  This means that when I zoom in, my adaptive management features are still 0 (0*100) and all my other features are at 10,000 (100*100).  Then I simply set my transparency slider accordingly.

 

I think your situation might be easier, because you only need the one variable.  Hope this helps and isn't too confusing of a walkthrough!

0 Kudos
erica_poisson
Frequent Contributor

Hi Josh,

Thanks for sharing that example - I'll give it a try. I was actually going to try it out this morning, but when I open the Map Viewer (in Chrome or Firefox) and select the layer, there is no longer a "transparency by attribute" option under the symbology settings. I'm mystified. 

erica_poisson_0-1686313525479.png

 

Erica
0 Kudos