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?
-
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?PolyGeo– PolyGeo ♦2014年06月06日 01:18:34 +00:00Commented Jun 6, 2014 at 1:18
2 Answers 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
-
Thanks, but I need the interactive graph, not a static image.Dan Bean– Dan Bean2014年06月06日 16:22:00 +00:00Commented Jun 6, 2014 at 16:22
To get the graph in from a script tool you need to do two things.
- make sure you run the script in the foreground (background processing will cause it to blow up)
- set an output parameter in your script and set that to the in memory name of the graph, out_graph_name in your case