Search code examples
ironpythonspotfire

How can I draw a curve based on expression via ironpython?


In my scatterplot visualization, I added a "Curve from Data Table" with a custom expression that references variables from this data table. How can I do this via ironpython?

enter image description here


Solution

  • It all revolves around the FittingModels module. Assuming it is a ScatterPlot:

    from Spotfire.Dxp.Application.Visuals import *
    from Spotfire.Dxp.Application.Visuals.FittingModels import *
    
    # Get visual v by name
    # (alternatively input v as input parameter of type Visualization)
    vss=Document.ActivePageReference.Visuals
    v=None
    for vv in vss:
        if vv.Title=='the title of your chart': v=vv
    # This should already be a ScatterPlot, just cast it
    my_chart=v.As[ScatterPlot]()
    
    # Get hold of the FittingModels for this chart
    fitting_models=my_chart.FittingModels
    # If you want to remove all
    #fitting_models.Clear()
    
    # Define the new curve from data table
    data_table=Document.Data.Tables["the name of your data table"]
    curve_name = 'the name you give to the new curve'
    curve_expression = 'the curve expression'
    
    # If the curve already exists, do nothing, otherwise add it
    exists=False
    for ff in fitting_models:
        if ff.Curve.DisplayName==curve_name:
            exists=True
    
    if exists==False:
        new_curve = fitting_models.AddCurve(data_table,curve_expression)
        new_curve.Curve.CustomDisplayName=curve_name