TIBCO Spotfire Professional supports the ability for analysts to browse for and select an image to display inside a Text Area. However, in some situations, you may want to refresh the image or dynamically point to a different image based off certain conditions. This can be accomplished using a Script Control that will retrieve the image from a URL and then display it in the Text Area inside a binary Label Property Control.
Let’s assume we are working on an analysis in Spotfire of various car parts, and we are analyzing issues with parts that are still under warranty. We can go through our analysis, and when we select certain parts to review, we can not only show details about the part that are include in our Data Table, but we can also trigger a Script which will retrieve an image of this part from a Web query and return it into our Text Area as shown below:
When the user selects another car part to analyze in Spotfire that can trigger the Script to go and retrieve another image to display specific to the newly selected car part and replace the old image.
In the Script, we first need to generate the URL for the image. We can read in the model or part number value from a Document Property or from the marked row, and then use that value to create the URL that will provide us with the image.
For example, there may be a Web server that has images for all of the possible car parts and one of the query variables is the part model or SKU. In the example provided above, we have stored the selected car part in a Document Property. We can use that to build our URL using code similar to the code below:
uriString = "http://www.cartpartsrus.com?model=” + Document.Properties[“strWhichPart”]
uri = Uri(uriString)
NOTE:In order to assign a value to a Document Property in a Script Control, you first need to create that Document Property in Spotfire from the Edit > Document Properties > Properties tab.
Once we have the URL built, we will use an HTTP request to get the URL and return it as an image from a HTTP response stream.
webRequest = HttpWebRequest.Create(uri)
webRequest.Method = "GET"
rsp = webRequest.GetResponse()
img = Image.FromStream(rsp.GetResponseStream())
rsp.Close()
stream = MemoryStream()
img.Save(stream, ImageFormat.Png)
stream.Seek(0, SeekOrigin.Begin)
We can then turn the stream into a blob and then set the binary Label Property Control to that blob.
blob = BinaryLargeObject.Create(stream)
Document.Properties["PngImage"] = blob
Another common example for this is to retrieve something like a Stock Chart and display it in a Text Area.
Below is the full script. In this case, is retrieving the image for the stock symbol TIBX from the Yahoo Finance website and storing it in a Label Property Control called ‘PngImage’
from System.IO import MemoryStream, SeekOrigin
from System import Uri
from System.Net import HttpWebRequest
from System.Drawing import Image
from System.Drawing.Imaging import ImageFormat
from Spotfire.Dxp.Data import BinaryLargeObject
uriString = "http://chart.finance.yahoo.com/z?s=TIBX"
uri = Uri(uriString)
webRequest = HttpWebRequest.Create(uri)
webRequest.Method = "GET"
rsp = webRequest.GetResponse()
img = Image.FromStream(rsp.GetResponseStream())
rsp.Close()
stream = MemoryStream()
img.Save(stream, ImageFormat.Png)
stream.Seek(0, SeekOrigin.Begin)
blob = BinaryLargeObject.Create(stream)
Document.Properties["PngImage"] = blob