Hi all,
I wrote a Python script that reads some maps that already have "GraduatedColorsRenderer" with 8 breaks for symbology.
The script is supposed to use CIM definitions to change Legends Label formatting to Percentage with 1 decimal point.
The script runs without any errors but when you check the layer, the changes are not applied visually. Although when you check the "Advanced symbology" pane, it shows that the changes are there. (Also after running the script, if you call components in "l_cim.renderer.numberFormat" it shows the changes are made.
If you manually "refresh values" in "primary symbology" pane or change the decimal point or check thousand separator checkbox in "Advanced symbology" pane, suddenly all the changes from before applies to the labels. (See the Screenshots and Script below)
Question 1) Is there any refresh method I should use or that's just a bug?
Question 2) How to change the option to Number represented as fractions (using python)
Here is an example of the issue:
Before Running the Script:
(Actual legend on the left and the formatting setting on the right)
After Running the Script:
Category and decimal places changed on the right,
but the actual labels are the same as before
After Changing Decimal places to 2:
Here is the python Script:
import arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT")
#list of current map names
map_list = ['map_cityA', 'map_cityB', 'map_cityC' ]
# set symbology for each map
for map_name in map_list:
mp = aprx.listMaps(map_name)[0]
for lyr in mp.listLayers():
if lyr.isFeatureLayer:
l_cim = lyr.getDefinition('V3')
# create an empty empty numeric object for percentage
num_obj = arcpy.cim.CreateCIMObjectFromClassName('CIMPercentageFormat', 'V3')
num_obj.alignmentOption = 'esriAlignLeft'
num_obj.alignmentWidth = 12
num_obj.roundingOption = 'esriRoundNumberOfDecimals'
num_obj.roundingValue = 1 # decimal places to show
num_obj.zeroPad = False
# num_obj.useSeparator = False
num_obj.showPlusSign=False
# assign the new numeric object to the renderer's number format
l_cim.renderer.numberFormat = num_obj
lyr.setDefinition(l_cim) #set the CIM
# tried to force it to refresh (did not work)
l_cim = lyr.getDefinition('V3')
lyr.setDefinition(l_cim)
#save project after
aprx.save()
Thank you all!
My platform: Windows 11 (22H2) and ArcGISPro 3.1.0
do you close the symbology tab before running the script and reopen it after?
Yes, I only kept the script open and all the maps and layouts were closed while running it. After seeing your comment I made sure that the empty symbology tab is closed as well, but that made no difference.
I also kept one of the maps open to see if that changes anything and it did not.
I forgot to mention that I also run l_cim.renderer.heading = 'A new Heading' in the same loop and that immediately works and changes the symbology heading but the legends labels (as mentioned in the main post) don't change!
I don't think lines 27 and 28 will force a refresh, so you may as well get rid of those. and don't keep any maps open to see if it changes anything, open it after, and if that does nothing, save the project, close it and reopen it to see if the changes persisted... I am not convinced that the project is being saved if it is open and the script isn't being run outside of an open project
Thank you for the comment Dan. I got the idea of Force refresh (lines 27 and 28) from Example 2 and 3 of this documentation, although that was for Layout, I thought it might work on the layers as well. So if that doesn't apply here I will exclude that.
Yeah, I normally don't open any maps before running the script . I did otherwise once just to test.
I also put aprx.save() inside the loop to save project after changing each map and that did not fix it. Closing and opening the project also did not fix it.
The part that I still don't get is when I put l_cim.renderer.heading = 'A new Heading' into line 11 or 24 of the script (before or after changing the labels) the heading gets changed immediately even if the map is open! They are both under l_cim.renderer and why are they showing these different behaviors?
@Mahdi_Ch did you ever find a solution to this? I'm having the same issue