简体   繁体   English

如何在d3js中过滤一系列元素?

[英]How to filter a range of elements in d3js?

I need to translate downwards all the elements below the one on which mouse is over. 我需要向下平移鼠标上方的所有元素。 Currently I can filter the selection by using .filter. 目前,我可以使用.filter过滤选择。 The following code can filter the selection for elements i>10. 以下代码可以过滤对i> 10的选择。 How do I use the current id of the mouseover element instead of '10'. 如何使用mouseover元素的当前ID代替“ 10”。 Or if there is a better way to shift the proceeding elements, that'll be great. 或者,如果有一种更好的方式来转移进行中的元素,那就太好了。 Thanks 谢谢

var bars = chart.selectAll('.bar')
    .data(data)
    .enter().append('g')
    .attr('class', 'bar')
    .attr('transform', function(d, i) {
        return 'translate(0,' + y(i) + ')';
    })
    .on("mouseover", function(d, i) {
        d3.select(this)
        .filter(function(d,i ) { console.log(this);return i>10 })
            .attr('transform', function(d, i) {
                return 'translate(0,' + (y(i)) + ')';
            });
    });

古腾堡 Live Here 住在这里

var bubble = d3.layout.pack()
    ...

d3.json("js/data/fortune2009.json", function(json) {

    var node = svg.data([json]).selectAll("g.node")
        .data(bubble.nodes); // filter here?

    node.enter()
        .append("g")
        .attr("class", "node")
        .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });

    node.append("circle")
        .attr("r", function(d) { return d.r; })
        .attr("class", function(d) { return "q" + color(d.Profit) + "-9"; });

    node.filter(function(d) { return !d.children; })
        .append("text")
        .attr("dy", ".3em")
        .style("text-anchor", "middle")
        .text(function(d) { return d.Company.substring(0, d.r / 3); });
});

replace i in the return with $(this).attr('id') to get the moused-over element's id. $(this).attr('id')替换返回的i以获取鼠标悬停的元素的ID。

.on("mouseover", function(d, i) {
        d3.select(this)
        .filter(function(d,i ) { console.log(this);return $(this).attr('id') >10 })
            .attr('transform', function(d, i) {
                return 'translate(0,' + (y(i)) + ')';
            });
    });

[Update] - if you see Lars' comment, he correctly points out that if the id you are looking for is contained in your data you can access it by simply (and more efficiently) by doing d.id rather than through the element's id attribute. [更新] -如果您看到Lars的评论,他正确地指出,如果您要查找的id包含在data ,则可以通过执行d.id而不是通过元素的ID来简单(更有效)地访问它属性。

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

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