Summary
A sample script for exporting a visualization as an image
Introduction
The following is a sample IronPython script for exporting a Spotfire visualization as an image:
Copyright © 2017. TIBCO Software Inc. Licensed under TIBCO BSD-style license. #visual is a script input parameter that needs to be added to reference #the visualization being exported as an image import System from System.Drawing import Bitmap, Rectangle from System.Drawing import Graphics, Image from Spotfire.Dxp.Framework.ApplicationModel import ApplicationThread app = Application.GetService(ApplicationThread) image = Bitmap(800, 600) def f(visual=visual, document=Document, app=app, image=image, Graphics=Graphics, Rectangle=Rectangle): try: vis = visual gfx = Graphics.FromImage(image) rect = Rectangle(0,0,image.Width,image.Height) visualBounds = document.ActivePageReference.GetVisualBounds(vis, rect) vis.Render(gfx, visualBounds) except: return # DO SOMETHING WITH THE IMAGE HERE image.Save(r'C:Tempimage.bmp') app.InvokeAsynchronously(f)
Here is another example on how to export a visualization to a binary document property that can be later used in a text area by using a label property control:
import System
from System.IO import MemoryStream, SeekOrigin
from System.Drawing import Bitmap, Graphics, Rectangle
from System.Drawing.Imaging import ImageFormat
from Spotfire.Dxp.Application.Visuals import VisualContent
# 1. Import the specific Spotfire Data class for BLOBs
from Spotfire.Dxp.Data import BinaryLargeObject
def ExportVisual(visual):
try:
content = visual.As[VisualContent]()
# Create canvas
width = 800
height = 600
bitmap = Bitmap(width, height)
gfx = Graphics.FromImage(bitmap)
rect = Rectangle(0, 0, width, height)
# Render
content.Render(gfx, rect)
return bitmap
except Exception as e:
print("Error rendering visual: " + str(e))
return None
# --- MAIN EXECUTION ---
# 1. Get the bitmap from the visual
# Ensure 'vis' is passed as a parameter to the script
my_bitmap = ExportVisual(vis)
if my_bitmap:
try:
# 2. Save Bitmap to MemoryStream
stream = MemoryStream()
my_bitmap.Save(stream, ImageFormat.Png)
# 3. CRITICAL: Reset stream position to the beginning
# If you don't do this, the BLOB reader starts at the end and reads nothing.
stream.Seek(0, SeekOrigin.Begin)
# 4. Create the Spotfire BinaryLargeObject (BLOB)
blob = BinaryLargeObject.Create(stream)
# 5. Save to Document Property
# Make sure property 'aa' exists and is type 'Binary'
Document.Properties["binaryDocumentProperty"] = blob
print("Success: Visual saved to property a 'binaryDocumentProperty'" you can render it as a Label Property Control)
except Exception as e:
print("Error saving to property: " + str(e))
finally:
# Cleanup
if stream: stream.Dispose()
if my_bitmap: my_bitmap.Dispose()
See also
References
- API Reference: Spotfire Analyst
- API Reference: Page.GetVisualBounds Method
- API Reference: Visual.Render Method
Comments
0 comments
Article is closed for comments.