Introduction
Automating operations in Spotfire® with scripting is immensely powerful. It can be used for anything from providing shortcuts to frequent operations to building extremely capable enterprise grade analytic applications.
In the article we will see how you can enable end-users to change which columns to display in a table visualization. This may be useful when you need to use a single table to display different contents dependent on the user's current interest. There are two ways to change data table columns to display via property control.
Method 1: Selecting one or multiple columns to display on a table visualization
- Create a Text area and add a Property Control of type List box (multiple select)
-
In the Property Control setting:
- Add a new Document Property of type String. Let's name it "columnSelection".
- Set property value through "Column selection".
- Click Select Columns... button and choose columns to include.
- Now click Script... button and select "Execute the script selected below:" and click New.
- Add script parameter of type Visualization and select your data table. Let's name it "tablePlot".
- Paste-in the script below and click OK to close all settings.
- You can now select columns to display using the List box (multiple select) Property Control.
Script:
#get columnSelection document property from Spotfire.Dxp.Data import DataPropertyClass selection = Document.Data.Properties.GetProperty(DataPropertyClass.Document, "columnSelection").Value #apply column(s) selection to the table plot from Spotfire.Dxp.Application.Visuals import * tablePlot.As[TablePlot]().Columns.Clear() tablePlot.As[TablePlot]().Columns.AddRange(selection)
You can download the attached ColumnsSelection.dxp to see the property control in action.
Method 2: Selecting a collection of columns to display on a table visualization
- Create a Text area and add a Property Control of type List box or Drop-down list
-
In the Property Control setting:
- Add a new Document Property of type String. Let's name it "collectionSelection".
- Set property value through "Fixed value".
- Click Add button and add the columns collections names you would like to add.
- Now click Script... button and select "Execute the script selected below:" and click New.
- Add script parameter of type Visualization and select your data table. Let's name it "tablePlot".
- Paste-in the script below, edit it with relevant data, name it and click OK to close all settings.
- You can now select a collection of columns to display using Property Control.
Script:
# Copyright © 2017. TIBCO Software Inc. Licensed under TIBCO BSD-style license. from Spotfire.Dxp.Application.Visuals import TablePlot # Get a handle to the data table # tablePlot is the visualization parameter passed to the script to specify the table to work on. dataTable= tablePlot.As[TablePlot]().Data.DataTableReference # Get a handle to the table plot table = tablePlot.As[TablePlot]() # Get the ColumnCollection for the table plot columns = table.TableColumns if (Document.Properties["collectionSelection"] == "Financial" ): columns.Clear() # Add the relevant data table columns to the table plot ColumnCollection columns.Add(dataTable.Columns.Item["Goods exports US$"]) columns.Add(dataTable.Columns.Item["IncomeGroup"]) if (Document.Properties["collectionSelection"] == "Population" ): columns.Clear() # Add the relevant data table columns to the table plot ColumnCollection columns.Add(dataTable.Columns.Item["Population"]) columns.Add(dataTable.Columns.Item["Urban Population %"]) columns.Add(dataTable.Columns.Item["Life Expectancy at Birth (years)"]) if (Document.Properties["collectionSelection"] == "Other" ): columns.Clear() # Add the relevant data table columns to the table plot ColumnCollection columns.Add(dataTable.Columns.Item["Internet Subscribers"]) columns.Add(dataTable.Columns.Item["Power Consumption (kWh)"])
You can download the attached CollectionSelection.dxp to see the property control in action.
References
- API Reference: TablePlot class
- API Reference: TablePLot.TableColumns property
- API Reference: TableColumnCollection class
- API Reference: DataTable class
- API Reference: DataTable.Columns property
- API Reference: DataColumnCollection class
License: TIBCO BSD-Style License
columns_selection_-_example.dxp
columns_selection_-_script.txt
- 2
Recommended Comments
There are no comments to display.