Tip of the Week

Storing State in your Script Controls

Using Script Controls can really increase the analytic power of your Spotfire files and make it easier for people to search for insight.  Since an entire script is wrapped in a single transaction, tasks like looping through filters will only update the document once at the end.  This means, to perform tasks where you want to update the document multiple times, the users must execute the script multiple times.  One problem with this is that scripts do not maintain state between uses.  To solve this , we will use a property to store our state.

Consider the following example.  Assume we are analyzing Unemployment rates by County in the United States from 1999 – 2009. We can display this on a Map Chart as shown below.

 

This is nice and very useful to visualize, but a consumer may not want to see the unemployment rates summed up for all 11 years, they may want to see how the rates change year from year instead.  Sure, they can use the filter panel to move the year filter one year at a time, but it would be nice to wrap this in a Script so the consumer just has to click a button. At the same time, we can also perform some helpful updates while we are in there.

 

To start, create a Script Control in a Text Area.   The Script is going to pass in a visualization parameter that refers to the Map Chart.

The script will retrieve the ‘Year’ Column Filter as an Item Filter and will move the Item Filter by one step. We need to use a property to store the current step so we can just increment it by one next time the Script is executed.

 

Below is the full script:

 

import Spotfire.Dxp.Application.Filters as filters

from Spotfire.Dxp.Application.Filters import ItemFilter, FilterTypeIdentifiers

from Spotfire.Dxp.Data import DataProperty, DataType, DataPropertyAttributes, DataPropertyClass

 

myPanel = Document.ActivePageReference.FilterPanel

myFilter= myPanel.TableGroups[0].GetFilter("Year")

 

myFilter.FilterReference.TypeId = FilterTypeIdentifiers.ItemFilter

itemFilter = myFilter.FilterReference.As[filters.ItemFilter]()

 

whichCol = itemFilter.DataColumnReference

if (whichCol.Properties.PropertyExists("CurrentStep") == False):

    myProp = DataProperty.CreateCustomPrototype("CurrentStep",0,DataType.Integer,DataPropertyAttributes.IsVisible|DataPropertyAttributes.IsEditable)

    Document.Data.Properties.AddProperty(DataPropertyClass.Column, myProp)

    whichCol.Properties.SetProperty("CurrentStep",0)

else:

    whichVal = whichCol.Properties.GetProperty("CurrentStep")

    print whichVal

    print itemFilter.Values.Count

    if (whichVal == itemFilter.Values.Count):

            whichCol.Properties.SetProperty("CurrentStep",0)

    else:

        itemFilter.Value = itemFilter.Values.Item[whichVal]

        if (itemFilter.Value):

                        vis.Title = "Overall Unemployment by Country in " + itemFilter.Value

        else:

                        vis.Title = "Overall Unemployment by Country from 1999 to 2009"      

        whichCol.Properties.SetProperty("CurrentStep",whichVal+1)


 

 

The end result? Consumers can click on the button and it will step through the Year filter so that they can see the unemployment rates for the given year one year at a time. In addition the title of the Map Chart is updated to include the year for the current step.

 

Below is the result of the visualization and Filter Panel when the Script is executed to the point where the year column is 2007.

 

 

For more information on Script Controls, consider taking our  blended training course dedicated to them.

Comments

 

uggkensington said:

http://www.uggkensingtons.org/

http://www.uggkensingtons.org/ugg-fox-fur-short-boots-5531-c-54.html | UGG Fox Fur Short Boots 5531

http://www.uggkensingtons.org/ugg-kensington-boots-5678-c-2.html | UGG Kensington Boots 5678

http://www.uggkensingtons.org/ugg-rainier-eskimo-boots-5189-c-85.html | UGG Rainier Eskimo Boots 5189

http://www.uggkensingtons.org/ugg-retro-cargo-boots-1895-c-3.html | UGG Retro Cargo Boots 1895

December 27, 2011 8:22 PM
 

uggskensingtonsale said:

www.uggskensingtonsale.org

www.uggskensingtonsale.orgugg-adirondack-boots-ii-c-6.html | UGG Adirondack Boots II

www.uggskensingtonsale.orgugg-adirondack-tall-boots-c-11.html | UGG Adirondack Tall Boots

www.uggskensingtonsale.orgugg-amberlee-boots-c-34.html | UGG Amberlee Boots

www.uggskensingtonsale.orgugg-annabelle-boots-c-35.html |

December 28, 2011 4:39 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit

Syndication

Tags

Other Spotfire Blogs

Spotfire's interactive information visualization and analytic solutions give users a remarkable experience for quickly and easily querying data and reporting results for superior business intelligence. From portfolio management and customer retention programs to key processes such as CRM, marketing, research, bioinformatics, yield and asset management and design for manufacturing, enterprises around the world rely on Spotfire's business analytics software to improve operational performance.

©Copyright 2000-2011 TIBCO Software Inc | Privacy Policy | Terms of Use I Blog I Contact Us I Content Center