Business Intelligence Tools / Developers Corner

IronPython in Spotfire — Turning lines & curves on and off

  • Would you like to be able to turn lines and curves on and off with the click of a button?
  • Have you tried to implement IronPython code you found on blog posts but failed?

My IronPython skills are still a work in progress, and I struggle with it just as much as the average user.  However, learning new things each and every day is one of the most fun and rewarding parts of my work.  Some months/years ago, I stumbled upon two sets of code written to show or hide lines/curves.  I knew they would be useful, so I saved them in a Word document.   At least some of what I saved was generated from this Community post (thanks to Sean Riley) —  https://community.tibco.com/questions/python-turn-lines-curves-and.

When I attempted to implement the code in my own files, I ran into issues with the code not producing the desired result, so I set out to figure out what was holding it back.  It turns out that the way the code is structured imposes limitations in terms of how many lines you can have on a visualization, whether the page can contain multiple visualizations, and how many lines you want to be able to turn on and off at a time.  This is one of the reasons why users struggle to implement code they find online.  In the end, I learned enough to produce a third set of code that marries the strengths of the first two and performs in any scenario.  The learning experience is worth sharing.

Thus, this week, I will use this v1.1 Show Hide Lines DXP file  to present 3 sets of IronPython code that turn lines and curves on and off using different code structure.  I will present the code and then describe what the code is doing, the advantages of the way it’s written, and then the limitations of the way it’s written.  Again, I’m still learning IronPython so if you see something that could be improved, please leave a comment.

Code Set 1

from Spotfire.Dxp.Application.Visuals import LineChart

for fm in vis.As[LineChart]().FittingModels:

if vis.As[LineChart]().FittingModels[0].Enabled == True:

vis.As[LineChart]().FittingModels[0].Enabled = False

else:

vis.As[LineChart]().FittingModels[0].Enabled = True

 

Description: This particular piece of code is referencing FittingModels, which refers to any Line or Curve.  It will turn any line/curve on and off, as long as there is only one line/curve.  It requires the creation of a parameter called “vis”, which is then connected to a single visualization.

Advantages: Because the ‘vis’ parameter connects to a specific visualization, the user may have other line charts with lines and curves on the same page, and the button will still work.  Also, if the line/curve is changed, say from a straight line fit to an average line, the button will still work without modification.

Restrictions: With this code, the user may have only one line/curve on a chart for it to work, and that means only one line/curve setup, not just one line/curve checked in the properties dialog. I added a second line/curve to this chart, and it quit working, even if I unchecked the straight line fit.  The second line/curve had to be completely deleted to get the code working again.

Code Set 2

from Spotfire.Dxp.Application.Visuals import *

for visual in Document.ActivePageReference.Visuals:

if visual.TypeId == VisualTypeIdentifiers.LineChart:

lc = visual.As[LineChart]()

for fm in lc.FittingModels:

if fm.TypeId.DisplayName == “Straight Line Fit”:

if fm.Line.DisplayName == “My line”:

fm.Enabled=not(fm.Enabled)

for fm in lc.FittingModels:

if fm.TypeId.DisplayName == “Line from Column Values”:

if fm.Line.DisplayName == “Type Curve A”:

fm.Enabled=not(fm.Enabled)

Description:  This piece of code works by referencing the type of line/curve and the line/curve name.  Note, there is no connection to the specific line chart.

Advantages: This code will work if there are multiple lines/curves on a visualization.  The sample visualization has three lines/curves, and the code has been setup to turn off two of them.  To turn off all lines, copy and paste lines 3-5, and adjust the line/curve name and line/curve type.

Restrictions: This code will not work with more than one line chart on the page.  Additionally, the code must ‘import *’.  It will not work with ‘import LineChart’.  If anyone can tell me why (in comments), it would be greatly appreciated.  I decided not to dive into that rabbit hole.

 

Code Set 3

from Spotfire.Dxp.Application.Visuals import *

for fm in vis.As[LineChart]().FittingModels:

if vis.TypeId == VisualTypeIdentifiers.LineChart:

lc = vis.As[LineChart]()

for fm in lc.FittingModels:

if fm.TypeId.DisplayName == “Line from Column Values”:

if fm.Line.DisplayName == “Type Curve A”:

fm.Enabled=not(fm.Enabled)

#for fm in vis.As[LineChart]().FittingModels:

#if vis.As[LineChart]().FittingModels[0].Enabled == True:

#vis.As[LineChart]().FittingModels[0].Enabled = False

#else:

#vis.As[LineChart]().FittingModels[0].Enabled = True

Description: This code merges the work of the previous two pieces of code.  It uses a parameter named ‘vis’ that connects to the ‘Visualization Connected to a Button’ visualization.  It specifies both the type of line/curve and the name of the line/curve.

Advantages: This code will work with multiple lines on a visualization, and it will work if there is another line chart on the page, which makes it an improvement over the other two pieces of code.

Restrictions:  This code may have other restrictions, but I haven’t run into them yet.

This post covered the syntax for a few different types of lines/curves, but it did not cover all of them.  You can find the proper API for all lines and curves at this TIBCO website.  (insert link)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

9 thoughts on “IronPython in Spotfire — Turning lines & curves on and off

  1. Rax Reply

    Informative article. I am a Spotfire developer and this gave me more information regarding the ironPython.

  2. Mya Reply

    Hi Julie,
    This article is very helpful. I’d like to apply this same feature to a ‘horizontal line’ and a ‘line from data table’ in my Lines & Curves dialog box, however this code doesn’t work for these types of lines. Have you encountered this problem before? Thank you.

    • Julie Schellberg of Big Mountain Analytics, LLC Post authorReply

      It should work for any line out of Lines & Curves, as long as you change the piece of the code that references the line type. Which of the examples are you using?

    • Julie Schellberg of Big Mountain Analytics, LLC Post authorReply

      Did you make sure to change the chart type for the chart you are using. I was just using this code again and I got errors because the code I show is for a Line Chart, and I was trying to apply it to a Bar Chart.

  3. Chelsea Reply

    Thanks, this is very helpful. I was easily able to get the Straight Line fit to toggle on and off, but I can’t get it to work for Horizontal Lines. My Lines & Curves shows this: “Horizontal Line: Log” where yours was using the straight line fit example of “Straight Line Fit: Straight line”. I’m wondering if plain vertical and horizontal lines are not the same syntax. I tried researching to no avail. For reference, I added a horizontal line from a data table to plot log scale minor lines on my chart.

    Now about the script:

    I set my parameter as vis.

    —My script—

    from Spotfire.Dxp.Application.Visuals import *

    #loop through all line charts and assign them as “lc”
    #must assign a script parameter for vis
    for fm in vis.As[LineChart]().FittingModels:
    if vis.TypeId == VisualTypeIdentifiers.LineChart:
    lc = vis.As[LineChart]()

    #loop through lines and curves and find the line called Horizontal Line: Log and toggle it
    for fm in lc.FittingModels:
    if fm.TypeId.DisplayName == “Horizontal Line”:
    if fm.Line.DisplayName == “Log”:
    fm.Enabled=not(fm.Enabled)

  4. Julie Reply

    Apologies for taking so long to get a solution. The solution is Reference Line. Replace “Horizontal Line” with “Reference Line”. That’ll do it.

  5. Chelsea Reply

    Thank you, I am so excited to try this. This will take my Spotfire analysis to the next level. I knew it would be a word or two difference, but couldn’t figure out where.

  6. Belinda Reply

    Hi Julie,

    A very informative article. I’ve found many articles on the topic of toggling on/off lines in visualisations, however they all start and end with the code. As a relative newby to spotfire, I wonder if you could take a step back and describe exactly what I need to do with his code (where do I enter it)?

Leave a Reply

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