datamaps icon indicating copy to clipboard operation
datamaps copied to clipboard

Zoom feature

Open eliihen opened this issue 11 years ago • 31 comments

Adds a zoom-to-bounding-box on click feature, adressing issue #25. Also includes a zoomFactor in the options, effectively giving people the option to choose how far the camera zooms. Has a special case for circles, as they do not have a bounding box

Demo here: http://esphen.com/Open-Source/datamaps/src/examples/bubble-in-center.html

eliihen avatar Aug 16 '14 22:08 eliihen

Can you think of a way to address the updatePopup() function to account for the zoom?

markmarkoh avatar Aug 27 '14 19:08 markmarkoh

I've done the following: o Moved zoomConfig from geographyConfig o Fixed the bugs you found in clicked function o Made popup take zoom into account by getting coordinates from container instead of svg layer

eliihen avatar Aug 29 '14 20:08 eliihen

More changes: o Adds public toggleZoom funciton that can be passed bool to set zoom (needed to add/remove event listeners on map elements) o Now checks against negative values on zoomFactor. Flipped the map upside down. Cool effect though o Renames function to clickZoom. click was a bit too generic

And as per contrib. guidelines: o Removes buildfiles from repo o Adds demo (zoom-on-click.html)

I updated my demo with the latest revision at: http://esphen.com/Open-Source/datamaps/src/examples/zoom-on-click.html

eliihen avatar Aug 29 '14 22:08 eliihen

FWIW...

There appears to be an awfully simple method to add both zoom and pan features to a datamap, as seen in this non-datamap d3 map: http://bl.ocks.org/andrewxhill/4455569#index.html

If you hook into the datamap svg, and call this in the done event, then both zooming and panning are enabled, and moreover, the zooming and panning works with labels and bubbles.

 datamap.svg.call(d3.behavior.zoom() 
        on("zoom", redraw));

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

mg1075 avatar Sep 27 '14 17:09 mg1075

@mg1075 Thank you for the tip! I found the missing piece documented here: https://github.com/markmarkoh/datamaps#events Note: There is a missing period between zoom() and on, it should read: datamap.svg.call(d3.behavior.zoom().on("zoom", redraw));

CHgsd avatar Oct 15 '14 05:10 CHgsd

Thanks @mg1075, your tip was so useful!

amytych avatar Mar 02 '15 08:03 amytych

Thanks a bunch! @mg1075 it a was a lot easier than other things I've tried and it worked like a charm.

Wilsonkho avatar Apr 14 '15 05:04 Wilsonkho

Here is a working example of @mg1075's tip:

http://jsbin.com/boribamugo/1/edit?html,output

cc @Wilsonkho

markmarkoh avatar Apr 15 '15 19:04 markmarkoh

Hi guise, just made use of datamaps here: https://auth.lonelyplanet.com/profiles/wunderbart/trips I've added zoom with bounds so the map always stays on screen. There are also buttons for programmatic zooming. Currently thinking about optimizing zoom animation on mobiles (it's pretty choppy now). Any suggestions? Lemme know what you think! :smile:

WunderBart avatar Aug 03 '15 16:08 WunderBart

@WunderBart that's fantastic! I'd love to get some of that in the core library. Any chance you could post a gist of how you got some of that to work? (PR also welcome).

markmarkoh avatar Aug 03 '15 17:08 markmarkoh

Sure! Will prepare some gist after I push a PR with fix for d3 dependency loading issue (the has not yet been loaded for context one).

WunderBart avatar Aug 03 '15 21:08 WunderBart

@WunderBart - your zoom with bounds looks like it is working brilliantly and the zoom buttons are a nice touch too. Any word on the D3 fix and when they might be available for datamaps? I could sure use it right now :)

Thanks!

adrianbj avatar Aug 26 '15 23:08 adrianbj

Thanks, @adrianbj :D I'm sorry for the delay. Too many things at once lately. Will try to push on friday :)

WunderBart avatar Aug 26 '15 23:08 WunderBart

No problem at all - Friday would be awesome - thanks!

adrianbj avatar Aug 26 '15 23:08 adrianbj

@mg1075's tip conflicts with Datamap.prototype.resize() (used for responsiveness), because resize sets the CSS transform property and @mg1075 the SVG transform attribute. If both are set one of them is ignored. Below works with responsive=True. But I would rather change the resize method to use the SVG transform attribute instead.

done: function(datamap) {
    datamap.svg.call(d3.behavior.zoom().on("zoom", redraw));
    function redraw() {
        var prefix = '-webkit-transform' in document.body.style ? 
                        '-webkit-' : '-moz-transform' in document.body.style ? 
                                    '-moz-' : '-ms-transform' in document.body.style ? 
                                            '-ms-' : '';
        var x = d3.event.translate[0];
        var y = d3.event.translate[1];
        datamap.svg.selectAll("g")
            .style(prefix + 'transform', 
                'translate(' + x +'px, ' + y + 'px) scale(' + (d3.event.scale) + ')');
    }
}

Cheers!

lukpueh avatar Sep 30 '15 12:09 lukpueh

Hi all, the tip by @lukpeh works great on Chrome and Firefox but It's doesn't work on Internet Explorer. Someone knows what can it happen? Thanks and regards

revoltafelipe avatar Feb 01 '16 12:02 revoltafelipe

@WunderBart have you got any update? Your implementation in Lonely Planet looks awesome!

magic890 avatar Feb 05 '16 12:02 magic890

@WunderBart any chances to use your great example?

pualien avatar Mar 25 '16 13:03 pualien

@WunderBart don't mean to pester you (it sounds like you're a busy chap) but any chance of that gist? To back up what everyone else has already stated, your map on LP profiles looks amazing!

moxxuk avatar Mar 30 '16 14:03 moxxuk

Guise, I'm really sorry about this insane delay and thanks for reminding me :smile: I promise I'll prepare a small fiddle a bit later today (gotta :ship: first, you know). Great to hear you like that zoom!

WunderBart avatar Mar 30 '16 14:03 WunderBart

Great stuff @WunderBart, really looking forward to showing this off :)

moxxuk avatar Mar 30 '16 14:03 moxxuk

Here's more less how I did it: https://jsfiddle.net/wunderbart/Lom3b0gb/ There's not much comments explaining the code yet and the structure is taken out from larger app so it can be a bit overkill. I'll be happy to answer any questions. Enjoy!

WunderBart avatar Mar 30 '16 21:03 WunderBart

Fantastic, thanks!

moxxuk avatar Mar 31 '16 16:03 moxxuk

In @WunderBart implementation another interesting part is that the country boundaries stroke do not scale with the zoom:

var paths = this.datamap.svg.selectAll("path"); // preserve stroke thickness paths.style("vector-effect", "non-scaling-stroke");

This will be applied also to arcs, resulting in a glitch. To apply this transformation only to country boundaries here is the correct selector

var paths = this.datamap.svg.selectAll("path.datamaps-subunit");

magic890 avatar Apr 19 '16 07:04 magic890

Will this ever be merged into master?

TomCrowley avatar Nov 15 '16 16:11 TomCrowley

@esphen Which version did you fork this repo from, I am on datamaps version 0.5.8 and the tap to zoom on mobile doesn't work. The zoom doesn't work on chrome or safari on a iphone 6, I'm assuming maybe its because my version has a bunch of new code that may be interferring.

thomaskbird avatar Nov 17 '16 15:11 thomaskbird

@ThomasBird1984

Wow, no idea. This was literally years ago. Looking at the commit history of my forked repo, it looks like 0.3.2. I was under the impression the author of the library ended up with not including this feature, and instead recommending the solutions provided in this thread. In which case this PR should be closed.

Ping @markmarkoh

eliihen avatar Nov 17 '16 15:11 eliihen

@esphen That sucks this is the exact functionality I needed. Alright will do thanks!

thomaskbird avatar Nov 17 '16 16:11 thomaskbird

@thomasbird1984 just use @WunderBart implementation it is going to work with the latest release.

magic890 avatar Nov 18 '16 09:11 magic890

Here's more less how I did it: https://jsfiddle.net/wunderbart/Lom3b0gb/ There's not much comments explaining the code yet and the structure is taken out from larger app so it can be a bit overkill. I'll be happy to answer any questions. Enjoy!

Hi! I know this was a LOONG while ago, but there is any way to click in one country, and it will zoom on that country?

souocare avatar Aug 07 '22 16:08 souocare