Jump to content
  • Trigger IronPython Script with Marking and create visualizations in trellis mode layout


    This article demonstrates how to create dynamic dashboards in Spotfire using IronPython scripting triggered upon marking. It starts by setting up a bypass data function to pass marked values to a document property. Then, an IronPython script creates and arranges bar chart visuals based on the marked sites, alongside existing visuals like treemaps. Finally, the layout is applied to the page, and adjustments can be made for specific needs.

    Here is an example that shows how to trigger an IronPython script upon marking. The script creates a set of visualizations and uses the Spotfire Layout API

    trellis.thumb.gif.a59afd3f61bd2eadaf6d0576edcf110e.gif

    To create this dashboard, you need to create a document property that is linked to an IronPython script that creates the barcharts when this document property changes. To update the document property we need a bypass data function. A bypass data function uses the same input as output, so let's start by creating the bypass data function

    bypass data function 

    Create a data function to pass the marked values to a document property:

    1. Data > Data Function Properties
    2. Register New and type "x" as the script in either R or Python. Name it "bypass" or something relevant
    3. Set the input and output parameters to be the same "x" allowing the input to take any data type value and the output to be a value
    4. Click run and set the refresh function automatically
    5. Set the input x to be an Expression that concatenates the unique values of a column of the selection by limiting the data from the marking. For example: UniqueConcatenate([Site])
    6. Set the output x to the "markedSites" Document Property. Create this property if not listed.
    7. Click on the following image to watch the entire process 👉 bypassdf.gif.2cf0d313a50dfe20e15670312208b246.gif


    iron python script

    This IronPython script is designed to create and arrange bar chart visuals in Spotfire. It first deletes any existing bar chart visuals on the active page. Then, it creates a bar chart template and duplicates it for each selected site. Finally, it arranges the visuals in a layout where the treemap is placed side by side with the bar chart visuals, with the bar chart visuals organized into rows, each containing up to three visuals.

    Create this script directly from File > Document Properties > Properties tab so when the markedSites document property changes, the script runs. Click to see how it should look like 👉 image.png.3874eee5d2eddb4c52724877413d79d7.png

    from Spotfire.Dxp.Application.Visuals import BarChart, VisualContent, VisualTypeIdentifiers, LabelOrientation, BarChartOrientation  
    from Spotfire.Dxp.Application.Layout import LayoutDefinition
    
    #script params
    dataTable = Document.Data.Tables["Best predictors"]
    
    
    #delete all barchart visuals
    page = Document.ActivePageReference
    for vis in page.Visuals:
    	if vis.TypeId == VisualTypeIdentifiers.BarChart:
    		page.Visuals.Remove(vis)
    
    #The last visual should be the treemap or whatever visual you use for marking. 
    tm = next(iter(page.Visuals))
    
    
    #create a barchart template
    bc = Application.Document.ActivePageReference.Visuals.AddNew[BarChart]()
    bc.Data.DataTableReference = dataTable
    bc.Title = "${DataTable.DisplayName}"
    bc.Legend.Visible= False
    bc.YAxis.Expression = "Sum([p-value])"
    bc.XAxis.Expression = "<[Case Name]>"
    bc.SortedBars=True
    bc.Orientation = BarChartOrientation.Horizontal
    
    
    #duplicate as many as selected sites 
    siteNames = Document.Properties["markedSites"]
    sites = sorted([s.strip() for s in siteNames.split(',')])
    
    #duplicate barchar template for each site
    firstSite = sites.pop(0)
    bc.Title = firstSite
    siteVisuals = [bc]
    bc.Data.WhereClauseExpression = "[Site_No] = '"+firstSite+"'"
    
    #create visuals
    for site in sites:
    	vis = page.Visuals.AddDuplicate(bc.Visual)
    	vis.Title =site
    	bc = vis.As[BarChart]()
    	bc.Data.WhereClauseExpression = "[Site_No] = '"+site+"'"
    	siteVisuals.append(vis.As[BarChart]())
    
    
    #arrange visuals
    #tm is the existing treemap
    ld = LayoutDefinition()
    ld.BeginSideBySideSection()
    ld.Add(tm, 10)
    
    # Begin a stacked section for the second column
    ld.BeginStackedSection(70)
    
    # Initialize a counter for siteVisuals
    i = 0
    
    for bc in siteVisuals:
        if i % 3 == 0:
            if i > 0: ld.EndSection()
            ld.BeginSideBySideSection()
        ld.Add(bc.Visual)
        i += 1
    ld.EndSection()
    ld.EndSection()
    ld.EndSection()
    
    # Apply the layout to the page
    page.ApplyLayout(ld)

    Test the script. You might want to tweak it to accomodate your needs. For example, the limit data with the expression from the Data.WhereClauseExpression  property or perhaps you have more bar charts in your page that you want to avoid deleting or other visuals that you need to re-arrange. Notice how the visuals are placed in the LayoutDefinition that is applied at the page level. You might also want to avoid using the same marking on other pages to avoid undesirable results. 

    Another enhancement to the script is to take an existing bar chart as a blueprint instead of programatically set the default values. That visual can be passed as a parameter to the script and reside on a hidden page.

     

     


    User Feedback

    Recommended Comments

    There are no comments to display.


×
×
  • Create New...