1

Inside my AngularJs web application I'm building a map using Leaflet.js and Leaflet markercluster.

To render charts I'm using nvd3.js and the nvd3-angular-directive

I've got different data coming from all over the world and I'm showing a simple CircleMarker for each nation that has received data.

My data are organised this way:

{
  US : {country:"US", count:10, rate: {high: 3, medium: 4, low: 3}},
  IT : {country:"IT", count:4, rate: {high: 1, medium: 1, low: 2}}
}

This means that in the map a CircleMarker will be shown in the center of the USA and Italy.

Now let's get to the issue, I'm binding a popup on each CircleMarker, I want each popup to contain a nvd3 pie (specifically a donut) chart that will show the distribution of rates for that particular nation.

The problem is that currently the map is rendered properly and the markers are placed perfectly but when I click on a marker the popup that appears is empty (see figure). I've googled but wasn't able to find anything that can help me.

Empty Popup


This is how I build the CircleMarkers and the cluster:
var markers = L.markerClusterGroup({
  maxClusterRadius: 120,
  iconCreateFunction: function (cluster) {
    return L.divIcon({html: '<b>' + cluster.getChildCount() + '</b>', className: 'mycluster', iconSize: L.point(40, 40)});
  }
})

var geolocData = {
  US : {country:"US", count:10, rate: {high: 3, medium: 4, low: 3}},
  IT : {country:"IT", count:4, rate: {high: 1, medium: 1, low: 2}}
}

for (key in geolocData){

  var latlng = retrieveLatLongFromCountryCode(key)
  var marker = new L.circleMarker(latlng, {stroke: false, className:'the-marker-class', fillOpacity: 1, color:'#00FF00', weight: 1})

  var popupChartOptions = {
    chart: {
      type: 'pieChart',
      height: 140,
      donut: true,
      donutRatio: 0.40,
      x: function(d) { return d.label },
      y: function(d) { return d.value },
      color: [
        '#2ecc71',
        '#f1c40f',
        '#e74c3c',
      ],
      valueFormat: d3.format(',.0d')
    }
  }
  var popupChartData = [{
    label: 'low',
    value: geolocData[key].rate.low
  }, {
    label: 'medium',
    value: geolocData[key].rate.medium
  }, {
    label: 'high',
    value: geolocData[key].rate.high
  }]

  marker.bindPopup('<nvd3 options="chartOptions" data="chartData"></nvd3>')
  markers.addLayers(marker)
}

map.addLayer(markers)

UPDATE

I've changed things a bit. Firstly I've extended the CircleMarker class

var customCircleMarker = L.CircleMarker.extend({
  options: { 
    countrycode: '',
    rates: {},
    count: 0
  }
})

Then I use this CustomCircleMarker inside my map

var marker = new customCircleMarker(latlng, {stroke: false, className: 'the-class-name', fillOpacity: 1, weight: 1})

var popupChartData = [{
    label: 'low',
    value: [geolocLoad[key].rate.low]
}, {
    label: 'medium',
    value: [geolocLoad[key].rate.medium]
}, {
    label: 'high',
    value: [geolocLoad[key].rate.high]
}]

Then I bind the popup, fill the marker with my custom data and create an on click callback which emits a message with my useful data.

marker.bindPopup('<div id="chart-' + key + '"></div>')
marker.countrycode = key
marker.rates = popupChartData
marker.count = geolocLoad[key].count

marker.on('click', function(e){
  $scope.$emit('marker:click', this.countrycode, this.count, this.rates)
})

The receiver will take the data and render the chart this way:

$scope.$on('marker:click', function(caller, countrycode, count, rates){
  console.log('received', countrycode, count, rates)

  var width = 500,
      height = 500

  nv.addGraph(function(){
    var chart = nv.models.pieChart()
      .x(function(d) { return d.label })
      .y(function(d) { return d.value })
      .width(width)
      .height(height);

    d3.select("#chart-"+countrycode)
      .datum(rates)
      .attr('width', width)
      .attr('height', height)
      .call(chart);

    return chart;
  })
})

Sadly this isn't working again.. not working again

NOTE: I'm not using the angular-leaflet-directive since I've found it pretty overkill and didn't like it that much. Do you think it's better for me to use it anyway?

fredmaggiowski
  • 2,232
  • 3
  • 25
  • 44

1 Answers1

2

I've solved the issue by myself, I'm posting an answer since it might be helpful to future users.

The most important thing I forgot was to insert an <svg> element inside of the popup, this caused the chart to not being rendered since there were no 'canvas' we're the library could render it..

Marker generation:

var marker = new customCircleMarker(countries.names[c].latlng, {stroke: false, className: 'the-class-name', fillOpacity: 1, weight: 1})

marker.bindPopup('<div id="chart-' + key + '" class="country-popup-chart-container"><svg class="country-popup-chart"></svg></div>', {closeButton: false})
marker.countrycode = key
marker.rates = geolocLoad[key].rate
marker.count = geolocLoad[key].count

marker.on('click', function(e){
  $scope.$emit('marker:click', this.countrycode, this.count, this.rates)
})

Chart generation triggered with a $emit invoked on click:

$scope.$on('marker:click', function(caller, countrycode, count, rates) {

  var popupChartData = [{
    label: 'low',
    value: rates.low,
    color: '#2ecc71'
  }, {
    label: 'medium',
    value: rates.medium,
    color: '#f1c40f'
  }, {
    label: 'high',
    value: rates.high,
    color: '#e74c3c'
  }]

  nv.addGraph(function(){
    var chart = nv.models.pieChart()
      .x(function(d) { return d.label })
      .y(function(d) { return d.value })
      .showLabels(false)
      .donut(true)
      .donutRatio(0.5)

    d3.select('#chart-' + countrycode + ' svg')
      .datum(popupChartData)
      .call(chart)

    return chart
  })
}) 

Sized up with the following CSS:

div.country-popup-chart-container{
  height: 80px;
  width: 200px;
}

The final result is:

final result

fredmaggiowski
  • 2,232
  • 3
  • 25
  • 44