Oliver Drummond
Oliver Drummond

Reputation: 530

How To Toggle Lines & Curves Names Visibility On Spotfire With Iron Python?

I've been trying to create an IronPython script to toggle my BarChart Horizontal Lines names with no luck.

I would like to achieve this with a button click: enter image description here

The code I am currently using is:

from System.Drawing import Color
from Spotfire.Dxp.Application.Visuals import *

# vis parameter referencing an existing BarChart visualization
vis = vis.As[BarChart]()

# Read the document property with the toggle value (true/false)
Document.Properties['GenericToggleLineNames'] = not Document.Properties['GenericToggleLineNames']

#Loop through all the Lines & Curves collection
if Document.Properties['GenericToggleLineNames']:
    for fm in vis.FittingModels:
        if fm.Line.DisplayName == 'Defined Underload Limit':
            fm.Line.CustomDisplayName = 'Defined Underload Limit'
        elif fm.Line.DisplayName == 'Defined Warning Limit':
            fm.Line.CustomDisplayName = 'Defined Warning Limit'
        elif fm.Line.DisplayName == 'Defined Critical Limit':
            fm.Line.CustomDisplayName = 'Defined Critical Limit'
else:
    for fm in vis.FittingModels:
        if fm.Line.DisplayName == 'Defined Underload Limit':
            fm.Line.CustomDisplayName = ''
        elif fm.Line.DisplayName == 'Defined Warning Limit':
            fm.Line.CustomDisplayName = ''
        elif fm.Line.DisplayName == 'Defined Critical Limit':
            fm.Line.CustomDisplayName = ''

But, when I get to the "Show = true", the code does not change the CustomDisplayNames.

According to the Spotfire API, DisplayName only offers a get method, while CustomDisplayName offers both get and set.

Does anyone know how to create this toggle?

Upvotes: 0

Views: 1951

Answers (3)

dogfrog
dogfrog

Reputation: 21

The annotations of the lines are are stored in the Line.Details. You can just loop through the details items and specify what to show.

# Imports
from System.Drawing import Color
from Spotfire.Dxp.Application.Visuals import *

#Add a vis parameter referencing an existing LineChart visualization
vis = vis.As[BarChart]()

#Loop through all the Lines & Curves collection
Document.Properties['GenericVisualisationDescriptions'] = not Document.Properties['GenericVisualisationDescriptions']

for fm in vis.FittingModels:
    labels_tooltips=fm.Line.Details
    if Document.Properties['GenericVisualisationDescriptions']:
        for lt in labels_tooltips:
            lt.ShowInLabel=True    #turns label on
            lt.ShowInTooltip=True  #turns tooltip on
    else:
        for lt in labels_tooltips:
            lt.ShowInLabel=False   #turns label off
            lt.ShowInTooltip=False #turns tooltip off

In case you you want to just show/hide a specific element you can distinguish by them by the Name or DisplayName. Just print them to identify which elements you need:

print lt.Name,lt.DisplayName,lt.ShowInLabel,lt.ShowInTooltip

Upvotes: 0

Oliver Drummond
Oliver Drummond

Reputation: 530

I've managed to get it working in a hideous way. Will share it here if anyone needs it, but please, would love to find a proper way to do it.

Even though Spotfire API documentation mentions that the ReferenceCurve.DisplayName is a read-only Property (only have the get method), it looks like it's being changed when CustomDisplayName is updated.

With that in mind, I've created another set of IFs, looking for the "new" DisplayName and replacing them with the old ones.

# Imports
from System.Drawing import Color
from Spotfire.Dxp.Application.Visuals import *

#Add a vis parameter referencing an existing LineChart visualization
vis = vis.As[BarChart]()

#Loop through all the Lines & Curves collection
Document.Properties['GenericVisualisationDescriptions'] = not Document.Properties['GenericVisualisationDescriptions']

if Document.Properties['GenericVisualisationDescriptions']:
    for fm in vis.FittingModels:
        if fm.Line.DisplayName == ' ':
            fm.Line.CustomDisplayName = 'Defined Underload Limit'
        elif fm.Line.DisplayName == '  ':
            fm.Line.CustomDisplayName = 'Defined Warning Limit'
        elif fm.Line.DisplayName == '   ':
            fm.Line.CustomDisplayName = 'Defined Critical Limit'
else:
    for fm in vis.FittingModels:
        print fm.Line.DisplayName
        print fm.Line.CustomDisplayName
        if fm.Line.DisplayName == 'Defined Underload Limit':
            fm.Line.CustomDisplayName = ' '
        elif fm.Line.DisplayName == 'Defined Warning Limit':
            fm.Line.CustomDisplayName = '  '
        elif fm.Line.DisplayName == 'Defined Critical Limit':
            fm.Line.CustomDisplayName = '   '

Upvotes: 0

BMAnalytics
BMAnalytics

Reputation: 124

I wrote a blog post on how to do this. Please go here -- https://datashoptalk.com/ironpython-in-spotfire-turning-lines-curves-on-and-off/. The code will vary depending on what you have on the page. Please upvote if you get to the right answer with this post.

Upvotes: 0

Related Questions