How do I create a graph and change properties of the plot and axes via Grapher automation?

This Grapher sample script shows how to create a graph and change the properties of the line/scatter plot and the axes. It then shows how to add a legend that contains text from worksheet cells, and how to export the graph to a TIF file with the same as the GRF name.


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

Sub Main

 'Initialize Grapher
  Dim GrapherApp, Docs, Plot, Graph1 As Object
  Set GrapherApp = CreateObject("Grapher.Application")
  GrapherApp.Visible = True
  Set Docs = GrapherApp.Documents
  Set Plot = Docs.Add(grfPlotDoc)

 'Create a line plot
  Set Graph1 = Plot.Shapes.AddLinePlotGraph(GrapherApp.Path + "\samples\tutorial.dat")
  Set Line1 = Graph1.Plots(1)
  Set XAxis = Graph1.Axes("X Axis 1")

 'Add second line plot
 Set Line2 = Graph1.AddLinePlot(GrapherApp.Path + "\samples\tutorial.dat",1,3)

 'Change Line color for Line2
 Line2.line.foreColor = grfColorBlue

 'Set the XAxis to use date/time labels
 XAxis.TickLabels.UseDateTimeFormat = True

 'Change the Date/time format
 XAxis.TickLabels.MajorFormat.DateTimeFormat = 0

 'Add a legend
 Set Legend1 = Graph1.AddLegend(True)

 'Change the text of the legend line text to show the column name

 'Link legend entries to worksheet
 Wks = GrapherApp.Path+"\Samples\Tutorial.dat"
 While i < Legend1.EntryCount+1
  Legend1.EntryFont(i).worksheet = wks

 'Export to a TIF that contains the same name as the data file name

End Sub


Updated March 8, 2019

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



Please sign in to leave a comment.