javascript - D3JS 附加文本和鼠标行为不会出现

标签 javascript d3.js syntax

问题摘要:矩形显示正常,但文本标签根本不显示,并且鼠标行为(只是淡入不同颜色)根本不起作用。我对 D3JS 还很陌生,所以这可能是一个“菜鸟”问题,但我只是无法找出问题 - 尽管我猜测它与我用来附加的内容有关。

这是我的代码:

        var w = 1000;
        var h = 1000;

        var svgBody = d3.select("body").append("svg")
            .attr("width", w)
            .attr("height", h);

        var contributors = [{name: "a", iy: 30}, 
            {name: "b", iy: 60},
            {name: "c", iy: 90},
            {name: "d", iy: 120}];


        var r = svgBody.selectAll(".rect").data(contributors)
            .enter()
            .append("g")
                .attr("class", "rect");

       r.append("rect")
            .attr("width", 90)
            .attr("height", 20)
            .style("fill", "#db8215")
            .attr("stroke", "black")
            .attr("x", 400)
            .attr("y", function(d){return d.iy});


        //Mouse behaviors
        //Mouseover - it turns slightly lighter
        r.on("mouseover", function(d){
            d3.select(this).select("rect").transition()
                .duration(500)
                .attr("color", "#e06d08");              
        });

        r.on("mouseout", function(d){
            d3.select(this).select("rect").transition()
                .duration(500)
                .attr("color", "#ffa354");
        });

        r.append("text")
            .attr("text-anchor", "middle")
            .attr("x", 400)
            .attr("y", function(d){return d.iy})
            .style("font-size", "30px")
            .style('fill', 'white')
            .style('font-family', 'Helvetica')
            .text(function(d){return d.name});

有几个帖子提出了相同的问题,但涉及不同的问题。我的代码不会将文本直接附加到所需的形状,例如 thisthisthis ,并且控制台调试器中没有弹出任何错误。

非常感谢所有帮助!预先感谢您!

编辑:抱歉,看起来我采用了早期版本的代码。我尝试附加到“g”元素(在看到其他帖子之后)和“rect”元素(之前)。

更新:事实证明,选择(可能与不正确的附加一起)做到了,正如已接受的答案中所述。非常感谢!

最佳答案

r.on("mouseover", function(d){
            d3.select(this).select("rect").transition()
                .duration(500)
                .attr("color", "#e06d08");              
        });

除了 d3.select(this) 选择矩形之外,选择是正确的,因此之后不需要 .select("rect") 。您必须使用 .style("fill", "..."),而不是使用 .attr("color", "...")。 “鼠标悬停”也是如此。下面的工作片段。

编辑:标签未显示的原因是因为您将它们附加到矩形。相反,它们应该与 rect 一起位于 g 元素中。更新了代码片段。

var w = 1000;
        var h = 1000;

        var svgBody = d3.select("body").append("svg")
            .attr("width", w)
            .attr("height", h);

        var contributors = [{name: "a", iy: 30}, 
            {name: "b", iy: 60},
            {name: "c", iy: 90},
            {name: "d", iy: 120}];


        var r = svgBody.selectAll(".rect").data(contributors)
            .enter()
            .append("g")
                .attr("class", "rect");
            r.append("rect")
                .attr("width", 90)
                .attr("height", 20)
                .style("fill", "#db8215")
                .attr("stroke", "black")
                .attr("x", 400)
                .attr("y", function(d){return d.iy});


        //Mouse behaviors
        //Mouseover - it turns slightly lighter
        r.on("mouseover", function(d){
            d3.select(this).select('rect').transition()
                .duration(500)
                .style("fill", "#e06d08");              
        });

        r.on("mouseout", function(d){
            d3.select(this).select('rect').transition()
                .duration(500)
                .style("fill", "#ffa354");
        });

        r.append("text")
        		.attr('class', 'text')
            .attr("text-anchor", "middle")
            .attr("x", 400)
            .attr("y", function(d){return d.iy})
            .style("font-size", "30px")
            .style('fill', 'white')
            .style('font-family', 'Helvetica')
            .text(function(d){
            	return d.name
             });
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

关于javascript - D3JS 附加文本和鼠标行为不会出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45272062/

相关文章:

javascript - jsblocks更新数据更新 View

javascript - ng-show 不适用于 Ionic 框架

javascript - 使用 d3js 从 SVG 边界框获取点击事件

javascript - 未捕获的 TypeError : n. getMonth 不是 d3 条形图中的函数

javascript - 将非法 token 传递给 JavaScript 函数?

javascript - 绑定(bind)不适用于 KnockoutJS 中 JSON 加载的嵌套模板

javascript - 从数组中获取最大的 3 个数字?

javascript - d3 : update of chart via exit/remove/merge does not process changed label text properly

声明字节数组时出现 C++ 语法错误

c# - 在 C# 中,如何将泛型类型参数约束为仅接受可为 null 的值类型?