Opened 8 years ago

Closed 8 years ago

#174 closed enhancement (fixed)

add legend for vector layers

Reported by: bartvde Owned by: elemoine
Priority: major Milestone: 0.7
Component: GeoExt.LegendPanel Version: trunk
Keywords: Cc:
State: Commit


Attachments (2)

174.patch (34.6 KB) - added by tschaub 8 years ago.
vector legend with example and tests (539 bytes) - added by elemoine 8 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 Changed 8 years ago by tschaub

  • Owner set to tschaub
  • Status changed from new to assigned

I'll take this on (with components from Styler) unless someone else has a start on it.

comment:2 Changed 8 years ago by bartvde

Hey Tim, thanks in advance. I had a start on it (using your excellent FeatureRenderer class), but one thing I ran into was setting the symbolType on the FeatureRenderer. In my application the legend would be instantiated before my vector layers had features, so I could not determine the symbolType from the data, and set it on the Layer object instead. This is not ideal though. Hope to hear your solution on that.

This was my code up until now:

 * Copyright (c) 2008-2009 The Open Source Geospatial Foundation
 * Published under the BSD license.
 * See for the full text
 * of the license.
 * @include GeoExt/widgets/FeatureRenderer.js

/** api: (define)
 *  module = GeoExt
 *  class = LegendVector
 *  base_link = `Ext.Panel <>`_

/** api: constructor
 *  .. class:: LegendVector(config)
 *  Show a legend image for a Vector layer.
GeoExt.LegendVector = Ext.extend(Ext.Panel, {

    /** api: config[layer]
     *  ``OpenLayers.Layer.Vector``
     *  The vector layer to create the legend for. Not required if record is
     *  provided.
    layer: null,
    /** api: config[record]
     *  ````
     *  Optional record containing the layer.
    record: null,

    /** api: config[bodyBorder]
     *  ``Boolean``
     *  Show a border around the legend image or not. Default is false.
    bodyBorder: false,     

    /** private: method[initComponent]
     *  Initializes the Vector legend.
    initComponent: function() {;
        if(!this.layer) {
            this.layer = this.record.get("layer");
        this.add(new GeoExt.FeatureRenderer({
            symbolizer: this.layer.styleMap.styles["default"].defaultStyle,
            symbolType: (this.layer.symbolType) ? this.layer.symbolType: 'Point'

comment:3 Changed 8 years ago by tschaub

Putting up an initial patch just so others can comment. This depends on (and includes the patch for) #175.

This is also going to require some changes to the LegendPanel. I'm trying to keep those to a minimum, but we might want to redesign that a bit. It currently assumes layer.CLASS_NAME will be useful for determining the legend type. This doesn't work well for multiple vector layer subclasses that use one legend component. The only change I've included in this first patch is to add a getLegendType method to the legend panel. This comes with a legendTypes property. It is still more brittle than it could be. If legend components were a sublcass of some common thing, we could more easily register them and have a common interface that the legend panel would expect.

Also, the legend panel currently assumes that legend components will have an updateLegend method that takes a URL string. This is not going to be useful for the vector legend. I think it would work out better if legend components simply had update (or it could be updateLegend) and could figure out how to update themselves if called with no arguments (the WMS legend component can get the URL from the record).

Anyway, more to come later.

comment:4 Changed 8 years ago by tschaub

I'll keep an example updated in the opengeo/style sandbox.

comment:5 Changed 8 years ago by bartvde

Tim, is this ready for review?

comment:6 Changed 8 years ago by tschaub

  • State changed from None to Review

I'm kicking this to review because we're approaching the 17th cutoff. It still needs tests, but the example demonstrates it in action. I'll try to work in some tests tonight.

Changed 8 years ago by tschaub

vector legend with example and tests

comment:7 Changed 8 years ago by tschaub

Patch updated to include tests. Example works in FF3, IE8, and IE6. Tests are passing in FF3 and IE8. I'm looking into IE6 test issue now.

comment:8 Changed 8 years ago by tschaub

I mis-spoke. Tests are passing and example is working in FF3, IE8, and IE6 (and Safari 4). Thanks for any review.

comment:9 Changed 8 years ago by elemoine

  • Owner changed from tschaub to elemoine
  • Status changed from assigned to new

Changed 8 years ago by elemoine

comment:10 Changed 8 years ago by elemoine

  • State changed from Review to Commit

Good patch. See for a correction to a typo in the API doc. Please commit.

comment:11 Changed 8 years ago by tschaub

  • Resolution set to fixed
  • Status changed from new to closed

(In [1999]) Adding a legend component for vector layers. This can be constructed with a vector layer or a list of rules and a symbolizer type. To use a legend panel without entries for your vector layers, set layer.displayInLayerSwitcher to false. r=elemoine closes #174

Note: See TracTickets for help on using tickets.