Spotfire

Spotfire IronPython: Reset Markings

Posted on Updated on

Here is a code snippet for removing markings across visualizations/data tables within an analyses. This script will not reset the filters and only work on the marking. To illustrate this, I have constricted the date range for couple of date filters in the filter column.

Reset_Markings_1

I will now make some selections on the bar chart

Reset_Markings_2

To remove the markings without resetting the applied filters, we can execute the script through the button Reset Markings

Reset_Markings_3

Code snippet for reference: Reset_Markings_4

# Import required libraries
from Spotfire.Dxp.Data import *
from Spotfire.Dxp.Application.Filters import *

def resetMarking():
# Loop through each data table
for dataTable in Document.Data.Tables:
# Navigate through each marking in a given data table
for marking in Document.Data.Markings:
# Unmark the selection
rows = RowSelection(IndexSet(dataTable.RowCount, False))
marking.SetSelection(rows, dataTable)

# Call the function
resetMarking()

Below is one more use of the snippet applied for multiple visualizations which use cascading markings.

Reset_Markings_5

When the analysis contains multiple markings, then visualizations using these multiple markings have to be set individually. Below I am removing the marking for a single data table.

Reset_Markings_6

Though we Unmark marking for a single visualization, the markings which have got applied through cascading are not reset in other visualizations. In the bottom visualization, the markings are not reset as it inherits markings from the center visualization.

Reset_Markings_7

In such cases, our script should do the job.

Reset_Markings_8

Advertisements

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.

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

Spotfire: Show/Hide Regression Line (Straight Line Fit)

Posted on Updated on

The Straight Line Fit from Lines & Curves could be customized and shown/hidden as per requirement.

Below is a Scatter plot used to add a straight line fit.

ShowHide_Regression_Line_1

Make sure that the Straight Line Fit is the first fitting model in Lines & Curves (to use the script below).

ShowHide_Regression_Line_3

Add a button with script as below. In the previous step, since the first fitting model is our Straight Line Fit, we refer it in the script as FittingModels[0]. If your Straight Line is the second fitting model, you could refer it as FittingModels[1] and so on. Additionally, we need to pass the Scatter Plot as Visualization input vis to the script.

ShowHide_Regression_Line_4

IronPython Script:

from Spotfire.Dxp.Application.Visuals import ScatterPlot
for fm in vis.As[ScatterPlot]().FittingModels:
for vis in Application.Document.ActivePageReference.Visuals:
if vis.Title == "Sales by Profit":
if vis.As[ScatterPlot]().FittingModels[0].Enabled == True:
vis.As[ScatterPlot]().FittingModels[0].Enabled = False
else:
vis.As[ScatterPlot]().FittingModels[0].Enabled = True

Now you can use the button to toggle and show/hide the regression line.

ShowHide_Regression_Line_2

Spotfire: Changing Time Hierarchy using Multiple Date Columns

Posted on Updated on

When using multiple date columns (switched using property control) on a particular axis, we lose the ability to change the time dimension of the date selected. In order to overcome this issue, we can add a Time Hierarchy property control (a slider) and use it to alter the date column’s time hierarchy.

We start off by building a property control for changing date column. An important note here is to provide the Value as exact literals of the column name which you want to use against a Display Name. 

Change_Time_Hierarchy_3

Then we create a TimeHierarchy slider control to navigate across three dimensions – Year, Quarter and Month.

Change_Time_Hierarchy_4

And apply the below custom expression on the date column.

Change_Time_Hierarchy_5

This customization allows us to change the Time Hierarchy for Date columns. Below is a view of quarterly Sales (by Order Date)

Change_Time_Hierarchy_2

And a monthly Sales view (using Dispatch Date)

Change_Time_Hierarchy_1

K-Means Clustering using Spotfire

Posted on Updated on

To perform K-Means clustering in Spotfire, we need an appropriate line chart on which the clustering calculation could be performed.

Below we are using a simple line chart with Sales on Y-axis (a single column and not a custom expression with multiple columns) and Order Date on X-axis.

K-Means_Cluster_1

The Euclidean distance is used to perform the clustering with maximum number of clusters to be 9.

K-Means_3

Here is a segmented view of similar product sub-categories which fall in clusters depending on their sales performance.

K-Means_Cluster_2

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))