This article contains a sample script for setting the weights for a weighted average fit curve. Note that there are two sections of the script. The first section contains direct commands that can be used to adjust the weight parameters in Grapher 14 and newer versions. The second section contains and example of how to set the weights in Grapher 13 down to Grapher 5.
To run this script:
- Copy the script below.
- Open Grapher and turn on the Script Manager by clicking View | Display | Script Manager.
- Press Ctrl+A to select all of the existing lines in the Script Manager and then press DELETE.
- Press Ctrl+V to paste it into the Script Manager.
- Click the green arrow Start/Resume icon in the Script Manager.
Sub Main Dim GrapherApp As Object Set GrapherApp = CreateObject("Grapher.Application") GrapherApp.Visible = True 'Add plot window Set Plot = GrapherApp.Documents.Add(grfPlotDoc) 'Add graph Set LineGraph = Plot.Shapes.AddLinePlotGraph(GrapherApp.Path+"\samples\sample3.dat") 'Set LinePlot to the line plot object Set LinePlot = LineGraph.Plots.Item(1) 'Add fit LinePlot.AddFit(grfWeightedAvgFit) 'Set LineFit to the fit curve object Set LineFit = LinePlot.Fits.Item(1)
'---- Grapher 14 and newer support using the commands below. ----
'Set the LineFit options -- for weighted fit, this is the window width
LineFit.Option = 5
'Set the LineFit options -- for weighted fit, this is the window width LineFit.Option = 3 LineFit.SetWeight(1,1) 'Sets the order 1 weight to 1 LineFit.SetWeight(2,1) 'Sets the order 2 weight to 1 LineFit.SetWeight(3,2) 'Sets the order 3 weight to 2 LineFit.SetWeight(4,2) 'Sets the order 4 weight to 2 LineFit.SetWeight(5,1) 'Sets the order 5 weight to 1 '---- Grapher 13 and older versions use SendKeys to set the weight parameters. ----
'Set the LineFit options -- for weighted fit, this is the window width
LineFit.Option = 3
'Select the fit curve LineFit.selected = True 'Linefit weighted fits -- how to set the weights in G5 through PI AppActivate "Grapher" SendKeys "%{Enter}" 'activates PI SendKeys "{Left}" 'insure Plot section Is closed SendKeys "{Right}" 'expand Plot section SendKeys "{Down}" 'move selection down To Weights Dialog control SendKeys "{Down}" SendKeys "{Down}" SendKeys "{Enter}" 'bring up Weights Dialog 'The Weights dialog is open. Go to first entry, tab to Set Weight button and press Enter SendKeys "{Down}" SendKeys "{Tab}" SendKeys "{Enter}" 'Set the weight for the first item using SendKeys SendKeys "5" SendKeys "{Enter}" 'Go to the second entry, tab to Set Weight button and press Enter SendKeys"+{Tab}" '+ indicates Shift is pressed SendKeys "{Down}" SendKeys"{Tab}" SendKeys "{Enter}" 'Set the weight for the second item using SendKeys SendKeys "2" SendKeys "{Enter}" 'Go to the third entry, tab to Set Weight button and press Enter SendKeys"+{Tab}" '+ indicates Shift is pressed SendKeys "{Down}" SendKeys"{Tab}" SendKeys "{Enter}" 'Set the weight for the third item using SendKeys SendKeys "3" SendKeys "{Enter}" 'Close dialog SendKeys"{Tab}" SendKeys"{Enter}" End Sub
Updated March 6, 2019
Comments
Please sign in to leave a comment.