Select to view content in your preferred language

Legend for Graphics Layer

9175
15
Jump to solution
02-21-2012 10:50 AM
SaraSecunda
Emerging Contributor
Hello,

I have a graphics layer with a classbreaks renderer.  The breaks are computed mathematically based on the attribute values.  How can I show a legend for this graphics layer?

Thanks.

Sara S.
0 Kudos
15 Replies
derekswingley1
Deactivated User
Two things:
â??it sounds like you're using Internet Explorer. If that's the case, be sure to generalize your features using maxAllowableOffset. This results in less JSON sent to the client and is easier for IE to use. Other browsers are less likely to have this problem.
â??the reason you see one feature is that your feature layer does not have an ObjectID field.

As I said in my previous post, we don't have a sample that shows this but I posted code that shows how to do it. Here's the relevant code:
var features = [];
dojo.forEach(response.items, function (item, idx) { // idx is used as the object id
  var attr = {};
  attr["description"] = item.description;
  attr["title"] = item.title ? item.title : "Flickr Photo";
  attr["ObjectID"] = idx; // populate the feature's object id

  var geometry = esri.geometry.geographicToWebMercator(new esri.geometry.Point(item.longitude, item.latitude, map.spatialReference));

  var graphic = new esri.Graphic(geometry);
  graphic.setAttributes(attr);
  features.push(graphic);
});
0 Kudos
SaraSecunda
Emerging Contributor
Derek,

It's working!  Thank you!
0 Kudos
derekswingley1
Deactivated User
Nice, glad to help. Can you mark one of my posts as answer?
0 Kudos
JenniferGaa
Occasional Contributor
nliu's recommendation is the path you should take- create a feature layer from a feature collection and you'll be able to use that with the legend dijit.


What would the layerInfo look like for a feature layer created from a feature collection - the layerInfo the Legend dijit expects.  I am trying to use just the feature layer itself.. is this correct?  Thanks!

When I forked and updated your example on jsFiddle to include a legend (see http://jsfiddle.net/ynm6W/2/), the legend content is "No legend"... so how do you/would you use the legend dijit if the FeatureLayer is based on graphics?

According to the documentation for the Legend dijit: "The legend supports the following layer types: ArcGISDynamicMapServiceLayer, ArcGISTiledMapServiceLayer, FeatureLayer and KMLLayer."  However then according to the constructor detail the layerinfos are: "A layer to add to the legend. Valid layer types are: ArcGISTiledMapServiceLayer,ArcGISDynamicMapServiceLayer."  So this means the FeatureLayer must be created from one of these two map service types to use the Legend dijit and a FeatureLayer from a feature collection would not be supported?
0 Kudos
derekswingley1
Deactivated User
Couple things...
�??dojo.require the legend dijit
�??layerInfos in the Legend constructor is an array of objects, not a single object
�??you were missing a closing curly brace and a comma in the Legend constructor

Try this:  http://jsfiddle.net/6R3T8/
0 Kudos
JenniferGaa
Occasional Contributor
Thanks Derek!  In my actual code I just needed to add the [] around the layerInfo variable and it works now.  🙂
0 Kudos