1
<script type="text/javascript">
    var divElem = d3.select("#svgpathSVGdata");

正方形的数据

var jsonsquare = [{
    x: 40,
    y: 170,
    width: 120,
    height: 120,
    label: "technology"
    },

    ];

使用已经声明的宽度和高度设置画布

var svgcanvas = divElem.append("svg:svg")
               .attr("width", w).attr("height", h);

这是相关的编码......由于某种原因它不会生成正方形

var square = svgcanvas.selectAll("rect").data(jsonsquare);

circle.enter().append('svg:rect').append("svg:g")
.attr('opacity', 0)
.attr("cursor", "pointer");
}).attr("x", function(d) {
    return d.x;
}).attr("y", function(d) {
    return d.y;
}).attr("width", function(d) {
    return d.width;
}).attr("height", function(d) {
    return d.height;
});

将标签链接到正方形

svgcanvas.selectAll("text").data(jsonsquare).enter().append("svg:text").text(function(d) {
    return d.label;
}).attr("x", function(d) {
    return d.x + 10;
}).attr("y", function(d) {
    return d.y + 10;
});





</script>​​​​​
4

1 回答 1

0

您将矩形的属性分配给svg:g它下面的组(即 )。在下面的行中,删除.append("svg:g")它应该可以工作。

square.enter().append('svg:rect').append("svg:g")

此外,您将不透明度设置为零:

.attr('opacity', 0)

这使您的矩形不可见。将其设置为 1 以获得完全可见性,或者将其设置为中间的某个数字以获得半透明填充效果。

于 2012-10-04T19:59:09.647 回答