﻿'===========================================================
'This script renames plots based on the Y variable column 
'header. It applies only to plot types with a YCol property. 
'Refer to the "YCol Property" page in the Help for a list of 
'supported plot types.
'===========================================================
Sub Main
	
	'=======================================================
    ' USER-DEFINED VARIABLES
    '=======================================================
	' Path to input GRF or GPJ file
	inputFile = "C:\Program Files\Golden Software\Grapher\Samples\Graph Magnifier.gpj"
	'=======================================================

	' Initialize Grapher
	Dim Grapher As Object
	Set Grapher = CreateObject("Grapher.Application")
	Grapher.Visible = True

	' Open the input file in a plot document
	Dim PlotDoc As Object
	Set PlotDoc = Grapher.Documents.Open(inputFile)

	' Initialize objects used in the for loop below
	Dim Graph As Object, Plot As Object, Wks As Object

	' Loop through all shape objects in the plot document
	For i = 1 To PlotDoc.Shapes.Count
		
		' If the shape object is graph...
		If PlotDoc.Shapes.Item(i).Type = grfShapeGraph Then
	
			' Create and object pointer to the graph 
			Set Graph = PlotDoc.Shapes.Item(i)

			' Loop through the plots under the graph
			For j = 1 To Graph.Plots.Count
								
				' Create an object pointer to the plot object 
				Set Plot = Graph.Plots.Item(j)

				' Get the worksheet used by this plot
				wksPath = Plot.Worksheet
				Set Wks = Grapher.Documents.Open(wksPath)

				' Get the column index from the plot
				columnIndex = Plot.YCol

				' Get the column name from the first worksheet row
				colName = Wks.Cells(1, columnIndex)

				' Use the the column name to rename the plot
				Plot.Name = colName

				' Close the worksheet
				Wks.Close(grfSaveChangesNo)
			Next
		End If
	Next
End Sub
