Follow

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, or download the attached BAS file: example script.BAS.
  2. In a Windows Explorer window, navigate to C:\Program Files\Golden Software\Grapher 12\Scripter.
  3. Double click on Scripter.exe to launch Scripter.
  4. Press Ctrl+A to select all of the existing lines then press Delete.
  5. If you copied this script, press Ctrl+V to paste it into Scripter. If you downloaded it, click File | Open, select the BAS file from your downloads directory, and click Open.
  6. Click Script | Run to run the script.
     

*********

The HTML Clipboard
Sub Main

 'Declares GrapherApp as an object
  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

 'Declare Docs as Object
  Dim Docs As Object

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

 'Declare Plot As Object
  Dim Plot As Object

 'Creates a new plot window and assigns it
 'to the variable named "Plot"
  Set Plot = Docs.Add(grfPlotDoc)

 'Declare Shapes As Object
  Dim Shapes As Object

 'Assigns the AutoShapes collection to the
 'variable named "Shapes"
  Set Shapes = Plot.Shapes

 'Add LinePlotGraph
  Dim Graph1 As Object
  Set Graph1 = Shapes.AddLinePlotGraph(GrapherApp.Path + "\samples\tutorial.dat")
  Set Line1 = Graph1.Plots(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 XAxis to the first X axis on the graph
 Set XAxis = Graph1.Axes("X Axis 1")

 '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
  Legend1.EntryName(1,"<<@b1>>")
  Legend1.EntryName(2,"<<@c1>>")

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

'Export to a TIF that contains the same name as the data file name
Plot.Export(Left(Wks,Len(Wks)-3)+"TIF")


End Sub

 

Updated April 14, 2017

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.