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:
- Copy the script below, or download the attached BAS file: example script.BAS
- In a Windows Explorer window, navigate to C:\Program Files\Golden Software\Grapher 13.
- Double click on Scripter.exe to launch Scripter.
- Press Ctrl+A to select all of the existing lines then press Delete.
- 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.
- Click Script | Run to run the 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 Start/Resume icon ( ) in the Script Manager.
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 September 24, 2018