datamaps icon indicating copy to clipboard operation
datamaps copied to clipboard

how bind a event on each area

Open ghost opened this issue 13 years ago • 10 comments

please

ghost avatar Mar 08 '13 08:03 ghost

What are you trying to accomplish?

markmarkoh avatar Mar 08 '13 08:03 markmarkoh

Hi, Mark

I have the same concern. Could I enable onClick event on each area? Say, when I click on one region, it will redirect me to its wiki page.

I find it in a gist of yours.

Events emitted: - map-click - map-mouseover - map-mouseout - map-touchstart

illnino avatar Mar 12 '13 12:03 illnino

Events are emitted to the container you call datamap on, so if it were #container1, you would do:

$("#container1").on('map-click', function(event, data) {
   // data.geography is the geoJSON
  alert( data.geography.id ); //alerts "CA" if you clicked California
  //  data.data is the data you passed in to render the map, so in the ch
  alert( data.data.electoralVotes ); //alerts 32 if you clicked California
});

Since it's not an A tag but a PATH element, you'll have to do your redirection in javascript. So in your case, pass in the Wikipedia link alongside electoralVotes (in that example).

markmarkoh avatar Mar 12 '13 14:03 markmarkoh

good answer

ghost avatar Mar 13 '13 08:03 ghost

Was this removed from the newest version?? I cannot get it to work. I looked at the source and I could not find any binding to the onclick event.

vrocha avatar Jul 23 '13 12:07 vrocha

It was changed pretty drastically, checkout this example here:

https://github.com/markmarkoh/datamaps#events

Basically you use the done callback, which gives you the datamaps object as a parameter, which has a svg property which is the actual SVG DOM element. Then you can bind events there.

.datamaps-subunit is the class to bind to for the actual geography elements (like the state of Texas).

markmarkoh avatar Jul 23 '13 13:07 markmarkoh

Thank you for the quick response! Something may have changed, I am not getting the datamaps object which would have been great, I am instead getting the svg object pass into the function.

vrocha avatar Jul 23 '13 15:07 vrocha

Hmph, that might be an oversight. I meant it to be the datamaps object. I'm on vacation at the moment - if you can figure it out and send a pull request I'd gladly pull it in, otherwise I'll fix that later this week.

For the meantime you should be able to attach events to the svg directly, like d3.select(svg).selectAll('.datamaps-subunit').on('click'....

markmarkoh avatar Jul 23 '13 17:07 markmarkoh

I see the problem it is not in the repo, the repo is fine. However, on the homepage for datamap links to this file (http://datamaps.github.io/scripts/datamaps.world.min.js) which is the problem. If you do a quick search for "done", you can this the problem -- b.options.done(b.svg).

vrocha avatar Jul 23 '13 21:07 vrocha

How do you invoke a click on a path? I'm looking to call the Geography popUp externally.

FreelanceWebNinja avatar Jul 21 '16 12:07 FreelanceWebNinja