Select to view content in your preferred language

Create a serial chart in a dashboard referencing multiple layers

2255
5
Jump to solution
07-26-2023 01:00 PM
Labels (1)
Matt-Barrett
Regular Contributor

Hello,

I am working on a dashboard of police call data, and I'm trying to keep the data in separate layers for each fiscal year's worth of data.  I had been doing one layer with multiple years and after 5 + years the table has over 150,000 records and it's gotten a bit unwieldy.  So right now I'm working with just two years of data.  I have a simple serial chart showing number of calls by sector in that year, but I'd like to have a chart that combines both years' calls to compare year to year.

MattBarrett_0-1690401314300.png

In addition, I have indicators for all 5 sectors just showing the total number of calls in each sector.  I thought it would be cool to be able to filter each indicator with the top call type in each sector automatically, so as we add calls data each month, the indicators would automatically update with the current top call type.  I can always post that question separately.  But I figured I'd just include it here for now.

My problem is I've never learned arcade beyond the basic calculate scripts because I've just never been good at learning coding.  It never makes sense to me.  So I'm hoping someone can point me in the right direction.

Thanks!

Matt

 

0 Kudos
2 Solutions

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

Assuming all your layers have the same fields, you should be able to merge them with a data expression:

// load your layers
var p = Portal("https://www.arcgis.com")
var featuresets = [
  FeaturesetByPortalItem(p, "b0d335151aad48a5883326b9aed69cdd", 0, ["*"], false),
  FeaturesetByPortalItem(p, "b0d335151aad48a5883326b9aed69cdd", 1, ["*"], false),
  FeaturesetByPortalItem(p, "b0d335151aad48a5883326b9aed69cdd", 2, ["*"], false),
]

// create the output featureset (this assumes that all layers have the same fields)
var merged_fs = {
  geometryType: "",
  fields: Schema(featuresets[0]).fields,
  features: []
}

// append every feature of every input layer to the output featureset
for(var fs in featuresets) {
  for(var f in featuresets[fs]) {
    var att = Dictionary(f).attributes
    Push(merged_fs.features, {attributes: att})
  }
}

// and return that featureset
return Featureset(merged_fs)

 

For the indicators, it's best to make that a separate question.


Have a great day!
Johannes

View solution in original post

JohannesLindner
MVP Frequent Contributor

If your company has its own Portal, you need to supply its URL in line 2. If you're working in AGOL, the URL should be correct.

Replace lines 4-6 with the layers you want to merge (and add more if applicable). The function we use to load layers in dashboards is FeaturesetByPortalItem. Its arguments are

  • the portal where the layer is stored (we create that variable in line 2)
  • the item id of the feature layer or service, you can find that in the service's URL:
    JohannesLindner_1-1690498544640.png

     

  • the layer id, which you can find in the layer's URL:
    JohannesLindner_2-1690498604089.png
  • the fields you want to load, the asterisk is a placeholder for "load all fields"
  • if you want to load geometries (slower, not necessary here)

 


Have a great day!
Johannes

View solution in original post

5 Replies
JohannesLindner
MVP Frequent Contributor

Assuming all your layers have the same fields, you should be able to merge them with a data expression:

// load your layers
var p = Portal("https://www.arcgis.com")
var featuresets = [
  FeaturesetByPortalItem(p, "b0d335151aad48a5883326b9aed69cdd", 0, ["*"], false),
  FeaturesetByPortalItem(p, "b0d335151aad48a5883326b9aed69cdd", 1, ["*"], false),
  FeaturesetByPortalItem(p, "b0d335151aad48a5883326b9aed69cdd", 2, ["*"], false),
]

// create the output featureset (this assumes that all layers have the same fields)
var merged_fs = {
  geometryType: "",
  fields: Schema(featuresets[0]).fields,
  features: []
}

// append every feature of every input layer to the output featureset
for(var fs in featuresets) {
  for(var f in featuresets[fs]) {
    var att = Dictionary(f).attributes
    Push(merged_fs.features, {attributes: att})
  }
}

// and return that featureset
return Featureset(merged_fs)

 

For the indicators, it's best to make that a separate question.


Have a great day!
Johannes
Matt-Barrett
Regular Contributor

Wow!  That's great!  So do I need to add my layer and field names in there somewhere?  None of that looks like anything I'm familiar with.

Thank you!

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

If your company has its own Portal, you need to supply its URL in line 2. If you're working in AGOL, the URL should be correct.

Replace lines 4-6 with the layers you want to merge (and add more if applicable). The function we use to load layers in dashboards is FeaturesetByPortalItem. Its arguments are

  • the portal where the layer is stored (we create that variable in line 2)
  • the item id of the feature layer or service, you can find that in the service's URL:
    JohannesLindner_1-1690498544640.png

     

  • the layer id, which you can find in the layer's URL:
    JohannesLindner_2-1690498604089.png
  • the fields you want to load, the asterisk is a placeholder for "load all fields"
  • if you want to load geometries (slower, not necessary here)

 


Have a great day!
Johannes
Matt-Barrett
Regular Contributor

Excellent!  This was exactly what I needed!  It worked, but then I realized I needed to add a field to each layer so I can split them by FY in the chart.  So I went back to ArcGIS Pro and did that, and the new field is the same in both layers, except that in one layer the values are "2022" and in the other layer the values are "2023".  But in the combined layer chart it seems to only be displaying the 2023 values.  So I'm not sure what's going on there.  I'll keep playing with it though.  Thanks for all your help!

0 Kudos
rVeloz
by
Occasional Contributor

Good evening. Thanks for the script; i was able to (partially) use it. I'm trying to merge 3 featurelayers, but the output only includes 2. I have two same schemas and one slightly different schema. I see in your instructions you mention that this script will work if the schema is the same in all layers. What changes, if any, would i make in the 'var merged_fs' section to accomodate the different schemas? Thanks in advance.

0 Kudos