Add a legend to a graph then edit it in Grapher automation

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

To run this script:

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

OR:

  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 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
	Legend.top = 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).symbolFreq=1
	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
	Legend.DeleteEntry(2)

	'Set UseSymbolTable
	LineScatterPlot1.UseSymbolTable = True

	'Set ShowSymbolTableLegend
	LineScatterPlot1.ShowSymbolTableLegend = True

	Wait 1

	'Set UseSymbolTable
	LineScatterPlot1.UseSymbolTable = False

	'Set ShowSymbolTableLegend
	LineScatterPlot1.ShowSymbolTableLegend = False

	'Set SymFillColorCol
	LineScatterPlot1.SymFillColorCol = 2

	'Set Symbol.Fill.Gradient
	LineScatterPlot1.Symbol.Fill.Gradient.Name = "Rainbow10"

	'Set Symbol.Fill.GradientLimitsAuto
	LineScatterPlot1.Symbol.Fill.GradientLimitsAuto = True

	'Set Symbol.Fill.GradientLimitsMin
	LineScatterPlot1.Symbol.Fill.GradientLimitsMin = 42.2

	'Set Symbol.Fill.GradientLimitsMax
	LineScatterPlot1.Symbol.Fill.GradientLimitsMax = 62.7

	'Set ShowColorScale
	LineScatterPlot1.ShowColorScale = True
  
End Sub

 

Updated March, 2020

Was this article helpful?

Comments

0 comments

Please sign in to leave a comment.