Select to view content in your preferred language

Arcade pop up chart

775
1
Jump to solution
10-01-2024 07:33 AM
Labels (3)
SarahHurley1
Occasional Contributor

Hi all, 

I am trying to create a chart in the pop up of the map using arcade. I have a layer with a dataset that has a "year", a "units" field (number of housing units for that year), and a "town" field (which has corresponding geometry for the map). I would like the popup to create a chart when you click on the town that categorizes the units into years, so it will be a column chart with the x-axis as categories for "years" and the y-axis is the sum of the units for that year. 

I am referencing this blog post to try to accomplish this: Part 2: Introducing Arcade pop-up content elements (esri.com)

This is my arcade script. When I run it in the window, it gives an error: Test execution error: Unknown Error. Verify test data. And then, obviously, no chart shows up in the popup. 

This is my script so far (edited from the blog post linked above):

var x = GroupBy($layer, 'year', { name: 'total', expression: 'units', statistic: 'SUM' });

var sorted = OrderBy(x, 'count DSC')
var chartValues = {}
var chartNames = []
for(var f in sorted) {
	chartValues[f.year] = f.total
	Push(chartNames, f.year)
}


return {
    type: 'media',
    title : 'Crime by precinct',
    description : 'Chart showing total crime by crime category',
    attributes : chartValues,  // replace this dictionary with your own key-value pairs,
    mediaInfos: [{
        type : 'columnchart', //linechart | barchart | piechart | columnchart
        //title : '',
        //caption : '',
        altText : 'bar chart showing units by year', //altText will be read by screen readers
        value : {
          fields: chartNames,  // choose what attributes to use in the chart
          //normalizeField : '',  // the name of the attribute to normalize by or value 
        }	  
      }]
}

 

Thanks for checking this out and let me know if anyone has any ideas of what I can try. 

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

There are a couple of things going on.

  • The FeatureSet "x" from the first line doesn't contain a "count" field to sort in line 3. Use the "total" field in line 3
  • Even though DSC in line 3 works, officially you should use DESC
  • The chartValue keys have to be strings, not numbers (although that doesn't seem to be documented anywhere). In my testing dataset, the Year field is numeric

Give this a try

var x = GroupBy($layer, 'Year', { name: 'total', expression: 'units', statistic: 'SUM' });

var sorted = OrderBy(x, 'total DESC')
var chartValues = {}
var chartNames = []
for(var f in sorted) {
	console(f.year)
	chartValues[`${f.year}`] = f.total
	Push(chartNames, f.year)
}

 

View solution in original post

0 Kudos
1 Reply
KenBuja
MVP Esteemed Contributor

There are a couple of things going on.

  • The FeatureSet "x" from the first line doesn't contain a "count" field to sort in line 3. Use the "total" field in line 3
  • Even though DSC in line 3 works, officially you should use DESC
  • The chartValue keys have to be strings, not numbers (although that doesn't seem to be documented anywhere). In my testing dataset, the Year field is numeric

Give this a try

var x = GroupBy($layer, 'Year', { name: 'total', expression: 'units', statistic: 'SUM' });

var sorted = OrderBy(x, 'total DESC')
var chartValues = {}
var chartNames = []
for(var f in sorted) {
	console(f.year)
	chartValues[`${f.year}`] = f.total
	Push(chartNames, f.year)
}

 

0 Kudos