From 7318ebd2f9a41bc84fb6bd8cabc81da2cba1267d Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Thu, 12 Oct 2023 10:06:19 +0200 Subject: [PATCH 1/2] choropleth: be more defensive when consuming breaks --- umap/static/umap/js/umap.layer.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/umap/static/umap/js/umap.layer.js b/umap/static/umap/js/umap.layer.js index 9525c217..084e93a5 100644 --- a/umap/static/umap/js/umap.layer.js +++ b/umap/static/umap/js/umap.layer.js @@ -161,7 +161,7 @@ L.U.Layer.Choropleth = L.FeatureGroup.extend({ } let mode = this.datalayer.options.choropleth.mode, classes = +this.datalayer.options.choropleth.classes || 5, - breaks = [] + breaks if (mode === 'manual') { const manualBreaks = this.datalayer.options.choropleth.breaks if (manualBreaks) { @@ -178,12 +178,12 @@ L.U.Layer.Choropleth = L.FeatureGroup.extend({ breaks = ss.ckmeans(values, classes).map((cluster) => cluster[0]) breaks.push(ss.max(values)) // Needed for computing the legend } - this.options.breaks = breaks + this.options.breaks = breaks || [] this.datalayer.options.choropleth.breaks = this.options.breaks.map(b => +b.toFixed(2)).join(',') const fillColor = this.datalayer.getOption('fillColor') || this.defaults.fillColor let colorScheme = this.datalayer.options.choropleth.brewer if (!colorbrewer[colorScheme]) colorScheme = 'Blues' - this.options.colors = colorbrewer[colorScheme][breaks.length - 1] || [] + this.options.colors = colorbrewer[colorScheme][this.options.breaks.length - 1] || [] }, getColor: function (feature) { From 8fdddddc9ea932c3fa3cf6ab466a63168d3f19d4 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Thu, 12 Oct 2023 10:07:51 +0200 Subject: [PATCH 2/2] Prevent computing choropleth breaks for each feature when importing When importing a choropleth layer, the layer does not have a umap_id, thus "hasDataLoaded" is immediately true, instead of being true when all the "known" data is imported. At this point, I can't find a good reason to keep the check on umap_id, so let's remove and move forward. --- umap/static/umap/js/umap.layer.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/umap/static/umap/js/umap.layer.js b/umap/static/umap/js/umap.layer.js index 084e93a5..accc9a90 100644 --- a/umap/static/umap/js/umap.layer.js +++ b/umap/static/umap/js/umap.layer.js @@ -135,7 +135,10 @@ L.U.Layer.Choropleth = L.FeatureGroup.extend({ [], this.datalayer.options.choropleth ) - this.datalayer.on('datachanged', this.redraw, this) + this.datalayer.onceDataLoaded(() => { + this.redraw() + this.datalayer.on('datachanged', this.redraw, this) + }) }, redraw: function () { @@ -717,7 +720,7 @@ L.U.DataLayer = L.Evented.extend({ }, hasDataLoaded: function () { - return !this.umap_id || this._dataloaded + return this._dataloaded }, setUmapId: function (id) {