简体   繁体   English

D3.js,需要在d3.js中点击事件

[英]D3.js, Need click event in d3.js

I am trying to make a bubble chart, in that if i click on a bubble, the title of the bubble should appear in the console. 我想制作一个气泡图,如果我点击一个气泡,气泡的标题应该出现在控制台中。 I tried some ways, but was not successful. 我尝试了一些方法,但没有成功。

d3.json("deaths.json",
function (jsondata) {

    var deaths = jsondata.map(function(d) { return d.deaths; });
var infections = jsondata.map(function(d) { return d.infections; });
var country = jsondata.map(function(d) { return d.country; });
var death_rate = jsondata.map(function(d) { return d.death_rate; });

    console.log(deaths);
console.log(death_rate);
console.log(infections);
console.log(country);
console.log(date);

//Making chart

for (var i=0;i<11;i++)
{
var f;
var countryname=new Array();
var dot = new Array();
dot = svg.append("g").append("circle").attr("class", "dot").attr("id",i)
.style("fill", function(d) { return colorScale(death_rate[i]); }).call(position);       

//adding mouse listeners....

dot.on("click", click());
function click() 
{
     /***********************/
 console.log(country); //i need the title of the circle to be printed
     /*******************/
    }

function position(dot) 
{
dot .attr("cx", function(d) { return xScale(deaths[i]); })
    .attr("cy", function(d) { return yScale(death_rate[i]); })  
    .attr("r", function(d) { return radiusScale(infections[i]); });
dot.append("title").text(country[i]);
}
}
});

I need the title of circle to be printed Please help!!! 我需要要打印的圆圈标题请帮忙!!!

You had the good idea by using the on("click", ...) function. 通过使用on("click", ...)函数on("click", ...)你有了一个好主意。 However I see two things: 但是我看到两件事:

  • The first problem is that you don't call the function on the click event but its value. 第一个问题是您不会在click事件上调用该函数,而是调用其值。 So, you write dot.on("click", click()); 所以,你写dot.on("click", click()); instead of dot.on("click", click); 而不是dot.on("click", click); . To understand the difference, let's imagine that the function click needs one argument, which would for example represent the interesting dot, what would it be? 为了理解差异,让我们假设函数click需要一个参数,例如代表有趣的点,它会是什么? Well, you would write the following: 好吧,你会写下面的内容:

     dot.on("click", function(d){click(d)}) 

    Which is equivalent (and less prone to errors) to writing: 这与写作相同(并且不易出错):

     dot.on("click", click) 
  • Now, the second point is that, indeed you want to pass the node as an argument of the function click . 现在,第二点是,确实要将节点作为函数click的参数传递。 Fortunately, with the on event, as I used in my example, the function click is called with the argument d which represents the data of dot . 幸运的是,对于on事件,正如我在我的示例中所使用的那样,使用参数d调用函数click,该参数表示dot的数据。 Thus you can now write: 因此你现在可以写:

     dot.on("click", click); function click(d) { console.log(d.title); //considering dot has a title attribute } 

    Note: you can also use another argument by writing function click(d,i) with i representing the index in the array, see the documentation for more details. 注意:您还可以通过编写function click(d,i)使用另一个参数,其中i表示数组中的索引,有关详细信息,请参阅文档

如果您的数据有标题,

    dot.on('click' , function(d){ console.log(d.title); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM