How can I add a legend to a graph then edit it via Grapher automation?

This article contains a sample script for creating a graph, adding a legend, and editing the legend.

To run this script:

  1. Copy the script below, or click here to download the BAS file: Legend properties.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.


Sub Main
	'Declare the variable that will reference 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

	'Declare Plot As Object
	Dim Plot As Object

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

	'Add LinePlotGraph
	Dim Graph1 As Object
	Set Graph1 = Plot.Shapes.AddLinePlotGraph(GrapherApp.Path + "\samples\tutorial.dat")

	'Add second curve
	Graph1.AddLinePlot(GrapherApp.Path + "\samples\tutorial.dat",,3)

	'Add legend
	Set Legend = Graph1.AddLegend(True)

	'Move legend to new spot
	Legend.left = 4 = 4.5

	'Change entry text for first line
	Legend.EntryName(1, "Curve 1")

	'Change entry font for second line
	Legend.EntryFont(2).face = "Times New Roman"
	Legend.EntryFont(2).Bold = True
	Legend.EntryFont(2).color = grfColorGreen
	Legend.EntryFont(2).Italic = True
	Legend.EntryFont(2).size = 8

	'Show symbols on the curves and set size
	Graph1.Plots(1).symbol.size = 0.2
	Graph1.Plots(2).symbolFreq = 2
	Graph1.Plots(2).symbol.size = 0.1

	'Change the symbol mode and size
	Legend.EntrySymbolMode (1,grfFixedSize)
	Legend.EntrySymbolSize (1,0.03)
	Legend.EntrySymbolMode (2,grfPlotSize)

	'Change the number of symbols shown
	Legend.SymbolCount = 2

	'Change the title font properties
	Legend.TitleFont.face = "Arial"
	Legend.TitleFont.size = 16
	Legend.TitleFont.color = grfColorBlue

	'Change the title text
	Legend.TitleText = "New Legend Title Text"

	'Fill the legend
	Legend.Fill.foreColor = grfColorBlack10

	'Change the line width
	Legend.line.width = 0.03

	'Change the frame style
	Legend.frameStyle = grfRoundFrame

	'Set the legend margin
	Legend.margin = 0.35

	'Return how many legend items are shown
	Debug.Print Legend.EntryCount

	'Delete the second legend item


End Sub


Updated October 25, 2016

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


Please sign in to leave a comment.