IronPython

Spotfire IronPython: Accessing Column Values in Script Context

Posted on Updated on

Below is a code snippet to pull data from a data table into Script context. The data from a particular column(s) could be used to perform validations or compared against values from other data tables.

DataTable_To_ScriptContext

IronPython code:

from Spotfire.Dxp.Data import *

tableName='SuperStore_Sample'
columnToFetch='Order Date'
activeTable=Document.Data.Tables[tableName]
rowCount = activeTable.RowCount
rowsToInclude = IndexSet(rowCount,True)
cursor1 = DataValueCursor.CreateFormatted(activeTable.Columns[columnToFetch])
ctr1 = 0
for row in activeTable.GetRows(rowsToInclude,cursor1):
rowIndex = row.Index
val1 = cursor1.CurrentValue
ctr1 = ctr1 + 1
if (ctr1 == 5):
break

Further, we could push the data into an array for temporary storage and use as per requirement.

Advertisements

Spotfire: Show/Hide Legend and Switch View Type

Posted on Updated on

In some cases (for ex. Trellis view), to get more visualization space, we could simply hide the legend.

For this, on change of View Type (using a drop down property control – ViewType), I am invoking a script to show or hide the legend.

Below, creating the property control ViewType:

ShowHide_Legend_4

Script executed on change of drop down value: ShowHide_Legend_3

IronPython Code:

from Spotfire.Dxp.Application import Visuals
from Spotfire.Dxp.Application.Visuals import LineChart
from Spotfire.Dxp.Application.Visuals import Legend
for vis in Application.Document.ActivePageReference.Visuals:
if vis.Title == "Sales - Trellis View":
vis.As[LineChart]().Legend.Visible = False
if vis.Title == "Sales - Full View":
vis.As[LineChart]().Legend.Visible = True

In Full View, Color by legend is shown. ShowHide_Legend_2

Disabling the legend in Trellis view.ShowHide_Legend_1

Analyses (dxp) for reference from TibCommunity

Getting column values from filtered data selection

Posted on Updated on

Reference snippet for fetching the values from a particular column after filtering is applied to the data table.

import Spotfire.Dxp.Data.DataTable
from Spotfire.Dxp.Data import *
from Spotfire.Dxp.Data import DataValueCursor

dataTable = Document.ActiveDataTableReference
rows = Document.ActiveFilteringSelectionReference.GetSelection(dataTable).AsIndexSet()

for row in rows:
for column in dataTable.Columns:
if (column.Name == "colName"):
print (column.RowValues.GetFormattedValue(row))

Spotfire IronPython: Reset all filters

Posted on Updated on

IronPython Code:

import Spotfire.Dxp.Application.Filters as filters

for scheme in Document.FilteringSchemes:
scheme.ResetAllFilters()

Spotfire IronPython: Reset all visible filters

Posted on Updated on

IronPython Code to reset all applied visible filters:

import Spotfire.Dxp.Application.Filters as filters
from Spotfire.Dxp.Application.Filters import *
# Navigate through each page in the analyses
for eachPage in Document.Pages:
# Get the active data table in the current page
activeTable = eachPage.ActiveDataTableReference
# Get the set of all table groups in the current page
tableGroup = eachPage.FilterPanel.TableGroups
# Navigate through all the table groups in the filter panel of current page
for t in tableGroup:
# To filter on the active data table used in the filter panel
if (t.Name == activeTable.Name):
# In table group (data table), navigate through the filter groups
for filterSubGroup in t.SubGroups:
# Navigate through the filters in each filter group
for filterHandle in filterSubGroup.FilterHandles:
filterReference = filterHandle.FilterReference
# Reset if the filter is visible
if filterHandle.Visible==True:
filterReference.Reset()
print "Reset done for: " + eachPage.Title + '.' + t.Name + '.' + filterSubGroup.Name + '.' + filterReference.Name
if filterHandle.Visible==False:
print "Reset not done for: " + eachPage.Title + '.' + t.Name + '.' + filterSubGroup.Name + '.' + filterReference.Name

Please take note that this script can be used to not reset the hidden filters in the analyses. That is, this will reset all the visible filters which are part of filter groups across all the pages in the analyses.

Snapshot for indentation reference:

Spotfire_IronPython_Reset_All_Visible_Filters