Creating Custom Reports with Script Controls - Part II


In last week’s post, we learned how to take data from table-based visualizations and create custom reports from them, using images and stylesheets.  In this week’s post we will look at how to take the non-table based visualizations and export them as images which we can use in our custom reports. 

To do this, we will need to use a Script Control and call the render method off a visualization. This method will render the visual to a specified graphics context. It takes in two parameters: a System.Drawing.Graphics object and a System.Drawing.Rectangle object.

The following code will create the Graphics object and populate it with the necessary information:

from System.Drawing import Bitmap,Graphics
bm = Bitmap(300, 300)
g = Graphics.FromImage(bm)

NOTE: The height and width can be passed in from Property Controls, allowing the user to determine the size of the exported image.

We then need to create Rectangle object to render the graphic to.

from System.Drawing import Rectangle, Point
r = Rectangle(Point(0,0), bm.Size)

Finally we need to call the Render method and save the graphic to a specific location. In next week’s post, we will post this to a known location for a complete export report. In this week’s post, we will show you how to save it to a temp file. This code snippet will take in both the Graphics object (g), Rectangle object (r), and Bitmap (bm)  we created earlier.

from Spotfire.Dxp.Application.Visuals import VisualContent
from System.IO import Path
tempFolder = Path.GetTempPath()
tempFilename = Path.GetTempFileName()
fullPath = Path.Combine(tempFolder, tempFilename)
vc = viz.As[VisualContent]()
vc.Render(g, r)
bm.Save(fullPath)

The complete script is shown below:

from System.Drawing import Bitmap, Graphics, Rectangle, Point
from Spotfire.Dxp.Application.Visuals import VisualContent
from System.IO import Path

tempFolder = Path.GetTempPath()
tempFilename = Path.GetTempFileName()
width = 300
height = 300

fullPath = Path.Combine(tempFolder, tempFilename)
vc = viz.As[VisualContent]()
bm = Bitmap(width, height)
g = Graphics.FromImage(bm)
r = Rectangle(Point(0,0), bm.Size)
vc.Render(g, r)
bm.Save(fullPath)

Applying Styles 

With the image now saved as a png file, we can apply any style to it that we want.  Here are a couple of samples created using Cascading Style Sheets.

The first example will overlay some text, maybe a title, or comments ontop of the exported image:

 

<style>
.basicOpacity{
opacity:.50;
filter:alpha(opacity=50);
filter:"alpha(opacity=50)";
}
.imghover a:hover img { filter: alpha(opacity=50);-moz-opacity: .5;opacity:0.5; }
.image {position:relative; width:172px; height:216px; margin:0;}
.text {position:absolute; top:220; left:200; width:230; height:75; background:#fff; padding:5px;}
.text {filter: alpha(opacity=70);-moz-opacity: 0.70; opacity:0.7;}
.words {position:absolute; top:230px; left:200; width:230; height:75; background:transparent; padding:5px;text-align:center;}

</style>

<body>
<div class="image"><img src="exportImage1.png"><div class="text"></div><div class="words">Outstanding Cases by Process Type<BR>03-21-11</div></div>

</body>

The second example will add a custom watermark image to your exported image:

<STYLE>
#watermark_box {
position:relative;
display:block;
}
img.watermark {
position: absolute;
top: 4px;
left: 4px;
opacity:.50;
filter:alpha(opacity=50);
filter:"alpha(opacity=50)";
}
</STYLE>

<div id="watermark_box">
<img src="exportImage1.png" />
<img src="watermark.png" class="watermark"  />
</div>

 

In next week's post, we will combine what we discussed last week and this week to create a custom report generator using Property and Script Controls, which works in Spotfire Professional as well as the Spotfire Web Player.