Hi
Got an odd one with the charts module and MakeFeatureLayer. Code will work fine if you run it inside the ArcPro py window but if it's outside ArcPro as standalone script or run from inside a GP toolbox script, the result is a catastrophic app crash with the 'please report crash dialog' showing up 😞
basic code:
import arcpy
import os
source_fc = "chart-issues.gdb\\pts"
profile_name = "AB"
query_def = "Name='{}'".format(profile_name)
print(query_def)
r_chart_layer = arcpy.management.MakeFeatureLayer(source_fc, "chart_layer", query_def, None, None)
profile_chart = arcpy.charts.Line(
x="M",
y=["Z"],
dataSource = r_chart_layer.getOutput(0),
title="Elevation Over Distance: {}".format(profile_name),
xTitle = "Distance (Feet)",
yTitle = "Elevation (Feet)"
)
print("chart initialized")
chart_path = arcpy.CreateUniqueName(os.path.join("{}.svg".format(profile_name)), arcpy.env.scratchFolder)
print(chart_path)
profile_chart.exportToSVG(chart_path, 500, 500)
print("qed")
I tried a ton of variations and found a few extra nuggets.
- I know the feature class and feature layer are correct. If I set the dataSource = source_fc, chart generates without error but i don't get the filter applied so it's not 'path to data kind of issue'
- If I export the layer to a table or lyrx it will also work but 'why do i have to add an extra step to write data back to disk'?
- If I add: print(r_chart_layer.getOutput(0).dataSource)
- It will print out the expected path to the feature class
- Oddly, this changes the failure, it's no longer an app crash but generates this error message:
- Traceback (most recent call last):
File "D:\support\arcgis\chart-issues\create-chart.py", line 39, in <module>
profile_chart.exportToSVG(chart_path, 500, 500)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\charts.py", line 449, in exportToSVG
svgRes = self.getSVG(width if width != None else self.displaySize[0],
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\charts.py", line 445, in getSVG
return _convertArcObjectToPythonObject(self._arc_object.getSVG(width, height))
RuntimeError: Invalid dataset: CIMPATH=internal_map/pts.json
There are work arounds but seems like you should be able to MakeFeatureLayer() and assign it as a chart data source without any extra steps.