markmarkoh / datamaps

Customizable SVG map visualizations for the web in a single Javascript file using D3.js
http://datamaps.github.io
MIT License
3.78k stars 1.01k forks source link

Prevent bubbles scaling with map #219

Open adrianbj opened 9 years ago

adrianbj commented 9 years ago

I want users to be able to zoom in so they can click/tap on bubbles that were overlapping when zoomed out, but current behavior doesn't help because the bubbles get bigger as well.

Am I missing an obvious setting or will I need to code a solution to redraw the bubbles after zooming?

Thanks!

hinok commented 8 years ago

This is small workaround... Of course it will not work for bubles with different radius and border-width.

function rescaleWorld(datamap) {
  datamap.svg
    .selectAll('g')
    .attr('transform', 'translate(' + d3.event.translate + ') scale(' + d3.event.scale + ')');
}

function rescaleBubbles(datamap) {
  var bubbleRadius = 4;
  var bubbleBorder = 15;

  datamap.svg
    .selectAll('.datamaps-bubble')
    .attr('r', bubbleRadius / d3.event.scale)
    .style('stroke-width', (bubbleBorder / d3.event.scale) + 'px');
}

var map = new Datamap({
  element: '#foo',
  done: function(datamap) {
    datamap.svg.call(d3.behavior.zoom().on('zoom', redraw));

    function redraw() {
      datamap.svg.select('g')
        .selectAll('path')
        .style('vector-effect', 'non-scaling-stroke');

      rescaleWorld(datamap);
      rescaleBubbles(datamap);
    }
  }
});