Define and change a custom fit curve equation via automation

The capability to automate the creation of a custom fit curve and modify the fit curve equation is available in Grapher versions 20.1.151 and above. A script exemplifying this functionality is provided below.


To run this script:

  1. Copy the script below.
  2. Open Grapher and turn on the Script Manager by clicking View | Display | Script Manager.
  3. Press Ctrl+A to select all of the existing lines in the Script Manager and then press DELETE.
  4. Press Ctrl+V to paste it into the Script Manager.
  5. Update the lines in the User-defined parameters section.
  6. Click the green arrow Start/Resume icon in the Script Manager. 

Sub Main
'Initialize Grapher
Dim Grapher As Object
Set Grapher = CreateObject("Grapher.Application")
Grapher.Visible = True

'User-defined parameters
file$ = "C:\Program Files\Golden Software\Grapher\Samples\Line scatter plot.gpj"

'Fit equations and name
fit1 = "Y = m*x+b"
fitname = "Custom Fit 1"
fit2 = "Y = m*x^2+b"

Dim Array1(1) As String
Array1(0) = "m"
Array1(1) = "b"

'Initial Values
Dim Array2(1) As Double
Array2(0) = 0
Array2(1) = 0

Dim Array3(1) As Double
Array3(0) = -1.79769e+308
Array3(1) = -1.79769e+308

Dim Array4(1) As Double
Array4(0) = 1.79769e+308
Array4(1) = 1.79769e+308

'Open an existing file containing a line plot
Set linescatterplotgpj = Grapher.Documents.Open(file$)

'Define object pointer
Set Graph3 = linescatterplotgpj.Shapes.Item(1)
Set LineScatterPlot = Graph3.Plots.Item(1)

'Create the custom fit
Grapher.AddCustomFitDefinition(fitname, fit1, Array1, Array2, Array3, Array4)

'Add the custom fit to the plot
LineScatterPlot.AddFit(grfCustomFit, fitname)
Set Fit = Graph3.Plots.Item(5)

'Change the custom fit equation

End Sub


Updated March 10, 2020

Was this article helpful?
1 out of 1 found this helpful



Please sign in to leave a comment.