admin管理员组

文章数量:1332649

I want to display the legend (caption?) of some data. For each part of the legend, I append a rect and a p to a parent division. The problem is that rect are not showing up.

Here is my code:

var groups = {{ groups|safe }};
groups.forEach(function(group, i) {
    var div = d3.select("#collapse-legend");
    div.append("rect")
        .attr("width", 17)
        .attr("height", 17)
        .style("fill-opacity", 1)
        .style("fill", function (d) { return c(i); });
    div.append("p").text(group)
});

Now, when I check the content of my web page, I get both rect and p, but rect:

  1. is not showing up
  2. seems to have a width of 0 (showing its area with firefox)

Is there some mistake in my code? Are there better ways to achieve this? I am very new to javascript and d3.js so please be indulgent ^^

Update

So this is what I ended with.
HTML:

<div ...>
    <svg id="legend-svg"></svg>
</div>

JavaScript:

// set height of svg
d3.select("#legend-svg").attr("height", 18*(groups.length+1));

// for each group, append rect then text
groups.forEach(function(group, i) {
    d3.select("#legend-svg").append("rect")
        .attr("y", i*20)
        .attr("width", 18)
        .attr("height", 18)
        .style("fill-opacity", 1)
        .style("fill", function (d) { return c(i); });
    d3.select("#legend-svg").append("text")
        .attr("x", 25)
        .attr("y", i*20+15)
        .text(group);
});

I want to display the legend (caption?) of some data. For each part of the legend, I append a rect and a p to a parent division. The problem is that rect are not showing up.

Here is my code:

var groups = {{ groups|safe }};
groups.forEach(function(group, i) {
    var div = d3.select("#collapse-legend");
    div.append("rect")
        .attr("width", 17)
        .attr("height", 17)
        .style("fill-opacity", 1)
        .style("fill", function (d) { return c(i); });
    div.append("p").text(group)
});

Now, when I check the content of my web page, I get both rect and p, but rect:

  1. is not showing up
  2. seems to have a width of 0 (showing its area with firefox)

Is there some mistake in my code? Are there better ways to achieve this? I am very new to javascript and d3.js so please be indulgent ^^

Update

So this is what I ended with.
HTML:

<div ...>
    <svg id="legend-svg"></svg>
</div>

JavaScript:

// set height of svg
d3.select("#legend-svg").attr("height", 18*(groups.length+1));

// for each group, append rect then text
groups.forEach(function(group, i) {
    d3.select("#legend-svg").append("rect")
        .attr("y", i*20)
        .attr("width", 18)
        .attr("height", 18)
        .style("fill-opacity", 1)
        .style("fill", function (d) { return c(i); });
    d3.select("#legend-svg").append("text")
        .attr("x", 25)
        .attr("y", i*20+15)
        .text(group);
});
Share Improve this question edited Feb 24, 2015 at 18:43 pawamoy asked Feb 24, 2015 at 14:42 pawamoypawamoy 3,8261 gold badge29 silver badges47 bronze badges 2
  • Have you tried setting the x and y attributes of the rect? – Lars Kotthoff Commented Feb 24, 2015 at 14:45
  • Yes I tried that, nothing changed – pawamoy Commented Feb 24, 2015 at 15:08
Add a ment  | 

2 Answers 2

Reset to default 7

SVG elements like <rect> cannot be direct children of html <div> elements. You must put them inside an <svg> container element.

SET X + Y values for rect :

.attr("x", 50)
.attr("y", 50)

本文标签: javascriptd3 rect not showing upStack Overflow