Suppose I have a visualization that's trellissed by panel by some categorical variable, with one panel per page. I would like to loop over the panels and export each image to a file, with the filename matching the categorical variable.
The image export works totally fine, following some published examples. However, I am having a lot of trouble actually getting the name of the current panel so that I can properly name the image.
Here's my code:
from Spotfire.Dxp.Application.Visuals import VisualContent
from System.Drawing import Bitmap, Graphics, Rectangle, Point
from System.IO import Path
import re
vc=viz.As[VisualContent]() #viz is the visualization parameter passed to the script
trellis=vc.Trellis
originalIndex=trellis.ActivePageIndex
outputDir=Document.Properties["imageOutputDir"]
for i in range(trellis.PageCount):
#move to the right page
trellis.ActivePageIndex=i
#make the actual image -
viz_r = Document.ActivePageReference.GetVisualBounds(viz)
width=viz_r.Width
height=viz_r.Height
bm = Bitmap(width,height)
g = Graphics.FromImage(bm)
g.TextRenderingHint = g.TextRenderingHint.AntiAlias
r=Rectangle(0, 0, width,height)
vc.Render(g, r)
#save the image
name="%d"%i
#here we would like to instead get the current value of the trellis variable!
#name=?
clean_name=re.sub(r'[/\\:*?"<>|]', '_',name)
tempFilename = outputDir+"\\%s.png"%clean_name
bm.Save(tempFilename)
print "image saved as " + tempFilename
trellis.ActivePageIndex=originalIndex
I've seemingly looked through all the methods of VisualContent
and Trellis
and haven't found it.
An alternative is to loop through the data and just get the values of the categorical variable. However, the order is not necessarily preserved, so this doesn't work well. If I can get just the data corresponding to each trellis panel, of course I could work from that.