Displaying Cross Table data as a new Data Table


When using a Cross Table to display various measurements and calculations on your data, it is sometimes useful to be able to use the aggregated data elsewhere in your document.  This could be so you can use it in other visualizations, or so you can build custom reports with the data.  Using a Script Control, you can programmatically access this data and add it back into Spotfire as a new Data Table, which will then allow it to be used in other visualization types and other reports.

Assume we have the following Cross Table:

The following script will take in one parameter, which is a reference to the Cross Table above:

from System.IO import Path, File, StreamWriter
from Spotfire.Dxp.Data import DataTableSaveSettings
from Spotfire.Dxp.Application.Visuals import CrossTablePlot
from Spotfire.Dxp.Data.Import import TextFileDataSource
from Spotfire.Dxp.Data.Import import TextDataReaderSettings
from Spotfire.Dxp.Application.Visuals import TablePlot
from Spotfire.Dxp.Application.Visuals import VisualTypeIdentifiers

#Temp file for storing the cross table data
tempFolder = Path.GetTempPath()
tempFilename = Path.GetTempFileName()

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

#Add tempfile back into Spotfire using TextFileDataSource
readerSet = TextDataReaderSettings()
readerSet.Separator = "\t"
readerSet.AddColumnNameRow(0)
textDataSource = TextFileDataSource(tempFilename, readerSet )
myNewTable = Document.Data.Tables.Add("CrossTableData",textDataSource)

#If you want to change the data table to be embedded and not linked, use the following code
settings = DataTableSaveSettings (myNewTable,False, False);
Document.Data.SaveSettings.DataTableSettings.Add(settings);


When the script is executed a new Data table is added, which is displayed on the top of the image below: 

In next week's tip we will learn how to take this new Data table, or any Data table, and build a custom report, using colors and styles, and images, which will print out much nicer than the raw Data table would.