最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How do I append text to my rect in d3? - Stack Overflow

matteradmin8PV0评论

Here is my code, the text is appending when I inspect the element but it is not showing up

var data = [0, 1, 2];
var width = 100;
var height = 100;

var canvas = d3.select("body")
               .append("svg")
               .attr("height", 500)
               .attr("width", 500)
               .attr("fill", "red")
               .append("g");


var someData = canvas.selectAll("rect")
                     .data(data)
                     .enter()
                        .append("rect")
                        .attr("height", height)
                        .attr("width", width)
                        .attr("fill", "red")
                        .attr("transform", function(d){ return "translate(" + height * d + ",0)";});

someData.append("text")
        .attr("transform", function(d){ return "translate(" + height * d + ",0)";})
        .attr("font-size", "2em")
        .attr("color", "black")
        .text(function(d){ return d;});

/

What did I do wrong?

Here is my code, the text is appending when I inspect the element but it is not showing up

var data = [0, 1, 2];
var width = 100;
var height = 100;

var canvas = d3.select("body")
               .append("svg")
               .attr("height", 500)
               .attr("width", 500)
               .attr("fill", "red")
               .append("g");


var someData = canvas.selectAll("rect")
                     .data(data)
                     .enter()
                        .append("rect")
                        .attr("height", height)
                        .attr("width", width)
                        .attr("fill", "red")
                        .attr("transform", function(d){ return "translate(" + height * d + ",0)";});

someData.append("text")
        .attr("transform", function(d){ return "translate(" + height * d + ",0)";})
        .attr("font-size", "2em")
        .attr("color", "black")
        .text(function(d){ return d;});

http://jsfiddle/2GAxG/

What did I do wrong?

Share Improve this question asked Apr 24, 2014 at 5:36 InstinctInstinct 2,2612 gold badges34 silver badges47 bronze badges 1
  • related: cambridge-intelligence./… – exchange Commented Aug 16, 2020 at 12:44
Add a ment  | 

2 Answers 2

Reset to default 2

Couple of Problems with your code:

  • The <text> cannot go inside a <rectangle> in svg.

  • The top level group being filled with a color, hides everything inside it.

A solution to the first point is to group the <rectangle> & <text> into sub groups of <g>

Update Code: (Demo)

var data = [0, 1, 2];
var width = 100;
var height = 100;

var canvas = d3.select("body")
               .append("svg")
               .attr("height", 500)
               .attr("width", 500)
               //.attr("fill", "red")
               .append("g");


var someData = canvas.selectAll("rect")
                    .data(data)
                    .enter()
                     .append("g")
                      .append("rect")
                      .attr("height", height)
                      .attr("width", width)
                      .attr("fill", "red")
                      .attr("transform", 
                       function(d){ return "translate(" + height * d + ",0)";});

canvas.selectAll("g")
        .append("text")
        .attr("transform", 
              function(d){ return "translate(" + height * d + ",30)";})
        .attr("font-size", "2em")
        .attr("color", "black")
        .text(function(d){ return d;});

Are you trying to add text at specific positions within the canvas or within shapes on the canvas? See fiddle: http://jsfiddle/sjp700/6hAg7/

var margin = { top: 40, right: 4, bottom: 40, left: 20 },
  width = 800,
  height = 800;

var data = [100,200,300];

var svgContainer = d3.select("body").append("svg")
        .attr("width", 800)
        .attr("height", 800)
        .style("background", "red")
        .append('g')
        .attr('transform', 'translate(' + margin.left + ', ' + margin.top + ')');


var rect = svgContainer.selectAll("rect")
                           .data(data)
                           .enter();

rect.append("rect")
    .attr("x", function(d){ return d;})
    .attr("y", function(d){ return d;})
    .attr("width", 100)
    .attr("height", 20)
    .style("fill", "lightblue");

rect.append("text")
       .style("fill", "black")
       .style("font-size", "14px")
       .attr("dy", ".35em")
       .attr("x", function (d) { return d; })
       .attr("y", function (d) { return d+10; })
       .style("style", "label")
       .text(function (d) { return d; });
Post a comment

comment list (0)

  1. No comments so far