Skip to Main Content
Spotfire Ideas Portal
Status Future Consideration
Product Spotfire
Categories API
Created by Guest
Created on Jan 9, 2024

Perform "Resize Columns to Fit" and "Size to Fit" from IronPython Script

Customer would like to perform "Resize Columns to Fit" and "Size to Fit" on table plot from IronPython Script.


  • Attach files
  • Guest
    Reply
    |
    Jul 22, 2024
    from Spotfire.Dxp.Application.Visuals import *
    from Spotfire.Dxp.Data import DataValueCursor
    from System.Drawing import *


    def getPageByName(pgName):
    for p in Document.Pages:
    if p.Title == pgName:
    return p

    def getVis(pageName, visName):
    page=getPageByName(pageName)
    for visual in page.Visuals:
    visualization = visual.As[Visualization]()
    if visualization != None:
    if isinstance(visualization, TablePlot):
    if visualization.Title == visName:
    vis=visualization
    return vis
    return None

    #get visualisations
    vis=getVis('xxx', "xxx")


    font = vis.TableFont
    dataTable=Document.Data.Tables[vis.Data.DataTableReference.Name]

    mc = Document.Properties['mc']

    pepCursor = DataValueCursor.CreateFormatted(dataTable.Columns["xxx"])
    for x in dataTable.Columns:
    try: #or create a list of column names and 'if c.Name in list'
    float(x.Name) #gets only numeric columns , non numeric get passed to catch
    cn = x.Name
    sz=0.0
    myColCursor = DataValueCursor.CreateFormatted(dataTable.Columns[x.Name])
    for row in dataTable.GetRows(myColCursor):
    if myColCursor.IsCurrentValueValid and not myColCursor.CurrentValue == '(Empty)':
    s = myColCursor.CurrentValue
    sl = (float(str(Graphics.FromImage(Bitmap(1, 1)).MeasureString(s, font))[1:-1].split(",")[0].split("=")[1]))+20
    sz = sz if sl < sz else sl
    tc = vis.TableColumns.TryGetTableColumn(dataTable.Columns[x.Name])[1]
    sz = sz if sz > 40 else 40
    if float(x.Name) <= mc:
    tc.Width = sz
    except:
    pass