Add chart to layer in map from Script Tool output

536
3
Jump to solution
11-15-2022 11:21 PM
Labels (2)
NickMiddleton
New Contributor III

Hi All,

 

Can an Arcpy Script Tool add a chart to a layer in a map?

The offending script will add the desired chart when the code is run from the Python Window as below:

 

import arcpy
import os
import datetime

lyrTemporalSamples = "samples1452temporal" #arcpy.GetParameterAsText(0)
inStationName =  "Vanesk Station" #arcpy.GetParameterAsText(1)
inBoreName = "All Bores" #arcpy.GetParameterAsText(2)

aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps("Halo")[0]
lyr = map.listLayers(lyrTemporalSamples)[0]
arcpy.env.addOutputsToMap = True
c = arcpy.Chart('TVCByDistance')
c.type = 'line'
c.dataSource = lyr
c.title = f"{inStationName}: Total Vegetative Cover By Distance from {inBoreName}"
c.yAxis.field = 'TVC'
c.yAxis.title = "% Total Vegetative Cover"
c.xAxis.field = 'distance'
c.xAxis.title = "Distance from a bore in metres (-9999 = no bore)"
c.line.aggregation = "MEAN"
c.line.splitCategory = "date_str"
c.addToLayer(lyr)
c.updateChart()

 

 

But when run it from a script tool with lines 5-7 above modified to the following the tool runs but doesn't add the chart:

 

lyrTemporalSamples = arcpy.GetParameterAsText(0)
inStationName =  arcpy.GetParameterAsText(1)
inBoreName = arcpy.GetParameterAsText(2)

 

Any help appreciated,

Nick

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
NickMiddleton
New Contributor III

Sorry for the disappointment but a restart of Pro has fixed my problem.

Hope the code might help someone and please reach out if you have questions.

Nick

View solution in original post

0 Kudos
3 Replies
NickMiddleton
New Contributor III

Sorry for the disappointment but a restart of Pro has fixed my problem.

Hope the code might help someone and please reach out if you have questions.

Nick

0 Kudos
Cristian_Galindo
Occasional Contributor III

I have no experience in the creation of "script tool", but in scripts with python and arcpy I do.

I would recommend try to make a old school debug technique:

In the line 8 set 

 

print(lyrTemporalSamples)

 

 

at least in that way you will know if the value is passed through the parameters. also validate the 

aprx 

 variable, to know if the current map is taken.

0 Kudos
ChristopherAllen
Esri Contributor

Hi @NickMiddleton ,

Thanks for the question! The preferred way of adding charts to an output layer or table in a tool is to set the  `charts` property of the output parameter (see the following documentation for more information).

To illustrate, I've created a simple script tool with the following parameters:

callen_esri_3-1668816340799.png

And the following tool code creates a new chart and adds it to the derived output layer by setting the `charts` property:

 

import arcpy

# capture the derived output layer parameter
output = arcpy.GetParameterInfo()[1]

# create the chart 
chart = arcpy.charts.Bar(x='neighbourhood_group', aggregation='count')

# add chart to the output layer by setting the `charts` property on the parameter
output.charts = [chart]

 

 

I hope this helps, but please feel free to reach out if you have any additional questions. 

Take care,

Chris