The Spotfire Community is moving to TIBCOmmunity and this forum location has closed. During the transition, you can still search the old forums but posting has been disabled. We encourage you to pick up the discussion at the new Spotfire community on TIBCOmmunity.
April 2011 - Posts - Tip of the Week

Tip of the Week

April 2011 - Posts

  • Performing Data Entry and Analysis with TIBCO Spotfire

    Spotfire provides a variety of mechanisms for accessing data, including local files, relational databases, web services, and everything in between.  One request which has come up more and more is the ability to perform data entry using Spotfire. This could be to store data in the underlying data source, it could be for a quick approach to analyze new data in the Web Player,  it could be for updating properties in the existing Data Table, or it could be to add rows or columns into a Data Table similar to how you would add them into an Excel file.

    We will explore all these requirements in future tips. In this tip, we will focus on how to use a Spotfire file as a data source itself and load data into it one row at a time. In this scenario, we will want Spotfire to keep track of all our training course registrations.  Rather than entering them one at a time in Excel or in a database, we will enter them directly for input into Spotfire, appending them to the existing Data Table.


    There are a variety of options for entering the data. You can use an HTML form which is outside of Spotfire to enter the data, and then use the Web Player APIs to load the data into Spotfire, or you can use a Text Area inside an existing Spotfire analysis file directly. In this tip, we will discuss the latter.


    To start, we should load one row of our training registration data. This would be to initialize the Data Table and Spotfire Analysis file.  For this example, the format of the Data Table is shown below.


     
    Then, we need to create the Text Area and Property Controls to allow the user to enter the data.  You can choose to simply use the out of the box features in a Text Area to create something similar to the image below:


     
    Or, you can use a Script to create a custom layout for your Text Area, similar to what we discussed in earlier posts.  A sample of the information above, displayed in an HTML table, is shown below:

     

    Once you create the Text Area and Property Controls that you would like to use, you will need to create a Script Control which will add the data to the currently loaded Data Table.  Assume we have each property control passed in as a parameter, and we call those parameters customer, type, date, costs, location, and company, the following script would add this information as a new row to our Data Table.


    import clr
    clr.AddReference("SpotfireTraining.ArrayDataSource, Version=1.0.0.0, Culture=neutral, PublicKeyToken=489d6add0b459834")
    from SpotfireTraining.ArrayDataSource import ArrayDataSource
    from System import Array,String,Object
    from Spotfire.Dxp.Data import AddRowsSettings
    from Spotfire.Dxp.Data import DataType

    temp = Array[object ]((customer,type,str(date),str(costs),location,company))
    ads =  ArrayDataSource(temp, Document.ActiveDataTableReference)
    ars = AddRowsSettings(Document.ActiveDataTableReference, ads)
    Document.ActiveDataTableReference.AddRows(ads,ars)

    This example uses an ArrayDataSource, which is built using the full C# SDK. This allows us to pass information into Spotfire from an array.  If you did not have access to the SDK to create the Array Data Source, you can still use this functionality, but it requires you to use the built-in TextData Source. The code for this solution is shown below:

    from System import Array,String,Object
    from System.IO import Path, File, StreamWriter
    from Spotfire.Dxp.Data import DataTableSaveSettings
    from Spotfire.Dxp.Data.Import import TextFileDataSource
    from Spotfire.Dxp.Data.Import import TextDataReaderSettings
    from Spotfire.Dxp.Data import AddRowsSettings
    from Spotfire.Dxp.Data import DataType
    #Temp file for storing properties
    tempFolder = Path.GetTempPath()
    tempFilename = Path.GetTempFileName()


    # generate data and write to file
    f = open(tempFilename, 'w')
    f.write(customer + '\t' + type + '\t' + str(date) + '\t' + str(costs) + '\t' + location + '\t' + company + '\n')
    f.close()
    # read file data and replace data table content
    readerSet = TextDataReaderSettings()
    readerSet.Separator = "\t"
    readerSet.SetColumnName(0, "Customer")
    readerSet.SetColumnName(1, "Type")
    readerSet.SetColumnName(2, "Date")
    readerSet.SetDataType(2,DataType.Date)
    readerSet.SetColumnName(3, "Costs")
    readerSet.SetDataType(3,DataType.Integer)
    readerSet.SetColumnName(4, "Location")
    readerSet.SetColumnName(5, "Company")
    textDataSource = TextFileDataSource(tempFilename, readerSet)

    ars = AddRowsSettings(Document.ActiveDataTableReference, textDataSource)
    Document.ActiveDataTableReference.AddRows(textDataSource,ars)

    Once completed, you know have a Spotfire Analysis file, which includes the data entry component for your solution , the analytic component, and also stores the data (the dxp file is the data source).  When the user enters the new information in the Property Controls and clicks on the Script Control button, the data is added to the existing Data Table as a new row and then persisted in the Spotfire Analysis file. All the existing pages and visualizations are automatically updated to analyze the latest data, including the row that was just added.

    If you have a need to use this data in other applications, or you wish to store it outside the Spotfire Analysis file, you can add another step where you store the information into a database after you enter it in the Property Controls. Then the Data Table in Spotfire is updated using Spotfire’s data on demand technology. This will be discussed in an upcoming tip.

     

  • Creating Custom Reports with Script Controls - Part III

    This is the final tip in a three part series, where we learn how to build custom HTML reports in TIBCO Spotfire.  We started by learning how to output aggregated data from table-based visualizations, and then how to export images of Spotfire visualizations.  We then applied stylesheets to these assets to create custom reports.  In this week’s tip, we will put it all together and include a custom report generator which will create an HTML report, which includes a custom layout, including stylesheets, comments, and a few other bells and whistles like which filters are being used. It can even be extended to create a WYSIWYG report generator interface.

    There are several key components to the Spotfire report: the visualizations, the layout, and the contextual information.  We have already learned how to output the visualizations and style them, so we will now focus on the layout.

    Custom Report Layout
    Since this is HTML, we can use HTML Tables, or Cascading Style Sheets to create an exact layout for the desired report. If you are building this report for a specific Spotfire Analysis file, you can build a layout specific to that as well. Otherwise, you can also use a more generic layout so it is applicable to all analysis files.

    You can also create a property control to allow the user to define the report type. In this example, we have a standard report (called ‘classic’), one specific for printouts, one for displaying on large displays, one specific for displaying in tablet PCs, and one for displaying in a Spotfire analysis file, like a Cover Page.


    Each selection will have its own CSS associated with it that will determine the layout and style.

    One example HTML and CSS is shown below:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html
    >
    <head>
    <title>Custom Spotfire Report</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <style type="text/css">
    <!--

    #Table_01 {
     position:absolute;
     left:0px;
     top:0px;
     width:928px;
     height:566px;
    }

    #tabbedNavigation {
     position:absolute;
     left:0px;
     top:0px;
     width:928px;
     height:19px;
    }

    #header {
     position:absolute;
     left:0px;
     top:19px;
     width:672px;
     height:158px;
    }

    #filter {
     position:absolute;
     left:672px;
     top:19px;
     width:248px;
     height:547px;
    }

    #divider {
     position:absolute;
     left:920px;
     top:19px;
     width:8px;
     height:547px;
    }

    #vis1 {
     position:absolute;
     left:0px;
     top:177px;
     width:672px;
     height:188px;
    }

    #vis2 {
     position:absolute;
     left:0px;
     top:365px;
     width:672px;
     height:201px;
    }

    -->
    </style>
    </head>
    <body style="background-color:#FFFFFF;">
    <div id="Table_01">
     <div id="tabbedNavigation">
      <!--INSERT TABBED NAVIGATION -->
     </div>
     <div id="header">
      <!--INSERT HEADER TEXT -->
     </div>
     <div id="filter">
      <!--INSERT FILTER SCRIPT -->
     </div>
     <div id="divider">
      <img id="spacer" src="images/divider.gif" width="8" height="547" alt="DIVIDER" />
     </div>
     <div id="vis1">
      <!-- INSERT FIRST SPOTFIRE VISUALIZATION-->
     </div>
     <div id="vis2">
      <!-- INSERT SECOND SPOTFIRE VISUALIZATION-->
     </div>
    </div>
    </body>
    </html>

    This example layout has placeholder for 4 things: header text, filter settings, and 2 spotfire visualizations.

    The header text can be inputted by a user from a series of property controls:
     

    In addition, we want to display the current filter settings per page in our export report. We are able to retrieve that using a script which is listed here.
    http://spotfire.tibco.com/community/blogs/stn/archive/2010/07/19/seeing-what-filter-devices-are-active.aspx

    Then we add a script to our text area, which will export the visualizations (which we learned the last two tips), write the HTML template layout which will read in the correct CSS file, and add in the properties and images to the report:


        from System.IO import Path   
        tempFolder = Path.GetTempPath()
        tempFilename = Path.GetTempFileName()
        f = open(tempFilename, 'w')
        f.write(
        "<html>\n"+
        "  <head>\n"+
        "    <link rel='stylesheet' type='text/css' href='print.css' media='print'/>\n"+
        "  </head>\n"+
        "  <body style='background-color:#FFFFFF;'>\n"+
        "  <div id='Table_01'>\n"+
     "  <div id='tabbedNavigation'>\n"+
     " <!--INSERT TABBED NAVIGATION -->\n"+
     "  </div>\n"+
     "  <div id='header'>\n"+
      Document.Properties["header"] +"\n"+
     "  </div>\n"+
     "  <div id='filter'>\n"+
      strFilterSettings +"\n"+
     "  </div>\n"+
     "  <div id='divider'>\n"+
     " <img id='spacer' src='images/divider.gif' width='8' height='547' alt='DIVIDER' />\n"+
     "  </div>\n"+
     "  <div id='vis1'>\n"+
          vis1src +"\n"+
     "  </div>\n"+
     "  <div id='vis2'>\n"+
      vis2Src +"\n"+
     "  </div>\n"+
        "</div>\n"+
        "</body>\n"+
        "</html>\n")
        f.close()


    Here are a couple samples  of the output when everything is put together. This scenario creates an HTML report which looks similar to the tabbed layout you would see in the Spotfire Web Player, only with static HTML: 


     


     

    Other Deployment Options
    If you wish to have a solution similar to this available for all Analysis files, then you can use the SDK and our C# extensions to create a Custom Export Tool, which can be built to show up both in TIBCO Spotfire Professional as well as in the TIBCO Spotfire Web Player. 

    You can also hook this code up to Automation Services and create a server-side reporting renderer. This would provide a centralized approach to storing and rendering reports, and would also allow users to view reports without installing additional software or requiring additional licenses.


     

Syndication

Other Spotfire Blogs

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