[英]Leaflet polygon display charts after click
I am new to Leaflet and I have a map on which I imported several polygons.我是 Leaflet 的新手,我有一个 map,我在上面导入了几个多边形。 The data of my polygons is stored in a.js file.
我的多边形数据存储在一个 .js 文件中。
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.我想在我的 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.我看到我可以将 D3.js 或 Chart.js 用于图表。
My problem is to link the click on a polygon of my map with the display of the correct graphic.我的问题是将点击我的 map 的多边形与正确图形的显示联系起来。
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:在我的 2.js 文件中有如下数据:
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您可能想使用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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.