IronPython to Turn Labels On and Off a Visualization

  • Would you like to turn labels on and off of a visualization without going into the properties menu?
  • Would you like to learn more about IronPython?

A user contacted me today and asked how they could turn labels on and off without entering the properties dialog.  They wanted to use a text area control to quickly turn labels on and off in a live demo.  I knew IronPython could do the trick, although, I wasn’t quite sure about the code.  A quick TIBCO Community search pulled up this link, and I was able to modify the code shown to meet my user’s needs.

In this post, I’ll show you how to attach IronPython to a property control that toggles labels on and off a scatter plot.  Then, I’ll explain how to modify the code for other types of visualizations.

Detailed Steps

  1. Add a text area and toggle it into Edit mode.
  2. Click the button in the toolbar to add an input property control (data type: string) to the text area called “ToggleLables”.  The case is important.
  3. While still in the Property Control dialog, click the Script button.
  4. Change the radio button to ‘Execute the script selected’.
  5. Click the New button.
  6. Name the script.
  7. Copy and paste the script below.  It will look like this:
  8. Click the Add button to create a parameter called varViz and map it to your visualization.
  9. Next, click Run Script to make sure there are no errors.
  10. Click OK and exit out of the dialogs.

Code Snippet

Copy and paste the script below, but use the screenshot above to mimick the indents.

from Spotfire.Dxp.Application.Visuals import *
from Spotfire.Dxp.Application.Visuals import LabelVisibility
visuals = Document.ActivePageReference.Visuals
varProp =Document.Properties[“ToggleLabels”]

for vis in visuals:
if vis == varViz:
scatterPlot = vis.As[ScatterPlot]()
#print scatterPlot.LabelVisibility

if varProp == “N”:
scatterPlot.LabelVisibility = LabelVisibility.None
if varProp == “Y”:
scatterPlot.LabelVisibility = LabelVisibility.All

With the code provided, you should be able to enter “Y” or “N” into the input property control to turn the labels on and off.

Modification for Other Visualization Types

Now, this particular code example runs with a scatter plot.  However, it’s easy to modify for any visualization type.  Just lookup up the namespace here.  Most are intuitive — ScatterPlot, BarChart, LineChart, etc.  Now, you have a control that will allow you to more easily turn labels on and off.

Spotfire Version

Spotfire 7.12

Feeback

Lastly, I am always looking to improve the content delivered to users and readers.  Please provide feedback on my content, instructions, and explanations in the comments section.  Thanks!  (Julie)

Guest Spotfire blogger residing in Whitefish, MT.  Working for SM Energy’s Advanced Analytics and Emerging Technology team!

5 thoughts on “IronPython to Turn Labels On and Off a Visualization

  1. Thanks for this great dedication Julie, I am wondering if there is a way that we can use a specific column as a label column other than the values itself. Say, we are plotting pressure values as Y-axis but we want to show the Depth as a label while we still have the Y-axis representing the Pressure value.

    1. Yes you can do that by simply selecting that column as the Label by column. I assume you are using a scatter plot. Maybe I am not understanding the question. Did you run into a limitation with that code?

Leave a Comment

Your email address will not be published. Required fields are marked *