1
votes

I have a code, like this for an Action Control button:

from Spotfire.Dxp.Application.Visuals import *
from Spotfire.Dxp.Data import *

if (vis.TypeId == VisualTypeIdentifiers.BarChart):
    vis.TypeId = VisualTypeIdentifiers.LineChart
    vis.XAxis.Expression = "City"

elif (vis.TypeId == VisualTypeIdentifiers.LineChart):
    vis.TypeId = VisualTypeIdentifiers.BarChart
    vis.XAxis.Expression = "Country"

For some reason, this gives me the error:

'Visual' object has no attribute 'XAxis'

I have also tried casting it like:

vis = vis.As[VisualContent]()

and to encase the Expression like ie.:

vis.XAxis.Expression = "[Country]"

But with no success, and similar error. When I remove the XAxis expression part, the code works successfully, and I can switch between Line and Bar chart with a button click. What I want however, is also to change the X-axis between these two states. Any ideas?

EDIT

Here is my updated script, which returns another error:


if (vis.TypeId == VisualTypeIdentifiers.BarChart):
    visual = vis.As[LineChart]()
    #visual.TypeId = VisualTypeIdentifiers.LineChart
    visual.XAxis.Expression = "[City]"

elif (vis.TypeId == VisualTypeIdentifiers.LineChart):
    visual = vis.As[BarChart]()
    #visual.TypeId = VisualTypeIdentifiers.BarChart
    visual.XAxis.Expression = "[Country]"

Error:

File "<string>", line 7, in <module>
AttributeError: 'NoneType' object has no attribute 'XAxis'

Again, the visualization is added as a Script parameter named "vis".

2
vis = vis.As[Visualization]() instead of vis = vis.As[VisualContent]() - Mark P.

2 Answers

1
votes

I managed to figure out an answer. Spotfire API is slightly messy in this regard, as the default object that you get from adding a visualization through the GUI it returns is a Visual object, which means you have to convert it to the Visualization Type you want before accessing Class fields like "XAxis" etc. that belong to that specific Class of visualization. This was confusing to me as a beginner to the Spotfire API, but it's an important distinction to make; you need to cast the visualization type from the Visual object to access fields. The Visual class can have it's TypeId altered, but there exists no field for XAxis, so we convert it to a BarChart or LineChart type, which has an XAxis field.

Visual Class fields: https://docs.tibco.com/pub/doc_remote/spotfire/7.12.0/doc/api/html/T_Spotfire_Dxp_Application_Visual.htm

Chart Class fields: https://docs.tibco.com/pub/doc_remote/spotfire/7.12.0/doc/api/html/T_Spotfire_Dxp_Application_Visuals_BarChart.htm

These are seperate chains of classes, and BarChart isn't a child class of Visual as one might have expected (I for one did).

if (vis.TypeId == VisualTypeIdentifiers.BarChart):
    vis.TypeId = VisualTypeIdentifiers.LineChart
    vis = vis.As[LineChart]()
    vis.XAxis.Expression = "[City]"

elif (vis.TypeId == VisualTypeIdentifiers.LineChart):
    vis.TypeId = VisualTypeIdentifiers.BarChart
    vis = vis.As[BarChart]()
    vis.XAxis.Expression = "[Country]"

If one does not cast the Visual object's TypeId in the start, it will be interpreted as a NoneType object.

0
votes

Here's a working example of some IronPython that I'm using to switch X-Axis expression. You may want to try something like "vis.As[LineChart]().XAxis.Expression" and "vis.As[BarChart]().XAxis.Expression".

from Spotfire.Dxp.Data import *
from Spotfire.Dxp.Application.Visuals import ScatterPlot

scaleType = Document.Properties["LookbackScaleValue"]

for page in Document.Pages:
    if page.Title == "Data Lookback":
        for visual in page.Visuals:
            if "Lookback" in visual.Title:
                if scaleType == "Categorical":
                    visual.As[ScatterPlot]().XAxis.Expression = '<BinByDateTime([TIMESTAMP],"Year.Month.DayOfMonth",2) NEST BinByDateTime([TIMESTAMP],"Hour",0) NEST BinByDateTime([TIMESTAMP],"Minute",0)>'
                elif scaleType == "Continuous":
                    visual.As[ScatterPlot]().XAxis.Expression = '[TIMESTAMP]'