【问题标题】:D3 SelectAll except one class to modify opacityD3 SelectAll 除了一个类来修改不透明度
【发布时间】:2017-04-20 15:14:11
【问题描述】:

我正在研究以下径向图:

 //Fade out all players except the first player
        g.selectAll(".teamArc")
                .attr("opacity", 0.6);

        g.selectAll(".teamCircle")
                .attr("opacity", 0.6);

        //Select the first player by default
        var firstPlayer = arcAndCircleG.first();

        firstPlayer.select(".teamArc")
                    .classed("active", true)
                    .attr("id", "selected")
                    .attr("stroke", "green")
                    .attr("stroke-width", "1px")
                    .attr("opacity", 1);

        firstPlayer.select(".teamCircle")
                    .classed("active", true)
                    .attr("id", "selected")
                    .attr("stroke", "green")
                    .attr("stroke-width", "1px")
                    .attr("opacity", 1);


        teamCircles.on("mouseover", function(d,i){

            g.selectAll(".teamArc").transition()
                    .duration(200)
                    .attr("opacity", function(d,j){
                        return j != i ? 0.6 : 1;
                    });

            g.selectAll(".teamCircle").transition()
                    .duration(200)
                    .attr("opacity", function(d,j){
                        return j != i ? 0.6 : 1;
                    }); 

        });

        teamCircles.on("mousemove", function(d){

            d3.select(this)
                .classed("hover", true)
                .attr("stroke", "black")
                .attr("stroke-width", "1px");

            d3.select(this.parentNode)
                .select(".teamArc")
                .classed("hover", true)
                .attr("stroke", "black")
                .attr("stroke-width", "1px");        
        });

        teamCircles.on("mouseout", function(d){

            g.selectAll(".teamCircle")
                .transition()
                .duration(200)
                .attr("opacity", 1);

            g.selectAll(".teamArc")
                    .transition()
                    .duration(200)
                    .attr("opacity", 1);

            d3.select(this)
                .classed("hover", false)
                .attr("stroke", "black")
                .attr("stroke-width", "0px");

            d3.select(this.parentNode)
                .select(".teamArc")
                .classed("hover", false)
                .attr("stroke", "black")
                .attr("stroke-width", "0px");    

        });

        teamCircles.on("click", function(d){
            console.log("selected");
            g.selectAll(".teamCircle")
                    .attr("opacity", 0.6)
                    .attr("stroke-width", "0px");

            g.selectAll(".teamArc")
                    .attr("opacity", 0.6)
                    .attr("stroke-width", "0px");


            d3.select(this)
                .classed("clicked", true)
                .attr("opacity", 1)
                .attr("stroke", "green")
                .attr("stroke-width", "2px");

            d3.select(this.parentNode)
                .select(".teamArc")
                .classed("clicked", true)
                .attr("opacity", 1)
                .attr("stroke", "green")
                .attr("stroke-width", "2px");
        })

        teamArcs.on("mouseover", function(d,i){
                    //The following bit of code adapted from http://bl.ocks.org/WillTurman/4631136

            g.selectAll(".teamArc").transition()
                    .duration(200)
                    .attr("opacity", function(d,j){


                        return j != i ? 0.6 : 1;
                    });

            g.selectAll(".teamCircle").transition()
                    .duration(200)
                    .attr("opacity", function(d,j){
                        return j != i ? 0.6 : 1;
                    }); 

            // var clickedCircle = g.selectAll(".teamCircle")
            //                     .filter("active");

            // var clickedArc = g.selectAll(".teamArc")
            //                     .filter("active");

            // clickedArc.attr("fill", "green");

            // console.log(clickedCircle);
            // console.log(clickedArc);                        
        });

        teamArcs.on("mousemove", function(d){

            d3.select(this)
                .classed("hover", true)
                .attr("stroke", "black")
                .attr("stroke-width", "1px");

            d3.select(this.parentNode)
                .select(".teamCircle")
                .classed("hover", true)
                .attr("stroke", "black")
                .attr("stroke-width", "1px"); 
        });

        teamArcs.on("mouseout", function(d){
            g.selectAll(".teamArc")
                    .transition()
                    .duration(200)
                    .attr("opacity", "1");

            d3.select(this)
                .classed("hover", false)
                .attr("stroke", "black")
                .attr("stroke-width", "0px");

            d3.select(this.parentNode)
                .select(".teamCircle")
                .classed("hover", false)
                .attr("stroke", "black")
                .attr("stroke-width", "0px"); 

            g.selectAll(".teamCircle")
                .transition()
                .duration(200)
                .attr("opacity", 1);
        });

默认情况下,我希望第一个“玩家”(圆弧和圆)处于活动状态。当用户将鼠标悬停在另一个圆弧或圆上时,除了一个处于活动状态的圆弧和圆以及悬停的圆弧和圆外,所有圆弧和圆都应淡化为不透明度 0.6。

我遇到的问题是,当我悬停时,所有圆弧和圆(包括活动圆弧)都在淡出。

见小提琴:JSFiddle

【问题讨论】:

    标签: javascript css d3.js


    【解决方案1】:

    您可以过滤您的选择并拒绝具有“活动”类的元素。

    teamArcs.on("mouseover", function(d, i) {
      //The following bit of code adapted from http://bl.ocks.org/WillTurman/4631136
      console.log("hello");
      g.selectAll(".teamArc")
        .filter(function() {
          return !this.classList.contains('active')
        })
        .transition()
        .duration(200)
        .attr("opacity", function(d, j) {
          return j != i - 1 ? 0.6 : 1;
        });
    
      g.selectAll(".teamCircle").transition()
        .filter(function() {
          return !this.classList.contains('active')
        })
        .duration(200)
        .attr("opacity", function(d, j) {
          return j != i - 1 ? 0.6 : 1;
        });
    });
    

    工作演示 - https://jsfiddle.net/hmLu4zqb/

    【讨论】:

    • 谢谢,但不完全在那里。似乎当您将鼠标悬停在一个弧上时,紧邻它的弧也会激活。也许那是我代码中的其他地方。非常感谢您对过滤的帮助。
    • 哦,是的。我没有小心。您还应该检查索引,例如:j != i - 1 ? 0.6 : 1
    • 修复了,过滤弄乱了索引i,所以在做不透明度属性测试时需要减去一个。我根据您的解决方案对其进行了修改。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 2013-07-18
    • 2015-06-14
    • 1970-01-01
    • 2012-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多