Labels

Replace datatable with Information Link


from System import Array,Guid,String,Object
from Spotfire.Dxp.Data.Import import InformationLinkDataSource, InformationLinkParameter


ilDataSource = InformationLinkDataSource(myGUID)

Document.Data.Tables[myDataTable].ReplaceData(ilDataSource)

Input
myGUID: GUID of the Information Link to use for the replacement
myDataTable: Datatable to replace

Set document properties from marked records


from Spotfire.Dxp.Data import DataValueCursor

myLatCursor = DataValueCursor.CreateFormatted(Document.Data.Tables["mydatatable"].Columns["latitude"])
myLongCursor = DataValueCursor.CreateFormatted(Document.Data.Tables["mydatatable"].Columns["longitude"])

markedRows = Document.Data.Markings["Marking"].GetSelection(Document.Data.Tables["mydatatable"]).AsIndexSet()
for row in Document.Data.Tables["mydatatable"].GetRows(markedRows, myLatCursor):
 Document.Properties['refLat'] = myLatCursor.CurrentValue

for row in Document.Data.Tables["mydatatable"].GetRows(markedRows, myLongCursor):
 Document.Properties['refLong'] = myLongCursor.CurrentValue

This script can be used to set 1 or more document properties from a marked record. If more than one are marked, it uses the last one in the stack. The example above sets the "refLat" and "refLong" properties from the "mydatatable" datatable, which has Latitude and Longitude columns. This script was used in the "Visualizing the radius around a point on a map" article as seen on the Tips & Tricks blog.

Export a cross table visualization to a file


from System.IO import Path, File, StreamWriter
from Spotfire.Dxp.Application.Visuals import CrossTablePlot
 
#Temp file for storing the cross table data
tempFolder = Path.GetTempPath()
tempFilename = Path.GetTempFileName()

#Export CrossTable data to the temp file
writer = StreamWriter(tempFilename)
vTable.As[CrossTablePlot]().ExportText(writer)

print tempFilename

Input 
vTable (Visualization)

Not suitable for use over the web since the local file system is used.

Change "Limit Data Using Filtering" message

This script will change the message that is displayed when "limit data using filtering" is used and the "message on white background" is selected.

from Spotfire.Dxp.Application.Visuals import Visualization

visual = argV.As[Visualization]();

visual.Data.LimitingMarkingsEmptyMessage = "New Description"

Input

argV (Visualization): The visualization that has been configured to show "message on white background"


Toggle visualization type

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

if viz.TypeId == VisualTypeIdentifiers.LineChart:
 viz.TypeId = VisualTypeIdentifiers.BarChart 
elif viz.TypeId == VisualTypeIdentifiers.BarChart:
 viz.TypeId = VisualTypeIdentifiers.LineChart

Input:
viz (Visualization): The visualization that you want to toggle between a different visual type.

This script will toggle a visualization between a Line Chart and a Bar Chart.  Other visualization types can also be used.

Attribution: Chris S. from TIBCOmmunity

Tag Marked Rows

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from Spotfire.Dxp.Data import DataColumn, TagsColumn
from Spotfire.Dxp.Data import DataPropertyClass, DataType, DataValueCursor, IDataColumn, IndexSet
from Spotfire.Dxp.Data import RowSelection 
 
# Tag the marked rows
markedRowSelection = Document.ActiveMarkingSelectionReference.GetSelection(Document.ActiveDataTableReference)
table = Document.ActiveDataTableReference
myTagColumn = table.Columns.Item["Decision"].As[TagsColumn]()

selectRows = IndexSet(table.RowCount, True)
myTagColumn.Tag("No", RowSelection(selectRows))
myTagColumn.Tag("Yes", markedRowSelection )

Mark rows

1
2
3
4
5
6
7
from Spotfire.Dxp.Application.Visuals import TablePlot

targetViz = markingViz.As[TablePlot]();

rowSelection = targetViz.Data.DataTableReference.Select(selectStatement);

targetViz.Data.MarkingReference.SetSelection(rowSelection, targetViz.Data.DataTableReference);

Input:
markingViz (Table): A visualization that current has the marking profile applied.
selectStatement (String): A query-like string that indicates what records should me marked.  For example:

columnname > 2

This script can be modified to work with any visualization type.


Sort a table visualization

1
2
3
4
5
6
from Spotfire.Dxp.Application.Visuals import TablePlot, TablePlotColumnSortMode

toTableSort = vTable.As[TablePlot]();
todataColumn = toTableSort.Data.DataTableReference.Columns[sortColumn];
toTableSort.SortInfos.Clear();
toTableSort.SortInfos.Add(todataColumn, TablePlotColumnSortMode.Ascending);

Input:
sortColumn (String): The column name of the column to sort
vTable (Table): The target table visualization


Export a table visualization to a file


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from System.IO import Path, File, StreamWriter
from Spotfire.Dxp.Application.Visuals import TablePlot
 
#Temp file for storing the table data
tempFolder = Path.GetTempPath()
tempFilename = Path.GetTempFileName()

#Export table data to the temp file
writer = StreamWriter(tempFilename)
vTable.As[TablePlot]().ExportText(writer)

print tempFilename

Input
vTable (Visualization)

Not suitable for use over the web since the local file system is used.

Change filter panel search criteria

1
2
3
4
5
page = Application.Document.ActivePageReference

filterPanel = page.FilterPanel

filterPanel.InteractiveSearchPattern = "status:modified"

This script is used to set the search criteria in the filter panel.  This can be used to selectively display only certain filters.  It's possible to search the filters based on the column properties, for example:

datatype:string
externalname:ag*
columntype:imported


Show current filter settings


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from Spotfire.Dxp.Application.Visuals import VisualContent
from System import Guid

page = Application.Document.ActivePageReference

#Get the filtering scheme used on this page
filteringSchemeRef = page.FilterPanel.FilteringSchemeReference

#Get the name of the filtering scheme on the active page
for filteringScheme in Document.FilteringSchemes:
 filteringSelection = filteringScheme.FilteringSelectionReference
 if filteringScheme == filteringSchemeRef: 
  filteringSchemeName = filteringSelection.Name

content="<STRONG>Filtering Scheme:</STRONG> " + filteringSchemeName

filterPanel = page.FilterPanel
filterPanel.InteractiveSearchPattern = "status:m"

for filters in filterPanel.FiltersMatchingSearchPattern:
 print filters.FilterReference.ToString()

for tableGroup in filterPanel.TableGroups :
 print tableGroup.ToString()
 tableGroup.Expanded=True
 for filterGroup in tableGroup.SubGroups :
  print filterGroup.ToString()
  print filterGroup.Expanded
  filterGroup.Visible=False
  if (filterGroup.Expanded == False):
      filterGroup.Expanded = True
  else:
   filterGroup.Expanded = False

for tableGroup in filterPanel.TableGroups:
 isMod=0
 str="<p>"
 if filterPanel.TableGroups.Count > 1:
  str+="<b>"+tableGroup.FilterCollectionReference.DataTableReference.Name+":</b><br>"
 for fh in tableGroup.FilterHandles:
  if fh.FilterReference.Modified:
   isMod=-1
   str+=fh.FilterReference.ToString()
   str+="<br>"
 for subGroup in tableGroup.SubGroups:
  for fh in subGroup.FilterHandles:
   if fh.FilterReference.Modified:
    isMod=-1
    str+=fh.FilterReference.ToString()
    str+="<br>"
 str+="</p>"
 if isMod==-1:
  content+=str  

ta=vTextArea.As[VisualContent]()
if(ta.HtmlContent is None):
 ta.HtmlContent = " "

if (ta.HtmlContent.find("<SPAN id=fs>")==-1):
  ta.HtmlContent=="<SPAN id=fs>"+ta.HtmlContent
final=ta.HtmlContent.split("<SPAN id=fs>")[0]+"<SPAN id=fs>"+content+"</SPAN>"
ta.HtmlContent=final

#Reset filter panel search and navigate to the Filter Settings Page
filterPanel.InteractiveSearchPattern = ""

Input:
vTextArea (Text Area): The text area that will be modified to show the current filter settings.

This script will list the modified filters for the current page, and how they have been modified.  The output will be generated in HTML format and inserted into the specified text area.  Here is a sample of the output:



Here is a similar variation that also is compatible with in-database connections:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from Spotfire.Dxp.Application.Visuals import VisualContent
from System import Guid

page = Application.Document.ActivePageReference


#Get the filtering scheme used on this page
filteringSchemeRef = page.FilterPanel.FilteringSchemeReference

filterPanel = page.FilterPanel

#Now set the filtering scheme to that of the originating page
filterPanel.FilteringSchemeReference = filteringSchemeRef

for filteringScheme in Document.FilteringSchemes:
 filteringSelection = filteringScheme.FilteringSelectionReference
 if filteringScheme == filteringSchemeRef: 
  filteringSchemeName = filteringSelection.Name

content="<STRONG>Filtering Scheme:</STRONG> " + filteringSchemeName + "<BR><BR>"

filterPanel.InteractiveSearchPattern = "status:m"
for filters in filterPanel.FiltersMatchingSearchPattern:
 content += filters.FilterReference.ToString() + "<BR>"


ta=vTextArea.As[VisualContent]()
if(ta.HtmlContent is None):
 ta.HtmlContent = " "

if (ta.HtmlContent.find("<SPAN id=fs>")==-1):
  ta.HtmlContent=="<SPAN id=fs>"+ta.HtmlContent
final=ta.HtmlContent.split("<SPAN id=fs>")[0]+"<SPAN id=fs>"+content+"</SPAN>"
ta.HtmlContent=final

filterPanel.InteractiveSearchPattern = ""

Change the page

1
Document.ActivePageReference = Document.Pages[2]

Page index starts at 0.

Change the URL of the Collaboration Panel

1
2
3
4
5
6
7
8
9
import clr
clr.AddReference ( "System" )
import System
from System import Uri
import Spotfire.Dxp.Application

for panel in Application.Document.ActivePageReference.Panels:
 if panel.Title == "Collaboration":
  panel.Url = Uri ( "http://www.google.com" )

This script will change the URL of the Collaboration Panel for the active page.

Perform operations on the columns in a Graphical Table

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from Spotfire.Dxp.Application.Visuals.Miniatures import GraphicalTable
from Spotfire.Dxp.Application.Visuals import VisualTypeIdentifiers

for col in gtable.As[GraphicalTable]().Columns: 
 #Calculated Value Columns
 if col.Visualization.TypeId == VisualTypeIdentifiers.CalculatedValueMiniatureVisualization:
  try:
   vExp = col.Visualization.ValueAxis.Expression
  except:
   0

 #Icon Columns
 if col.Visualization.TypeId == VisualTypeIdentifiers.IconMiniatureVisualization:
  try:   
   vExp = col.Visualization.IconAxis.Expression
  except:
   0

 #Sparkline Columns
 if col.Visualization.TypeId == VisualTypeIdentifiers.SparklineMiniatureVisualization:
  try:   
   vExp = col.Visualization.YAxis.Expression
  except:
   0

 #Bullet Graph Columns
 if col.Visualization.TypeId == VisualTypeIdentifiers.BulletGraphMiniatureVisualization:
  try:   
   vExp = col.Visualization.ValueAxis.Expression
  except:
   0

 try:
  col.Title = vExp
 except:
  0

Input:
gtable (Graphical Table)

This script will loop over the columns in a Graphical Table, find the expression used, and set the column title to display the expression.


Loop through all visualizations on a page

1
2
for p in page.Visuals:
 print p.Title

Input: 
page (Page)

Toggle the details-on-demand panel on\off

1
2
3
4
if (Document.ActivePageReference.DetailsOnDemandPanel.Visible == False):
   Document.ActivePageReference.DetailsOnDemandPanel.Visible = True
else:
   Document.ActivePageReference.DetailsOnDemandPanel.Visible = False

Toggle the filter panel on\off

1
2
3
4
if (Document.ActivePageReference.FilterPanel.Visible == False):
   Document.ActivePageReference.FilterPanel.Visible = True
else:
   Document.ActivePageReference.FilterPanel.Visible = False

Change the datatable of a table visualization

1
2
3
4
5
6
7
from Spotfire.Dxp.Application.Visuals import VisualContent

tbl = currentTableVisualization.As[VisualContent]()
newtbl = Document.Data.Tables.Item[newDataTableName]
tbl.Data.DataTableReference=newtbl
tbl.AutoConfigure()
tbl.ApplyUserPreferences()

Input:
currentTableVisualization (Visualization): The table visualization that will have its datatable changed.
newDataTableName (String): The name of the new datatable.


Clear marking (unmark)

1
2
3
4
5
6
7
from Spotfire.Dxp.Data import DataManager 
from Spotfire.Dxp.Data import IndexSet 
from Spotfire.Dxp.Data import RowSelection 

marking=Application.GetService[DataManager]().Markings[markingName]
selectRows = IndexSet(dataTable.RowCount, False)
marking.SetSelection(RowSelection(selectRows),dataTable)

Input:
markingName (String): The name of the marking profile to clear.
dataTable (Data Table): The data table that currently has the marking applied.

Refresh a stale data table

1
2
if table.IsRefreshable and table.NeedsRefresh:
   table.Refresh()

Input: table (Data Table)

This script is equivalent to pressing the red "refresh" icon that is shown when the data table is stale.

Get the current user's username

1
2
from System.Threading import Thread 
print Thread.CurrentPrincipal.Identity.Name 

Create a document property and set its value

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from Spotfire.Dxp.Data import DataProperty 
from Spotfire.Dxp.Data import DataType 
from Spotfire.Dxp.Data import DataPropertyClass 

propName  = "myDocumentProperty2" 
attr   = DataProperty.DefaultAttributes 
prop   = DataProperty.CreateCustomPrototype(propName, DataType.String, attr) 

Document.Data.Properties.AddProperty(DataPropertyClass.Document, prop) 

prop.Value  = "myDocumentPropertyValue"

Loop through all pages

1
2
for page in Application.Document.Pages:
 print page.Title

Set a document property

1
Document.Properties['property name'] = 'hello'