Create TablePlot and Add columns

#######################################
# Creates or updates a tablePlot with #
# columns selected from a multiselect #
# doc prop control                    #
# Author: Jose Leviaguirre  Aug 2015  #
#######################################

from Spotfire.Dxp.Application.Visuals import TablePlot, VisualContent
from Spotfire.Dxp.Data import DataPropertyClass

#Script Parameters/Settings
multiSelectColumnsPropertyControl = "selectedTablePlotColumns" #multiselect doc prop control
#dataTable = Document.Data.Tables["yourTable"] #reference to a specific data table
dataTable = Document.Data.Tables.DefaultTableReference #reference to the default data table

# 1 Create a table if not there already
tablePlotVisual = None
updateTablePlot = False
defaultTableTitle=dataTable.Name

# 1.1 check if tablePlot already exist (based on name)
for v in Document.ActivePageReference.Visuals: 
   tablePlotVisual = v
   updateTablePlot = v.Title == defaultTableTitle
   if v.Title == defaultTableTitle: break

# 2.1 If no tablePlot found, create a new one
if not updateTablePlot:
tablePlotVisual = Application.Document.ActivePageReference.Visuals.AddNew[TablePlot]()
tablePlotVisual.Data.DataTableReference = dataTable
tablePlotVisual.Title = "${DataTable.DisplayName}"
tablePlotVisual.Legend.Visible= False
else:
tablePlotVisual=tablePlotVisual.As[VisualContent]()


# 2 Adds columns to a Data Table based on a document property. 
# The doc prop is a comma separated list of columns for the data table to show.
# use a multiline doc prop control to create a csv list of cols

# 2.1 Remove all columns
cols = tablePlotVisual.Data.DataTableReference.Columns
tablePlotVisual.TableColumns.Clear()

# 2.2 get document property
selection = Document.Data.Properties.GetProperty(DataPropertyClass.Document, multiSelectColumnsPropertyControl).Value

# 2.3 Parse columns from selection and add to tablePlotVisual
for property in selection:
   for col in property.split(","): tablePlotVisual.TableColumns.Add(cols[str(col)])



No comments: