how to create a legend for GraphicLayer Dynamically with arcgis js api

1803
7
08-06-2011 01:17 AM
billchuang
New Contributor
Hi,
i have a trouble for a few days,I generalize a isosurface by submit a job to a GP Service,when i get the isosurface from the server,I add them to a GraphicLayer,now I want to create a legend for the graphiclayer,but i have no idea,is there anyone help me?
now below is my code partly:
function addIsosurface(outputFeat)
{
var symbol = new esri.symbol.SimpleFillSymbol();
    symbol.setColor(new dojo.Color([150, 150, 150, 0.5]));

    var renderer = new esri.renderer.ClassBreaksRenderer(symbol, "GRIDCODE");
    renderer.addBreak(0, 26, new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([56, 168, 0, 1])));
    renderer.addBreak(26, 28, new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([139, 209, 0, 1])));
    renderer.addBreak(28, 30, new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([255, 255, 0, 1])));
    renderer.addBreak(30, Infinity, new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([255, 0, 0, 1])));
   
    var features = outputFeat.value.features;
    var isoLayer = new esri.layers.GraphicsLayer();
    dojo.forEach(features,function(feature)
    {
     isoLayer.add(feature);
    })
    isoLayer.setRenderer(renderer);
    map.addLayer(mapLayer);
    dojo.connect(map,'onLayersAddResult',function(results)
    {
     var layerInfo = dojo.map(results, function(layer,index){
         return {layer:layer.layer,title:'temperature'};
       });
       if(layerInfo.length > 0){
         var legendDijit = new esri.dijit.Legend({
            map:map,
            layerInfos:layerInfo
         },"legendDiv");
         legendDijit.startup();
       }
    });
    map.addLayers([isoLayer]);
}
0 Kudos
7 Replies
HemingZhu
Occasional Contributor III
Hi,
i have a trouble for a few days,I generalize a isosurface by submit a job to a GP Service,when i get the isosurface from the server,I add them to a GraphicLayer,now I want to create a legend for the graphiclayer,but i have no idea,is there anyone help me?
now below is my code partly:
function addIsosurface(outputFeat)
{
var symbol = new esri.symbol.SimpleFillSymbol();
    symbol.setColor(new dojo.Color([150, 150, 150, 0.5]));

    var renderer = new esri.renderer.ClassBreaksRenderer(symbol, "GRIDCODE");
    renderer.addBreak(0, 26, new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([56, 168, 0, 1])));
    renderer.addBreak(26, 28, new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([139, 209, 0, 1])));
    renderer.addBreak(28, 30, new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([255, 255, 0, 1])));
    renderer.addBreak(30, Infinity, new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([255, 0, 0, 1])));
   
    var features = outputFeat.value.features;
    var isoLayer = new esri.layers.GraphicsLayer();
    dojo.forEach(features,function(feature)
    {
     isoLayer.add(feature);
    })
    isoLayer.setRenderer(renderer);
    map.addLayer(mapLayer);
    dojo.connect(map,'onLayersAddResult',function(results)
    {
     var layerInfo = dojo.map(results, function(layer,index){
         return {layer:layer.layer,title:'temperature'};
       });
       if(layerInfo.length > 0){
         var legendDijit = new esri.dijit.Legend({
            map:map,
            layerInfos:layerInfo
         },"legendDiv");
         legendDijit.startup();
       }
    });
    map.addLayers([isoLayer]);
}


Have you thought about adding your GP result as a layer on your .xmd (being a result map service)? If you add your GP to your .mxd as a layer and then add GP result as a layer at the same .mxd. Thay way you can define symbology for your result layer in .mxd and do not have to add code logic to symbolize. There are docs on ESRI online show how.
0 Kudos
billchuang
New Contributor
Have you thought about adding your GP result as a layer on your .xmd (being a result map service)? If you add your GP to your .mxd as a layer and then add GP result as a layer at the same .mxd. Thay way you can define symbology for your result layer in .mxd and do not have to add code logic to symbolize. There are docs on ESRI online show how.


Thanks for your reply.I have taken your advice,but after I publish a result map service,nothing is displayed in the map.when the job complete I use the "getResultImageLayer" to get the image layer like this:
function completeCallback(jobInfo)
{
if(jobInfo.jobStatus != "esriJobFailed")
{
  //isosurfaceGP.getResultData(jobInfo.jobId,"Output_Polygon", addIsosurface);
  imgParam = new esri.layers.ImageParameters();
  imgParam.format = "PNG24";
  imgParam.imageSpatialReference = map.spatialReference;
  imgParam.layerDefinition = ["GRIDCODE"];
 
  isosurfaceGP.getResultImageLayer(jobInfo.jobId,"Output_Polygon",imgParam,function(gpLayer)
  {
   map.addLayer(gpLayer);
  });
}
}

when I try the job in the rest,though the job is displayed "succeed",but I can't see the result,it displayed :
"Data Type: GPFeatureRecordSetLayer
Unable to get tool layers for task 'IsosurfaceAnalyze' and parameter 'Output_Polygon'
Supported Interfaces:   REST
"
I'm so confused.Can I ask your help?
0 Kudos
HemingZhu
Occasional Contributor III
Thanks for your reply.I have taken your advice,but after I publish a result map service,nothing is displayed in the map.when the job complete I use the "getResultImageLayer" to get the image layer like this:
function completeCallback(jobInfo)
{
if(jobInfo.jobStatus != "esriJobFailed")
{
  //isosurfaceGP.getResultData(jobInfo.jobId,"Output_Polygon", addIsosurface);
  imgParam = new esri.layers.ImageParameters();
  imgParam.format = "PNG24";
  imgParam.imageSpatialReference = map.spatialReference;
  imgParam.layerDefinition = ["GRIDCODE"];
 
  isosurfaceGP.getResultImageLayer(jobInfo.jobId,"Output_Polygon",imgParam,function(gpLayer)
  {
   map.addLayer(gpLayer);
  });
}
}

when I try the job in the rest,though the job is displayed "succeed",but I can't see the result,it displayed :
"Data Type: GPFeatureRecordSetLayer
Unable to get tool layers for task 'IsosurfaceAnalyze' and parameter 'Output_Polygon'
Supported Interfaces:   REST
"
I'm so confused.Can I ask your help?


One thing you can try is to put this layer on the result .mxd. In your GP Model, set the source at Your tool folder\Scratch\scratch.gdb. What happen is when you run GP on the server, ArcGIS server will create scratch.gdb at \\arcgisserver\arcgisjobs\jobid\scratch\. So the result layer will guarantee to be put in that .gdb.
0 Kudos
billchuang
New Contributor
One thing you can try is to put this layer on the result .mxd. In your GP Model, set the source at Your tool folder\Scratch\scratch.gdb. What happen is when you run GP on the server, ArcGIS server will create scratch.gdb at \\arcgisserver\arcgisjobs\jobid\scratch\. So the result layer will guarantee to be put in that .gdb.


Excuse me for my fool,as I'm a newer,I don't understand you still.I didn't do that before.
Can I ask your MSN or other?Pardon me my clumsiness.I want to learn more from you and make friend with you.
My MSN is bill_chuang@LIVE.CN
0 Kudos
HemingZhu
Occasional Contributor III
Excuse me for my fool,as I'm a newer,I don't understand you still.I didn't do that before.
Can I ask your MSN or other?Pardon me my clumsiness.I want to learn more from you and make friend with you.
My MSN is bill_chuang@LIVE.CN


There is an online help on  how to GP on Server. http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/An_overview_of_geoprocessing_with_ArcG.... They are good articles for newer. You probably will understand them better than what i said here. I found out reading online help is a good way of learning JS API or Other GIS stuff. You are welcome to contact me to discuss GIS issues. My Email is zhuheming@hotmail.com. (thought I log on Forum more times than i check my email -full of junk mails).
0 Kudos
billchuang
New Contributor
There is an online help on  how to GP on Server. http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/An_overview_of_geoprocessing_with_ArcG.... They are good articles for newer. You probably will understand them better than what i said here. I found out reading online help is a good way of learning JS API or Other GIS stuff. You are welcome to contact me to discuss GIS issues. My Email is zhuheming@hotmail.com. (thought I log on Forum more times than i check my email -full of junk mails).

Thanks for your reply.I think your advice is good.If I don't understanding somewhere,Can i ask your help again?^_^
0 Kudos
HemingZhu
Occasional Contributor III
Thanks for your reply.I think your advice is good.If I don't understanding somewhere,Can i ask your help again?^_^


No problem.
0 Kudos