最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can we create an onclick event for a d3 data map? - Stack Overflow

programmeradmin0浏览0评论

I am building a d3 data map: /

When I click on a country, I want the border to change color. How do I assign an onclick event to a d3 path using datamaps? The svg paths seem to lack a CSS ID as well as any identifying hook.

I am building a d3 data map: http://datamaps.github.io/

When I click on a country, I want the border to change color. How do I assign an onclick event to a d3 path using datamaps? The svg paths seem to lack a CSS ID as well as any identifying hook.

Share Improve this question edited Apr 8, 2013 at 12:15 victorsc 7229 silver badges30 bronze badges asked Apr 7, 2013 at 10:23 dangerChihuahua007dangerChihuahua007 21k38 gold badges128 silver badges211 bronze badges 1
  • If you're using that github project, it might be better to submit an issue to add a "highlight on click" option. There's already highlight on hover so you could fork the project and add a mousedown event here github./markmarkoh/datamaps/blob/master/public/js/app/views/… – minikomi Commented Apr 7, 2013 at 10:59
Add a ment  | 

2 Answers 2

Reset to default 2

You can use JQuery "done:function" and specify on("click") as below :

 done: function(datamap) {
        datamap.svg.selectAll('.datamaps-subunit').on('click', function(geography) {
            alert(geography.properties.name);
        });
    }

Refer to https://github./markmarkoh/datamaps for further detail.

You'll need both the done call back as well as the updateChoropleth function. For example, to turn each country black you would do:

done: function(datamap) {
  datamap.svg.selectAll('.datamaps-subunit').on('click', function(geography) {
      var m = {};
      m[geography.id] = '#000000';
      datamap.updateChoropleth(m);
  });
}
发布评论

评论列表(0)

  1. 暂无评论