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

Passing Data as Argument to done method #46

Closed ramnathv closed 8 years ago

ramnathv commented 10 years ago

Is it possible to access the data associated with an element when using the done method to trigger an action. I see that geography is allowed, but wasn't able to get data working, just like in popupTemplate.

markmarkoh commented 10 years ago

This should work for the geographies:

   done: function(map) {
            map.svg.selectAll('.datamaps-subunit').on('click', function(geography) {
              console.log( map.options.data[geography.id] );
            });
      }
ramnathv commented 10 years ago

Ah. I would never have figured that out! For consistency with the popupTemplate function, do you think it would be better to allow data to also be explicitly passed to the done method. I can work with what you have now, but thought it would be good to keep things consistent.

markmarkoh commented 10 years ago

The first version of datamaps had that, but I ended up removing it. For it to work, I had to hard code the events datamaps would allow to be emitted, like "click .bubble", "mouseenter .datamap-subunit" and so on. I ended up including the 20 or so events I thought would be used, but the decided to change my approach and allow you to listen to any event with d3's selection.on event handler.

Doing that lose the data parameter, though, and now a lookup is required.

I dislike the inconsistency and think it should be made consistent, and I want to explore some lightweight options, like: Inside any event handler, d3.select(this).datum() should return the geography as well as the corresponding data value.

I think that's attainable pretty easily, since right now it already provides the geography.