I'm using leaflet and markercluster.
I display on a leaflet map with thousands of markers and I use MarkerCluster to create clusters. It's work very good. Now i want to replace the icon to get pie charts as the example here
So I overload the function which creates the icon:
var markerCluster = new L.MarkerClusterGroup({
showCoverageOnHover: false, spiderfyOnMaxZoom: true, zoomToBoundsOnClick: true,
iconCreateFunction: defineClusterIcon
});
I can't adapt the code is the link because i don't use geojson data, my markers are get from ajax calls.
What i want to do is to get a simple pie chart for each clusters with 3 parts for 'Botanique', 'Zoologie' and 'Paleontologie'.
So for a cluster i get the childs. for each child I only can get the iconUrl link and count each 'Botanique', 'Zoologie' and 'Paleontologie'.
I declare the iconCreateFunction():
function defineClusterIcon(cluster) {
var children = cluster.getAllChildMarkers();
var bcount = 0,
zcount = 0,
pcount = 0 ;
for(var i in children){
var child = children[i];
switch ( child.options.icon.options.iconUrl ){
case 'resources/vendors/leaflet/images/marker-icon-bota.png' :
hcount ++; break ;
case 'resources/vendors/leaflet/images/marker-icon-paleon.png' :
pcount ++; break ;
case 'resources/vendors/leaflet/images/marker-icon-zoo.png' :
zcount ++; break ;
}
}
var data = {
'Botanique' : hcount ,
'Zoologie' : zcount ,
'Paleontologie' : pcount
};
//bake some svg markup
var html = bakeThePie(data);
//Create a new divIcon and assign the svg markup to the html property
myIcon = new L.DivIcon({
html: html,
className: 'marker-cluster',
iconSize: new L.Point(iconDim, iconDim)
});
return myIcon;
}
Is there a simple way to create the bakeThePie() function who return an svg ?
All the library I found append the svg directly in a div with a given id.