arcpy.charts.Line fails with simple csv

971
9
01-05-2023 04:18 PM
FredSpataro
Occasional Contributor III

Hi All, 

I must be missing something simple for a line chart.  I have a simple csv file X,Y data ... values are doubles and I want to create a line chart but it blows up with the error: 

File "<stdin>", line 1, in <module>
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))

If I use the 'scatter' chart type, works fine but I don't get the nice line thru the data...  so I don't think it's the data or csv format.  

ArcPro 3.0.2

CODE (running in a simple windows cmd window via C:\Users\fspataro\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\python.exe):

 

 

import arcpy
out_svg_file = r"T:\scratch\analysis2\test.svg"
temp_csv_file = r"T:\scratch\analysis2\test.csv"
c = arcpy.charts.Line(x='X', y='Y', dataSource=temp_csv_file)
c.exportToSVG(out_svg_file, width=400, height=400)

 

 

 

DATA (save as test.csv):

X,Y
0.00,8.32
3.27,8.66
6.54,9.06
9.81,9.44
13.08,9.86
16.35,10.38
19.62,11.02
22.89,11.74
26.17,12.67

Thanks!

 

0 Kudos
9 Replies
by Anonymous User
Not applicable

Pretty strange stuff, but if you convert the csv table to a dbf and pass that to the chart.Line method by using the path, it works.  It doesn't work if you assign the result of the arcpy.conversion.TableToTable method to a variable for the dataSource, (demonstrated below) BUT will work if you use the path string:

arcpy.conversion.TableToTable(temp_csv_file, r'T:\scratch\analysis2', 'tble')
c = arcpy.charts.Line(x='X', y='Y', dataSource=r'T:\scratch\analysis2\tble.dbf')
c.exportToSVG(out_svg_file, width=400, height=400)

 

Screenshot 2023-01-06 070405.png

 

Trying the result path assignment:

tble = arcpy.conversion.TableToTable(temp_csv_file, r'C:\GeoNet', 'tble')
c = arcpy.charts.Line(x='X', y='Y', dataSource=tble)

RuntimeError: dataSource: Valid layer, table or path to a dataset expected. Provided value: C:\GeoNet\tble.dbf .

ChristopherAllen
Esri Contributor

Hi @Anonymous User ,

You're correct, the second example is failing because `tble` is a `Result` object rather than the path. The following should work:

 

c = arcpy.charts.Line(x='X', y='Y', dataSource=str(tble))

 

Thanks,

Chris

0 Kudos
by Anonymous User
Not applicable

That is understandable to convert it to string first, but odd and mildly frustrating that the error message from the RuntimeError includes the valid path and on the surface, is a bit misleading. We know that the path info is there from the Result object, but it is not being parsed under the hood for the process, but is for the error.

0 Kudos
ChristopherAllen
Esri Contributor

Hi @Anonymous User , I completely understand your confusion with respect to the error message. We will look into either improving the error message or possibly accepting `Result` objects as a valid `dataSource` argument. 

Take care,

Chris

0 Kudos
FredSpataro
Occasional Contributor III

@Anonymous User Thanks!

 

The whole chart dataSource things is really odd... It works fine with a 'FeatureLayer' if you're running the code inside the ArcPro py window but standalone code with arcpy.MakeFeatureLayer doesn't work --- full on 'blow up' of the process.  

I did add the 'aggregation='min' on the constructor and got some sort of result but it was a bit off... 

I'm going to submit a bug on this and see what tech-support says.  I'll report back on the findings.

0 Kudos
ChristopherAllen
Esri Contributor

Hi @FredSpataro ,

Thanks for reporting this! I can reproduce the crash with the code you've posted using your data saved to a CSV. I'm logging an internal issue, but please feel free to submit a bug to technical support if you wish to track the progress.  

The code you posted should work without explicitly setting an `aggregation` in the constructor (this should just draw an unaggregated line chart), but it looks like you also tried setting `aggregation='min'` and that was also not drawing the chart how you expected. I tried with the following code: 

c = arcpy.charts.Line(x='X', y='Y', aggregation='min', dataSource=r'c:\temp\test.csv')
c.exportToSVG(r'c:\temp\test.svg')

 

And it looks like the chart rendered as expected:

 

callen_esri_0-1673298472185.png

 

But if you are seeing the chart draw incorrectly when setting `aggregation='min'`, could you please post a screenshot if possible? 

Thanks again!

Chris

0 Kudos
FredSpataro
Occasional Contributor III

@ChristopherAllen Thanks for the verification.  The 'aggregation=min' chart looks good when I use the full data.  Looks like I was tinkering and had converted the values to integers to see if 'doubles' were the cause...this produced non-unique values and then the aggregation kicked in and made the chart a bit off. 

ChristopherAllen
Esri Contributor

Great, thanks for following up! Glad to hear that it works when setting an aggregation, but we will address the bug you reported when no aggregation is set (ie, when the chart is unaggregated).

Take care, 

Chris

0 Kudos
ChristopherAllen
Esri Contributor

Hi @FredSpataro , I just wanted to follow up to let you know that this will be fixed in the upcoming Pro 3.1 release. Thanks again for reporting!

0 Kudos