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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15515215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:03:21  来源:igfitidea点击:

D3.js, Need click event in d3.js

javascriptd3.js

提问by Phanindar

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!!!

我需要打印圆圈的标题请帮忙!!!

回答by Christopher Chiche

You had the good idea by using the on("click", ...)function. However I see two things:

通过使用该on("click", ...)函数,您有一个好主意。但是我看到两件事:

  • The first problem is that you don't call the function on the click event but its value. So, you write dot.on("click", click());instead of 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? 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. Fortunately, with the onevent, as I used in my example, the function click is called with the argument dwhich represents the data of 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 irepresenting the index in the array, see the documentationfor more details.

  • 第一个问题是你不是在点击事件上调用函数而是调用它的值。所以,你写dot.on("click", click());而不是dot.on("click", click);. 为了理解差异,让我们假设函数 click 需要一个参数,例如表示有趣的点,它是什么?好吧,你会写以下内容:

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

    这相当于(并且不太容易出错)写作:

    dot.on("click", click)
    
  • 现在,第二点是,您确实希望将节点作为函数的参数传递click。幸运的是,对于on事件,正如我在我的示例中所使用的那样,函数 click 使用d代表dot. 因此,您现在可以编写:

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

    注意:您还可以通过编写使用另外一个参数function click(d,i)i代表数组中的索引,请参阅文档了解更多详情。

回答by bhab

If you have a title on your data,

如果您的数据有标题,

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