简体   繁体   中英

Leaflet polygon display charts after click

I am new to Leaflet and I have a map on which I imported several polygons. The data of my polygons is stored in a.js file.
Now I am looking to display simple chart when I click on a polygon.
My idea is to use the data (text and numeric) available with the polygon outline data or in a separate file.
I want to display the charts outside of my leaflet map. They will always be the same charts, only the data will change when you click on a different polygon.
I saw that I could use D3.js or Chart.js for charts.
My problem is to link the click on a polygon of my map with the display of the correct graphic.

EDIT: This is the code I use for the moment

<script type="text/javascript" src="file.js"></script>
<script type="text/javascript" src="file2.js"></script>

<script type="text/javascript">

    var map = L.map('map').setView([39.0, -98.26], 10);

    var osmLayer = L.tileLayer('https://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png', {
        maxZoom: 18,
        attribution: '© <a href= »http://osm.org/copyright »>OpenStreetMap</a> contributors',
        tileSize: 512,
        zoomOffset: -1
    }).addTo(map);
    


    function getColor(d) {
        return d > 1000 ? '#800026' :
                d > 500  ? '#BD0026' :
                d > 200  ? '#E31A1C' :
                d > 100  ? '#FC4E2A' :
                d > 50   ? '#FD8D3C' :
                d > 20   ? '#FEB24C' :
                d > 10   ? '#FED976' :
                            '#FFEDA0';
    }

    function style(feature) {
        return {
            weight: 2,
            opacity: 1,
            color: 'black',
            fillOpacity: 0.7,
            fillColor: getColor(feature.properties.density)
        };
    }


    var country = L.geoJson(data, {
        style: style,
    }).addTo(map);

    
    var small= L.geoJson(data2, {
        style: style,
    }).addTo(map);

    var baseMaps = {
      "OSM": osmLayer
      
    };

    var overlayMaps = {
        "Country": country,
        "Small": small
     
    };
 
    L.control.layers(baseMaps, overlayMaps).addTo(map);
</script>

And in my 2.js files there is data like:

var data={"type":"FeatureCollection","features":[
{"type":"Feature","properties":{"Zip":"92662","Name":"Paris","density":500},"geometry":{"type":"MultiPolygon","coordinates":xxx ,
]};

I have done a lot of research but not sure if it is possible or how to display a chart based on the clicked polygon (not a chart in a pop up). I need help knowing what to look for.

Thank you for your help

You probably want to use onEachFeature

function countryFunc(feature, layer){
  layer.on('click', function(){
    // console.log(feature);
    /* get your chart now */
  });
}
const country = L.geoJson(data, {style: style, onEachFeature:countryFunc}).addTo(map);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM