Create a graph from a Grapher template via automation

This article contains a sample script for creating a graph from a template and adding information from the worksheet to the graph.

To run this script:

  1. Click here to download the BAS file: text and values from worksheet.bas.
  2. Click Automation | Scripts | Run, select the BAS file from your downloads directory, and click Open.


  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
'Declare the variable that references the application
 Dim GrapherApp As Object

 'Creates an instance of the Grapher Application object
 'and assigns it to the variable named "GrapherApp"
 Set GrapherApp = CreateObject("Grapher.Application")

 'Make Grapher visible
 GrapherApp.Visible = True

 'Assigns the Documents collection to the variable named "Docs"
 Set Docs = GrapherApp.Documents

 'Defines the files to work with
 datafile$ = GrapherApp.Path+"\samples\tutorial.dat"
 template$ = GrapherApp.Path+"\templates\line scatter plot.grt"

 'Open the worksheet
 Set Wks = Docs.Open(datafile$)

 'Read the contents of a cell for text
 Text1 = Wks.Cells("A1")

 'Read the contents of a cell for Y axis maximum value
 WksYMax = Wks.Cells("B9").Value

 'Creates a new plot window and assign it to the variable named "Plot"
 Set Plot = Docs.Add(grfPlotDoc,template$,datafile$)

'Add text read from the text string in the worksheet

'Sets variables for Y Axis 1 and X Axis 1
Set YAxis1 = Plot.Shapes.Item("Graph 1").Axes.Item("Y Axis 1")
Set XAxis1 = Plot.Shapes.Item("Graph 1").Axes.Item("X Axis 1")

'Change the Y axis maximum based on the worksheet value
YAxis1.AutoMax = False
YAxis1.Max = WksYMax

'Updates the worksheet for the linked text axis titles
XAxis1.title.worksheet = datafile$
YAxis1.title.worksheet = datafile$

'Set TickLabels.MajorFormat.NumDigits
XAxis1.TickLabels.MajorFormat.NumDigits = 0

'Adjusts the TickMarks major spacing
XAxis1.Tickmarks.MajorSpacingAuto = False
XAxis1.Tickmarks.MajorSpacing = 1

'Set TickMarks.MinorSide
XAxis1.Tickmarks.MinorSide = grfTicksOff

End Sub


Updated October 18, 2018

Was this article helpful?
0 out of 3 found this helpful



Please sign in to leave a comment.