Adding callout text to selected feature with an incremental value?

821
4
06-22-2020 06:45 AM
helenchu
Occasional Contributor II

Hi all,

My ultimate goal to add callout text to my selected features with an incremental value like "sale 1", "sale 2"," sale 3" ect... What I have right now is to set up label to display label in callout text and use this snippet to turn the label on for selected features.  My issue is my label doesn't come from attribute table and each label is different from the others.  How do I modify this code to make it work or if this approach doesn't work in my case, what else could I use to accomplish it?  Thank you very much.

//Layer to label. US Cities lyr was used
      var lyrOfInterest = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();
      QueuedTask.Run(() => {
        var selection = MapView.Active.Map.GetSelection();
        //Get the OIDs of the selected features
        //We will use this to label those features only
        var selectedOIDs = selection[lyrOfInterest];
        //Get the layer's definition
        var lyrDefn = lyrOfInterest.GetDefinition() as CIMFeatureLayer;
        if (lyrDefn == null) return;
        //Get the label classes - we need the first one
        //Or you could create a new label class
        var listLabelClasses = lyrDefn.LabelClasses.ToList();
        var theLabelClass = listLabelClasses.FirstOrDefault();
        //set the label class Expression to use the Arcade expression
        theLabelClass.Expression = "$feature.CITY_NAME"; //label using city name
        //Where clause is set to the OIDs of the selected features
        theLabelClass.WhereClause = $"OBJECTID IN ({string.Join(",", selectedOIDs)})";
        //theLabelClass.TextSymbol = Set to any text symbol you want 
        //Set the label definition back to the layer.
        lyrOfInterest.SetDefinition(lyrDefn);
        lyrOfInterest.SetLabelVisibility(true);
      });
0 Kudos
4 Replies
UmaHarano
Esri Regular Contributor

Hi Helen,

Where will these values come from? For example:  If there are 10 selected features, is there any order to which feature will be sale1, sale 2?

Thanks

Uma

0 Kudos
helenchu
Occasional Contributor II

Yes there's order.  I have a listbox that store all accounts to search for in order.   Thanks.

0 Kudos
UmaHarano
Esri Regular Contributor

Hi Helen,

To label features in Pro with values that are dynamic, we use Label expressions.  Using Arcade, Python, you can make simple calculations (that increment a number, for example) and dynamically display the labels. So for example, if you want to add the word "Sale" in your label and then append a number that increments, it might be possible to do that with an expression using the Python or Arcade – If you have a starting number that you know.

But - To label features, it is recommended to have all the information in your feature table. Static info can be appended.

Thanks

Uma

0 Kudos
helenchu
Occasional Contributor II
In ArcObjects I placed text element at centroid of selected features.  If I want to do the same thing in Pro SDK what snippets should I look for ?  Is it the overlay?  Can I do this in layout view ?  My users asked me to keep the old way so they can move the callouts around to their taste .  Thank you.
Public Sub AddCompCallout_mapview(pFeature As IFeature, strCompLabel As String, strCompNum As String)
'add callout text

Dim pTextElement As ITextElement
pTextElement = New TextElement

Dim pElement As IElement
pElement = pTextElement
pTextElement.Text = strCompLabel + " " + strCompNum

Dim pArea As IArea
Dim pPointCentroid As IPoint = New Point
Dim pPointAnchor As IPoint = New Point
Dim pEnv As New Envelope

pEnv = pActiveview.Extent

pEnv.XMax = pFeature.Shape.Envelope.XMax
pEnv.XMin = pFeature.Shape.Envelope.XMin
pEnv.YMax = pFeature.Shape.Envelope.YMax
pEnv.YMin = pFeature.Shape.Envelope.YMin

pArea = pFeature.Shape
pPointCentroid.PutCoords(pEnv.LowerRight.X, pEnv.UpperRight.Y)

pPointAnchor = pArea.Centroid
pElement.Geometry = pPointCentroid


'font
Dim pFontDisp As stdole.IFontDisp
pFontDisp = New stdole.StdFont
pFontDisp.Name = "Arial"
pFontDisp.Bold = True
pFontDisp.Size = 14


'color
Dim pColor As ESRI.ArcGIS.Display.IRgbColor = New RgbColor
pColor.Blue = 255
pColor.Green = 0
pColor.Red = 0

Dim pTextSymbol As IFormattedTextSymbol
pTextSymbol = New TextSymbol

Dim pCallout As ICallout
pCallout = New BalloonCallout
pTextSymbol.Background = pCallout
pTextSymbol.Color = pColor
pTextSymbol.Font = pFontDisp
pCallout.AnchorPoint = pPointAnchor

Dim pMargin As ITextMargins
pMargin = pCallout
With pMargin
.LeftMargin = 1
.RightMargin = 1
.BottomMargin = 1
.TopMargin = 1
End With

pTextElement.Symbol = pTextSymbol


Dim pGraphicsContainer As IGraphicsContainer

pGraphicsContainer = pMxDoc.ActiveView
'pGraphicsContainer.DeleteAllElements()

pGraphicsContainer.AddElement(pElement, 0)


pElement.Activate(pMxDoc.ActiveView.ScreenDisplay)
pMxDoc.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, Nothing, Nothing)
End Sub
0 Kudos