Skip to content

Instantly share code, notes, and snippets.

Last active April 11, 2024 18:52
Show Gist options
  • Save gisminister/10001728 to your computer and use it in GitHub Desktop.
Save gisminister/10001728 to your computer and use it in GitHub Desktop.
Markercluster pie charts

The map shows traffic accidents recorded in Oslo, Norway, for the year 2013.

The Leaflet Markercluster plugin is wonderful. Since the markerclusters are divIcons you can put whatever you want inside them using the iconCreateFunction. I wanted my clusters to reveal more information than just the marker count and figured a pie chart would do the job. So I told the iconCreateFunction to do some D3 magic and this is the result.

The example is a bit more complicated than necessary due to how my dataset is structured. But if you take a look at the defineClusterIcon() function you'll see that I use d3.nest() to build a dataset for the pie chart based on a given property from all the cluster's children. Then I pass this dataset over to the bakeThePie() function together with instructions on how to style the chart. The function returns svg markup which in turn is placed inside the divIcon.

Feel free to suggest improvements.

#map {
#legend {
position: absolute;
top: 3px;
left: 780px;
margin: 10px;
padding: 5px;
border-radius: 5px;
z-index: 100;
font-size: 1em;
font-family: sans-serif;
width: 165px;
background: rgba(255,255,255,0.6);
.legendheading {
position: relative;
height: 25px;
padding: 5px 2px 0px 2px;
font-size: larger;
font-weight: bold;
.legenditem {
padding: 2px;
margin-bottom: 2px;
/*Marker clusters*/
.marker-cluster-pie g.arc{
fill-opacity: 0.5;
.marker-cluster-pie-label {
font-size: 14px;
font-weight: bold;
font-family: sans-serif;
.marker {
width: 18px;
height: 18px;
border-width: 2px;
margin-top: -10px;
margin-left: -10px;
border-style: solid;
fill: #CCC;
stroke: #444;
background: #CCC;
border-color: #444;
.marker div{
text-align: center;
font-size: 14px;
font-weight: bold;
font-family: sans-serif;
/*marker categories*/
fill: #F88;
stroke: #800;
background: #F88;
border-color: #800;
fill: #FA0;
stroke: #B60;
background: #FA0;
border-color: #B60;
fill: #FF3;
stroke: #D80;
background: #FF3;
border-color: #D80;
fill: #BFB;
stroke: #070;
background: #BFB;
border-color: #070;
fill: #9DF;
stroke: #007;
background: #9DF;
border-color: #007;
fill: #CCC;
stroke: #444;
background: #CCC;
border-color: #444;
/*marker icons*/
background-image: url('running-16.png');
background-repeat: no-repeat;
background-position: 0px 1px;
background-image: url('bicycle-16.png');
background-repeat: no-repeat;
background-position: 1px 0px;
background-image: url('motorcycle-16.png');
background-repeat: no-repeat;
background-position: 1px 0px;
background-image: url('car-16.png');
background-repeat: no-repeat;
background-position: 1px 0px;
.map-popup span.heading {
display: block;
font-size: 1.2em;
font-weight: bold;
.map-popup span.attribute {
display: block;
.map-popup span.label {
font-weight: bold;
<!DOCTYPE html>
<meta charset="utf-8">
<link rel="stylesheet" href="" />
<link href='' rel='stylesheet' />
<link rel="stylesheet" href="clusterpies.css" />
<script src="" charset="utf-8"></script>
<script src=''></script>
<script src="" charset="utf-8"></script>
<div id="container">
<div id="map" />
"use strict"
var geojson,
geojsonPath = 'traffic_accidents.geojson',
categoryField = '5074', //This is the fieldname for marker category (used in the pie and legend)
iconField = '5065', //This is the fieldame for marker icon
popupFields = ['5065','5055','5074'], //Popup will display these fields
tileServer = 'http://{s}{z}/{x}/{y}.png',
tileAttribution = 'Map data: <a href="">OSM</a>',
rmax = 30, //Maximum radius for cluster pies
markerclusters = L.markerClusterGroup({
maxClusterRadius: 2*rmax,
iconCreateFunction: defineClusterIcon //this is where the magic happens
map ='map').setView([59.95, 10.78], 8);
//Add basemap
L.tileLayer(tileServer, {attribution: tileAttribution, maxZoom: 15}).addTo(map);
//and the empty markercluster layer
//Ready to go, load the geojson
d3.json(geojsonPath, function(error, data) {
if (!error) {
geojson = data;
metadata =;
var markers = L.geoJson(geojson, {
pointToLayer: defineFeature,
onEachFeature: defineFeaturePopup
} else {
console.log('Could not load data...');
function defineFeature(feature, latlng) {
var categoryVal =[categoryField],
iconVal =[iconField];
var myClass = 'marker category-'+categoryVal+' icon-'+iconVal;
var myIcon = L.divIcon({
className: myClass,
return L.marker(latlng, {icon: myIcon});
function defineFeaturePopup(feature, layer) {
var props =,
fields = metadata.fields,
popupContent = ''; function(key) {
if (props[key]) {
var val = props[key],
label = fields[key].name;
if (fields[key].lookup) {
val = fields[key].lookup[val];
popupContent += '<span class="attribute"><span class="label">'+label+':</span> '+val+'</span>';
popupContent = '<div class="map-popup">'+popupContent+'</div>';
layer.bindPopup(popupContent,{offset: L.point(1,-2)});
function defineClusterIcon(cluster) {
var children = cluster.getAllChildMarkers(),
n = children.length, //Get number of markers in cluster
strokeWidth = 1, //Set clusterpie stroke width
r = rmax-2*strokeWidth-(n<10?12:n<100?8:n<1000?4:0), //Calculate clusterpie radius...
iconDim = (r+strokeWidth)*2, //...and divIcon dimensions (leaflet really want to know the size)
data = d3.nest() //Build a dataset for the pie chart
.key(function(d) { return[categoryField]; })
//bake some svg markup
html = bakeThePie({data: data,
valueFunc: function(d){return d.values.length;},
strokeWidth: 1,
outerRadius: r,
innerRadius: r-10,
pieClass: 'cluster-pie',
pieLabel: n,
pieLabelClass: 'marker-cluster-pie-label',
pathClassFunc: function(d){return "category-";},
pathTitleFunc: function(d){return metadata.fields[categoryField].lookup[]+' ('' accident'+(!=1?'s':'')+')';}
//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;
/*function that generates a svg markup for the pie chart*/
function bakeThePie(options) {
/*data and valueFunc are required*/
if (! || !options.valueFunc) {
return '';
var data =,
valueFunc = options.valueFunc,
r = options.outerRadius?options.outerRadius:28, //Default outer radius = 28px
rInner = options.innerRadius?options.innerRadius:r-10, //Default inner radius = r-10
strokeWidth = options.strokeWidth?options.strokeWidth:1, //Default stroke is 1
pathClassFunc = options.pathClassFunc?options.pathClassFunc:function(){return '';}, //Class for each path
pathTitleFunc = options.pathTitleFunc?options.pathTitleFunc:function(){return '';}, //Title for each path
pieClass = options.pieClass?options.pieClass:'marker-cluster-pie', //Class for the whole pie
pieLabel = options.pieLabel?options.pieLabel:d3.sum(data,valueFunc), //Label for the whole pie
pieLabelClass = options.pieLabelClass?options.pieLabelClass:'marker-cluster-pie-label',//Class for the pie label
origo = (r+strokeWidth), //Center coordinate
w = origo*2, //width and height of the svg element
h = w,
donut = d3.layout.pie(),
arc = d3.svg.arc().innerRadius(rInner).outerRadius(r);
//Create an svg element
var svg = document.createElementNS(d3.ns.prefix.svg, 'svg');
//Create the pie chart
var vis =
.attr('class', pieClass)
.attr('width', w)
.attr('height', h);
var arcs = vis.selectAll('g.arc')
.attr('class', 'arc')
.attr('transform', 'translate(' + origo + ',' + origo + ')');
.attr('class', pathClassFunc)
.attr('stroke-width', strokeWidth)
.attr('d', arc)
.attr('class', pieLabelClass)
.attr('text-anchor', 'middle')
//.attr('dominant-baseline', 'central')
/*IE doesn't seem to support dominant-baseline, but setting dy to .3em does the trick*/
//Return the svg-markup rather than the actual element
return serializeXmlNode(svg);
/*Function for generating a legend with the same categories as in the clusterPie*/
function renderLegend() {
var data = d3.entries(metadata.fields[categoryField].lookup),
legenddiv ='body').append('div')
var heading = legenddiv.append('div')
.classed('legendheading', true)
var legenditems = legenddiv.selectAll('.legenditem')
.attr('class',function(d){return 'category-'+d.key;})
.classed({'legenditem': true})
.text(function(d){return d.value;});
/*Helper function*/
function serializeXmlNode(xmlNode) {
if (typeof window.XMLSerializer != "undefined") {
return (new window.XMLSerializer()).serializeToString(xmlNode);
} else if (typeof xmlNode.xml != "undefined") {
return xmlNode.xml;
return "";
Display the source blob
Display the rendered blob
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Copy link

Great piece of code - thanks! I adopted it to show search results in a corpus on the map as pie charts, which was relatively easy to do starting from this code; in that project (TEITOK) the breakdown of the pie chart is not shown as a title, but rather as a table in a popup, given clearer info about each cluster, using markers.on('clustermouseover').

Copy link

cnlane commented Apr 8, 2020

Awesome example! It's just what a needed to show storm severity for multiple rain gauges. Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment