format numbers in Javascript legend

1518
4
09-12-2013 10:25 AM
MaraKaminowitz
Occasional Contributor
I am working on a web map based on the javascript sample here:
http://help.arcgis.com/EN/webapi/javascript/arcgis/jssamples/renderer_generate_renderer.html

The numbers in my fields are all decimals with 1-2 decimal places, and the legend always adds extra zero's to the end. You can see this behavior in their live sample as well - select "Population/Square Mile(2007)" on the map at http://help.arcgis.com/EN/webapi/javascript/arcgis/samples/renderer_generate_renderer/

I would like to round my number off and remove all the extra zero's, but I cannot figure out how. I tried modifying the script but I can't find a way to do it.  I rounded the number in the web service (I published the service so I can edit the original). I also tried to change the original field parameters to limit the number of decimals. I am stumped.

Coding is not my strong point.  Does anyone know of a way I can shorten the number of decimals to make the legend appear neater?
0 Kudos
4 Replies
MaraKaminowitz
Occasional Contributor
I am working on a web map based on the javascript sample here:
http://help.arcgis.com/EN/webapi/javascript/arcgis/jssamples/renderer_generate_renderer.html

The numbers in my fields are all decimals with 1-2 decimal places, and the legend always adds extra zero's to the end. You can see this behavior in their live sample as well - select "Population/Square Mile(2007)" on the map at http://help.arcgis.com/EN/webapi/javascript/arcgis/samples/renderer_generate_renderer/

I would like to round my number off and remove all the extra zero's, but I cannot figure out how. I tried modifying the script but I can't find a way to do it.  I rounded the number in the web service (I published the service so I can edit the original). I tried to change the original field parameters to limit the number of decimals. I am stumped.

Coding is not my strong point.  Does anyone know of a way I can shorten the number of decimals to make the legend appear neater?
0 Kudos
MaraKaminowitz
Occasional Contributor
I am working on a web map based on the javascript sample here:
http://help.arcgis.com/EN/webapi/javascript/arcgis/jssamples/renderer_generate_renderer.html

The numbers in my fields are all decimals with 1-2 decimal places, and the legend always adds extra zero's to the end. You can see this behavior in their live sample as well - select "Population/Square Mile(2007)" on the map at http://help.arcgis.com/EN/webapi/javascript/arcgis/samples/renderer_generate_renderer/

I would like to round my number off and remove all the extra zero's, but I cannot figure out how. I tried modifying the script but I can't find a way to do it.  I rounded the number in the web service (I published the service so I can edit the original). I tried to change the original field parameters to limit the number of decimals. I am stumped.

Coding is not my strong point.  Does anyone know of a way I can shorten the number of decimals to make the legend appear neater?
0 Kudos
JianHuang
Occasional Contributor III
Posting it 3 times really makes it urgent. 🙂
After the call to generateRenderer completes, you get the renderer and you can manipulate it before applying it to the layer. Here is a code snippet. It's probably more than you asked. You can ignore minValue and maxValue for your case.
Hope this helps.

function _processRenderer(renderer, prefix, unitLabel, formatLabel, precision, minValue, maxValue){
      if (renderer.declaredClass === "esri.renderer.ClassBreaksRenderer") {
      array.forEach(renderer.infos, function (item, idx) {
        if (idx === 0 && minValue !== undefined && minValue !== null) {
          item.minValue = minValue;
        }
        if (idx === renderer.infos.length - 1 && maxValue !== undefined && maxValue !== null) {
          item.classMaxValue = item.maxValue = maxValue;
        }
        
        if (precision) {
          item.classMaxValue = item.maxValue = Math.round(item.maxValue / precision) * precision;
          item.minValue = Math.round(item.minValue / precision) * precision;
        }
        
        if (formatLabel) {          
          item.label = number.format(item.minValue) + " - " + number.format(item.maxValue);
        }        
        
     if (prefix) {
       item.label = prefix + " " + item.label;
     }
        
        if (unitLabel) {
       item.label = item.label + " " + unitLabel;
     }
      });
      }
      else {
      array.forEach(renderer.infos, function (item, idx) {
        if (idx === 0 && minValue !== undefined && minValue !== null) {
          item.value = minValue;
        }
        if (idx === renderer.infos.length - 1 && maxValue !== undefined && maxValue !== null) {
          item.value = maxValue;
        }
        
        if (formatLabel) {
          item.label = number.format(item.value);
        }        
        
     if (prefix) {
       item.label = prefix + " " + item.label;
     }
        
        if (unitLabel) {
       item.label = item.label + " " + unitLabel;
     }
      });
      }
      return renderer;
    }
0 Kudos
MaraKaminowitz
Occasional Contributor
Sorry about that, I don't know how that happened. Thanks for the code, I'll give it a try.
0 Kudos