0

I have hundreds of pairs of FGDB and associated MXD files. I need to insert a graph in each mxd linked to its corresponding FGDB. No problem creating graph, but stuck on how to get graph into mxd file using arcpy. If the graph creation code is run inside the mxd manually, the graph appears, but I need to do it in the python script loop.

import arcpy
workspace = r'C:\gdb_dir'
workspaces = arcpy.ListFiles('*.gdb') # list of files to process
for workspace in workspaces:
 name_prefix = workspace[0:-4]
 current_MXD = name_prefix + '.mxd' # this file will already exist
 dataSrc = r'C:\name_prefix\Sample.gdb\DetailFeatures'
 fieldX = 'NEAR_X'
 fieldY = 'NEAR_Y'
 graph_template = r'C:\temp\ScatterGraphTemplate.tee'
 out_graph_name = "ScatterTest"
 out_graph = r'c:\graphs\' + name_prefix + '.grf' # How to insert this into mxd?
 graph = arcpy.Graph()
 graph.addSeriesScatterPlot(dataSrc, fieldY, fieldX)
 graph.graphAxis[0].title = "Near Y" 
 graph.graphAxis[2].title = "Near X"
 graph.graphPropsGeneral.title = "Test of Flake Property Graph"
 arcpy.MakeGraph_management(graph_template, graph, out_graph_name)
 arcpy.SaveGraph_management(out_graph_name, out_graph, "MAINTAIN_ASPECT_RATIO", 600, 375)

How do you insert out_graph into current_MXD?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jun 6, 2014 at 0:27
1
  • Would you be able to edit your Question to include a snippet of your code so far so that we can see more clearly what is already working and where you are stuck, please? Commented Jun 6, 2014 at 1:18

2 Answers 2

2

I don't think there is currently a way to add the grf file to the layout. However, you can save the graph as an image - see here . In your layout you can have a picture element called "GRAPH" which could be a placeholder for the image of your graph (just insert a picture into your layout). Change the element name here:

enter image description here

The path of the image will go here:

enter image description here

You can use arcpy to change the path of the image. See here for help on Picture Elements.

Some sample code might look like this:

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
for elm in arcpy.mapping.ListLayoutElements(mxd, "PICTURE_ELEMENT"):
 if elm.name == "GRAPH":
 elm.sourceImage = r"C:\Project\Data\MyGraph.jpg"
mxd.save()
del mxd
answered Jun 6, 2014 at 2:03
1
  • Thanks, but I need the interactive graph, not a static image. Commented Jun 6, 2014 at 16:22
0

To get the graph in from a script tool you need to do two things.

  1. make sure you run the script in the foreground (background processing will cause it to blow up)
  2. set an output parameter in your script and set that to the in memory name of the graph, out_graph_name in your case
Evil Genius
6,2992 gold badges29 silver badges40 bronze badges
answered Nov 25, 2015 at 17:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.