From 35758a1e139105d4cc03d9019e2168f70e3c8007 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Mon, 9 Oct 2023 15:36:55 +0200 Subject: [PATCH] Choropleth: do not add layer before knowing the whole dataset And only compute limits once. --- umap/static/umap/js/umap.layer.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/umap/static/umap/js/umap.layer.js b/umap/static/umap/js/umap.layer.js index 79bd184e..e509d268 100644 --- a/umap/static/umap/js/umap.layer.js +++ b/umap/static/umap/js/umap.layer.js @@ -128,6 +128,12 @@ L.U.Layer.Choropleth = L.FeatureGroup.extend({ [], this.datalayer.options.choropleth ) + this.datalayer.on('datachanged', this.redraw, this) + }, + + redraw: function () { + this.computeLimits() + if (this._map) this.eachLayer(this._map.addLayer, this._map) }, _getValue: function (feature) { @@ -165,13 +171,17 @@ L.U.Layer.Choropleth = L.FeatureGroup.extend({ }, addLayer: function (layer) { - this.computeLimits() - L.FeatureGroup.prototype.addLayer.call(this, layer) + // Do not add yet the layer to the map + // wait for datachanged event, so we want compute limits once + var id = this.getLayerId(layer); + this._layers[id] = layer; + return this; }, removeLayer: function (layer) { - this.computeLimits() - L.FeatureGroup.prototype.removeLayer.call(this, layer) + var id = layer in this._layers ? layer : this.getLayerId(layer); + delete this._layers[id]; + return this; }, onAdd: function (map) {