Select to view content in your preferred language

JS API: How to Update the Legend when changing Renderer

4289
7
Jump to solution
07-07-2016 01:27 PM
BatesRambow
Regular Contributor

I have an application I'm building with version 3.16 of the API.  One of the features is the ability to switch between 3 different symbology sets.  Each of these 3 is created using the UniqueValueRenderer and updating the FeatureLayer's renderer when a specific option is chosen.  Works great.

The problem I'm having is that I want the legend in the application to update when a new renderer is applied, but I haven't been able to figure it out.

I've tried:

app.legend.refresh();

I've also tried writing and calling a function that should destroy any existing legend and create a new one:

  function updateLegend(map, fl) {
    // destroy previous legend, if present
    if ( app.hasOwnProperty("legend") ) {
      app.legend.destroy();
      domConstruct.destroy(dojo.byId("legend"));
    }
    // create a new div for the legend
    var legendDiv = domConstruct.create("div", {
      id: "legend"
    }, dom.byId("legendWrapper"));

    app.legend = new Legend({
      map : map,
      layerInfos : [{
        layer : fl,
        title: 'Stormwater Service Requests'
      }]
    }, legendDiv);
    app.legend.startup();
  }

But neither of these updates the legend.  In fact, all I get is a message where the legend should be that says "Creating Legend..."

If I have my browser console open, an error is thrown (with both of these options):

Object doesn't support property or method 'replace'

File: 3.16, Line:1715, Column: 514

It looks like the error is being thrown in the API code, not the code for my app.  Does anyone have any insight on what this error might be, and what I can do to get the legend to update properly?

0 Kudos
1 Solution

Accepted Solutions
BatesRambow
Regular Contributor

I figured this out in case anyone else has this issue.  The problem was that I was not setting a 'label' value for each of the UniqueValues in my renderer.  It isn't totally obvious, but that 'label' value is required for the legend to display.

View solution in original post

7 Replies
FC_Basson
MVP Regular Contributor

It should not be necessary to rebuild the legend every time.  Try setting the autoUpdate property to true and run the refresh() after the layer renderer change.

0 Kudos
BatesRambow
Regular Contributor

Thanks, but that doesn't change anything.

0 Kudos
FC_Basson
MVP Regular Contributor

So the legend works when you initiate the application with any of the 3 symbology sets?  And you are not forcing the legend to update before the layer has completely updated the symbology renderer?

0 Kudos
BatesRambow
Regular Contributor

Thanks for following up, I figured it out.  I wasn't setting the 'label' parameter in the renderer.  After adding that, it worked.

0 Kudos
TimWitt2
MVP Alum

I might be wrong, but the legend widget is getting the symbology from the rest, so when you change the symbology in your app, it won't show a change in the legend.

0 Kudos
FC_Basson
MVP Regular Contributor

It does, but not if you apply a renderer.

BatesRambow
Regular Contributor

I figured this out in case anyone else has this issue.  The problem was that I was not setting a 'label' value for each of the UniqueValues in my renderer.  It isn't totally obvious, but that 'label' value is required for the legend to display.