简体   繁体   English

D3:鼠标悬停/鼠标移出时的enter(),exit()

[英]D3: enter(), exit() on mouseover/mouseout

I have a function which, on mouseover of a point on a line graph, calls the Twitter API and fetches a tweet associated with its timestamp. 我有一个函数,在将鼠标悬停在折线图上的某个点上时,会调用Twitter API并获取与其时间戳相关的推文。 It then adds nested divs and elements which correspond to data. 然后,它添加与数据相对应的嵌套div和元素。 My problem is that on mouseout, I'm wanting to remove that div and its associated data from the DOM so that when I mouseover another point, a new panel is created with the relevant data. 我的问题是,在mouseout上,我想从DOM中删除该div及其关联的数据,以便当我将鼠标悬停在另一点时,将创建一个包含相关数据的新面板。

My mouseover looks like this: 我的鼠标悬停看起来像这样:

.on("mouseover", function(d,i){
    var tweetDivs = d3.select(".panel").selectAll("div.panel-body")
                      .data(tweet_list)
                      .enter()
                      .append("div")
                      .attr("id", function(d){return "p"+d['id_str']})
                      .classed("panel-body", true);

                tweetDivs.append("img")
                    .attr("width", 20)
                    .attr("height", 20)
                    .attr("src", function(d){return d['user']['profile_image_url']})
                    .classed("panel-tweet-img-profile", true);

                tweetDivs.append("p")
                    .text(function(d){
                        var tweet_created_format = d3.timeFormat("%-I:%M%p, %e %b %Y")(d3.timeParse("%a %b %d %H:%M:%S %Z %Y")(d['created_at']));
                        return "@"+d['user']['screen_name']+"    ("+tweet_created_format+")";
                    })
                    .classed("panel-tweet-text-header", true);

                tweetDivs.append("p")
                    .text(function(d){return d['text'];})
                    .classed("panel-tweet-text-body", true);

                var infoBlock = tweetDivs.append("p")
                                .classed("panel-tweet-info-block", true);

                infoBlock.append("img")
                    .attr("src", imgRetweet)
                    .classed("panel-tweet-img-retweet", true);
                infoBlock.append("text")
                    .text(function(d){
                        return d['retweet_count'];
                    })
                    .classed("panel-tweet-text-retweet", true);

                infoBlock.append("img")
                        .attr("src", imgFav)
                        .classed("panel-tweet-img-favorite", true);
                infoBlock.append("text")
                    .text(function(d){
                        return d['favorite_count'];
                    })
                .classed("panel-tweet-text-favorite", true);
});

And my mouseout function which is meant to remove it, has the following exit() function: 我要删除它的mouseout函数具有以下exit()函数:

    .on("mouseout", function(d,i){
        // exit()
        var panelRemove = d3.select(".panel-body");

        panelRemove.data(tweet_list)
                    .exit()
                    .remove();
    });

I'm not sure what it is I'm doing wrong as I've passed the same data to be removed here. 我不确定自己做错了什么,因为我已经传递了相同的数据以在此处删除。 I've also tried d3.select(".panel").selectAll("div.panel-body") but nothing happens. 我也尝试了d3.select(".panel").selectAll("div.panel-body")但没有任何反应。

The initial panel appears absolutely fine, with all of the relevant data. 初始面板看起来非常好,包含所有相关数据。 But mouseout doesn't remove it and I can't show new panels. 但是mouseout不会将其删除,并且我无法显示新面板。

Since you are passing the same tweet_list data to the panelRemove selection, the exit() code won't find any node to remove. 由于将相同的tweet_list数据传递给panelRemove选择,因此exit()代码将找不到要删除的任何节点。 The exit().remove() will remove existing nodes that are no longer represented in the data. exit().remove()将删除不再在数据中表示的现有节点。 If you were to pass in an empty tweet list as new data, the exit().remove() should remove the nodes. 如果要传递一个空的tweet列表作为新数据,则exit().remove()应该删除节点。

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

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