TransWikia.com

Avoiding overlapping charts on a map with Leaflet?

Geographic Information Systems Asked by bocquet.wagner on July 16, 2021

I’m currently developing an online GIS with Leaflet and need to plot bar charts on a map. I’m using leaflet.minichart (https://rte-antares-rpackage.github.io/leaflet.minichart/) for the charts.
My problem is that some of the bar charts are overlapping – usually due the proximity of the centroids of my polygons. You can see a good example in the attached printscreen on the darkest polygon.
I would like to avoid such overlaps and at this stage I’m fairly agnostic to the type of solution – I just want to ensure a nice rendering along with user-friendliness. Here below my code:

function add_poly_charts(features){
            var feature; // contains the polygon centroid and name, while polygon_charts is an array containing the data for each polygon

            // size simply changing with the number of charts, this si what we woudl like to automate to avoid collisions
            var size=80;
            if(n_poly_charts>5){
                size=30;
            }

            for (var i=0; i< features.features.length; i++){
                feature=features.features[i];
                var chart=L.minichart([feature.properties.centroid_y,feature.properties.centroid_x],
                                      {data: polygon_charts[feature.properties.adm],type:"bar",height:size,width:size, labels: names_list,
                                      colors:chart_colors});

                poly_charts.push(chart);
                mymap.addLayer(chart);
            }

        }

Map developed with Leaflet and overlapping charts developed with leaflet.minichart

One Answer

Problem you are having is usually solved by method called marker clustering. This means that if several markers/icons are so close together that they overlap, they are replaced by symbolic cluster icon, which usually displays also number of included markers. Upon clicking on this cluster icon, all contained icons are shown in a circle around cluster icon, connected to it with lines (spiderfiering).

In your case you are using Leaflet.minichart plugin which does not offer this functionality. It's objects (charts) are extensions of Leaflet L.CircleMarker object, which is special case of vector layer. This means that also none of the marker clustering solutions (like Leaflet.markercluster plugin) can be used in combination with Leaflet.minichart plugin.

So, in short, you are out of luck here for out of the box solution. Programing custom solution? A lot of work, you can have a look at one of the clustering plugins code.

Answered by TomazicM on July 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP