How can I add a legend in Grapher with multiple different-sized symbols per bubble plot?

There is not a direct way to display multiple symbols of different sizes for each bubble plot if you have multiple bubble plots, but you can emulate this result by running the script below. 

  1. Copy the script below, or click here to download the BAS file: BubbleLegend.BAS.
  2. Open your Grapher project (like the attached ExampleBubblePlot.gpj), making sure it just contains the bubble plot graph(s).
  3. Make sure you have the Script Manager showing. If not, click View | Display | Script Manager to turn it on.
  4. If you copied this script, press Ctrl+V to paste it into Scripter. If you downloaded it, click the open file icon (​), select the BAS file from your downloads directory, and click Open.
  5. Click the green arrow (​) to run the script.
  6. Move the legend to where you want it.
  7. If the bubbles or text overlap the outline, change the limits and/or length of the x and/or y axis for the 'legend' graph.

'On the active document, looks for a bubble plot.If found, create a legend by
'creating a mini bubble plot that displays the values of the bubbles
'(additional worksheet data gets appended To the original bubble's data).

Sub Main

Dim Grapher As Object
Set Grapher = CreateObject("Grapher.Application")
Grapher.Visible = True

Set Doc = Grapher.Documents.Active

firsty = 100000
graphcount = 0

'Look for bubble plot
Dim plot As Object
For i=1 To Doc.Shapes.Count
	Set item = Doc.Shapes.Item(i)
	If item.Type = grfShapeGraph Then
		graphcount = graphcount+1
		For j=1 To item.Plots.Count
			If item.Plots.Item(j).PlotType = grfTernaryBubblePlot Or item.Plots.Item(j).PlotType = grfBubblePlot Or item.Plots.Item(j).PlotType = grfXYZBubblePlot Then
				Set plot = item.Plots.Item(j)

				'Different kinds of bubble plots have different ways to describe the bubble column
					bubbleCol = 0
					Select Case(plot.PlotType)
						Case grfBubblePlot
							bubbleCol = plot.zCol
						Case grfXYZBubblePlot
							bubbleCol = plot.sizeCol
						Case grfTernaryBubblePlot
							bubbleCol = plot.BubbleCol
					End Select

					'First, prepare the worksheet
					Set wks = plot.DisplayWorksheet
					newCol = wks.UsedRange.LastColumn+1
					wks.Cells(1,newCol).Value = firsty
					wks.Cells(2,newCol).Value = firsty
					wks.Cells(3,newCol).Value = firsty

					Set stats = wks.Range(plot.FirstRow, bubbleCol, plot.LastRow, bubbleCol).Statistics(True,False,wksStatsAll,wksIncludeAll,0,0)
					wks.Cells(1,newCol+1).Value = stats.Minimum
					wks.Cells(2,newCol+1).Value = stats.Median
					wks.Cells(3,newCol+1).Value = stats.Maximum

					firsty = firsty + 100000

					Doc.Activate 'done with worksheet now

					'Create a bubble plot to serve as the legend
					Set legGraph = Doc.Shapes.AddBubblePlotGraph(plot.worksheet,-1,newCol,newCol+1,"Legend for " + plot.Name)
					Set legPlot = legGraph.Plots.Item(1)

					'Display legend properties as original bubble plot
					legPlot.Labels.ShowLabels = True
					legPlot.Labels.LabelsInYCol = False
					legPlot.Labels.labelsCol = newCol+1

			End If
	End If

Set plot = Nothing 'reset

legstrt2 = graphcount+2
legstrt1 = graphcount+1

'Combines all of the 'legend' bubble plots into a single graph
For i = legstrt2 To Doc.Shapes.Count


'Deletes the now-empty graphs that had previously contained the other 'legend' bubble plots
For i = legstrt2 To Doc.Shapes.Count

'Assigns variables to the X and Y axes for the graph containing the 'legend' bubble plots
Set XAxis1 = Doc.Shapes.Item(legstrt1).Axes.Item(1)
Set YAxis1 = Doc.Shapes.Item(legstrt1).Axes.Item(2)

'Sets Y axis properties
YAxis1.length = 2
YAxis1.AutoMin = False
YAxis1.Min = 50000
YAxis1.AutoMax = False
YAxis1.Max = 370000
YAxis1.TickLabels.MajorOn = False
YAxis1.Tickmarks.MajorSide = grfTicksOff
YAxis1.Tickmarks.MinorSide = grfTicksOff

'Sets X axis properties
XAxis1.length = 2
XAxis1.AutoMax = False
XAxis1.Max = 3.5
XAxis1.AutoMin = False
XAxis1.Min = 0.8
XAxis1.TickLabels.MajorOn = False
XAxis1.Tickmarks.MajorSide = grfTicksOff
XAxis1.Tickmarks.MinorSide = grfTicksOff

'Sets the background line style for the legend to Solid
Doc.Shapes.Item(legstrt1) = "Solid"

End Sub


Updated December 22, 2017

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


Please sign in to leave a comment.