• 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)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Written by Julie Schellberg of Big Mountain Analytics, LLC
Residing in Whitefish, MT, an analytics partner with Ruths.ai. Specializing in Spotfire analytics, dabbling in Power BI and R.