03/12/2015

[TIBCO Spotfire] Export data table as CSV via IronPython

Here's a sample script to programmatically via IronPython export a data table as CSV

from Spotfire.Dxp.Data.Export import DataWriterTypeIdentifiers
from System.IO import File


writer = Document.Data.CreateDataWriter(DataWriterTypeIdentifiers.ExcelXlsDataWriter)
table = Document.ActiveDataTableReference #OR pass the DataTable as parameter
filtered = Document.ActiveFilteringSelectionReference.GetSelection(table).AsIndexSet() #OR pass the filter
stream = File.OpenWrite("PATH/NAME.csv")
names = []
for col in table.Columns:
    names.append(col.Name)
writer.Write(stream, table, filtered, names)
stream.Close()





Note: the path you pass to the File.OpenWrite function is relative to the machine running the analysis; this means that on the Web Player, unless you find a way to stream the data back to the user browser, the end user will never receive the file

[TIBCO Spotfire] Mark rows via IronPython

Here's a sample script to programmatically via IronPython mark rows with a particular value (set GRAPH as script parameter pointing to the visualization you want to work on):

from Spotfire.Dxp.Data import DataPropertyClass
from Spotfire.Dxp.Application.Visuals import VisualContent
from Spotfire.Dxp.Data import IndexSet
from Spotfire.Dxp.Data import RowSelection
from Spotfire.Dxp.Data import DataValueCursor
from System import String

# get object reference
vc = GRAPH.As [VisualContent] ()
dataTable = vc.Data.DataTableReference

# get marking
marking = vc.Data.MarkingReference

rowCount = dataTable.RowCount
rowsToInclude = IndexSet (rowCount, True)
rowsToSelect = IndexSet (rowCount, False)

cursor1 = DataValueCursor.CreateFormatted (dataTable.Columns ["COLUMN_NAME"])

#Find Records by looping through all rows
idx = 0
for row in dataTable.GetRows (rowsToInclude, cursor1):
    found = False
    aTag = cursor1.CurrentValue
    print aTag
# if there’s a match, mark it
    if aTag == VALUE:
        rowsToSelect [idx] = True
        print idx
    idx = idx + 1

#Set Marking
marking.SetSelection (RowSelection (rowsToSelect), dataTable)

[TIBCO Spotfire] Filter handling via IronPython - set value

Here is a sample script to programatically via IronPython set a value for a filter in Spotfire; if the value does not exist in the allowed filter values, set it to the maximum possible

import Spotfire.Dxp.Application.Filters as filters
import Spotfire.Dxp.Application.Filters.ListBoxFilter
from Spotfire.Dxp.Application.Filters import FilterTypeIdentifiers

#use following line if the filter is to be applied to the currently active page - CAUTION, as we might alter the filter for a different filtering scheme then!
myPanel = Document.ActivePageReference.FilterPanel
# alternatively use following line to se it for a specific page
#myPanel = myPage.FilterPanel
#where myPage is a script parameter that points to the page we want it to work on
myFilter = myPanel.TableGroups[0].GetFilter("FILTER_NAME")
lbFilter = myFilter.FilterReference.As[filters.ItemFilter]()
if VALUE in lbFilter.Values:
    lbFilter.Value = VALUE
else:
    lbFilter.Value = max(lbFilter.Values)

[TIBCO Spotfire] Cumulative sum

Here's a simple formula to plot the cumulative sum (trend) of data in Spotfire

Sum([COLUMN]) OVER (intersect(AllPrevious([Axis.Rows]),[GROUP_BY_1],...,[GROUP_BY_N])) as [ALIAS]

Just put it on the X-axis and you're set. The grouping is optional