Follow

How can I calculate weighted average statistics via Grapher automation?

This article contains a Grapher sample script for adding a fit curve to a line/scatter plot and using the Send Keys command to add weighted average statistics.

To run this script:

  1. Click here to download the BAS file: Weighted Average weights.BAS
  2. Click Automation | Scripts | Run, select the BAS file from your downloads directory, and click Open.

OR:

  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. Click the Start/Resume icon () in the Script Manager.

 

*********

Sub Main

 Dim GrapherApp As Object
 Set GrapherApp = CreateObject("Grapher.Application")
 GrapherApp.Visible = True

'Add a plot document
 Set Plot = GrapherApp.Documents.Add(grfPlotDoc)

 'Add a graph
 Set Graph = Plot.Shapes.AddLinePlotGraph(GrapherApp.Path+"\samples\sample3.dat")

 'Add a fit curve to the first curve
 Set FitWtgAvg = Graph.Plots(1).AddFit(grfWeightedAvgFit)

 'Select Fit curve -- Must be the only item selected to use the next SendKeys commands
 FitWtgAvg.selected = True

 'Use Send Keys to choose Edit | Properties to open Fit curve dialog
 AppActivate "Grapher"
  'SendKeys "{DoubleClickLeft x,y}"
 SendKeys "%E"
 SendKeys "o"

 'Change Window Width to 3
 SendKeys "3"

 'Tab to Weights button and press Enter
 SendKeys "{Tab}"
 SendKeys"{Enter}"

 '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}"
 Wait 1
 'Close dialog  SendKeys"{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}"
 SendKeys"{Enter}"


End Sub

 

Updated October 11, 2018

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.