繁体   English   中英

SVG矩形边框,不描边

[英]SVG rect border, not stroke

我正在制作d3图,并尝试在我的rect元素周围放置边框。 rect元素被附加到一个单元格,而text元素被附加到同一单元格。 因此,如果我在矩形中更改笔划,由于某种原因我会丢失所有文本,并且如果在单元格中更改笔划,边框和字体也会更改。

这是我用于绘制图形的代码的一部分。

this.svg = d3.select("#body").append("div")

          .attr("class", "chart")
          .style("position", "relative")
          .style("width", (this.w +this.marginTree.left+this.marginTree.right) + "px")
          .style("height", (this.h + this.marginTree.top + this.marginTree.bottom) + "px")
          .style("left", this.marginTree.left +"px")
          .style("top", this.marginTree.top + "px")
        .append("svg:svg")
          .attr("width", this.w)
          .attr("height", this.h)
        .append("svg:g")
          .attr("transform", "translate(.5,.5)");


        this.node = this.root = this.nestedJson;



        var nodes = this.treemap.nodes(this.root)
            .filter(function(d) { return !d.children; });

        this.tip = d3.tip()
              .attr('class', 'd3-tip')
              .html(function(d) {
                return "<span style='color:white'>" + (d.name+",\n "+d.size) + "</span>";
              })
        this.svg.call(this.tip);



        var cell = this.svg.selectAll("g")
            .data(nodes)
            .enter().append("svg:g")
            .attr("class", "cell")
            .call(this.position)
            .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
            .on("click", function(d) { return this.zoom(this.node == d.parent ? this.root : d.parent); })
            .style("border",'black');

        var borderPath = this.svg.append("rect")
            .attr("x", this.marginTree.left)
            .attr("y", this.marginTree.top)
            .attr("height", this.h - this.marginTree.top - this.marginTree.bottom )
            .attr("width", this.w - this.marginTree.left - this.marginTree.right)
            .style("stroke", 'darkgrey')
            .style("fill", "none")
            .style("stroke-width", '3px');


        cell.append("svg:rect")

            .attr("id", function(d,i) { return "rect-" + (i+1); })
            .attr("class","highlighting2")
                .attr("title", function(d) {return (d.name+", "+d.size);})
                .attr("data-original-title", function(d) {return (d.name+",\n "+d.size);})
          .attr("width", function(d) { return d.dx - 1; })
          .attr("height", function(d) { return d.dy ; })
          .on('mouseover', this.tip.show)
                .on('mouseout', this.tip.hide)
          .style("fill", function(d) {return coloring(d.color);});


        cell.append("svg:text")
            .attr("class", "treemap-text nameTexts") 
            .attr("id", function(d,i) { return "name-" + (i+1); })
            .attr("x", cellMargin)  
            .attr("y", function(d) {  return parseInt($('.treemap-text').css('font-size'))+cellMargin; })
          .text(function(d) {return (d.name);});

       cell.append("svg:text")
            .attr("class", "treemap-text sizeTexts") 
            .attr("id", function(d,i) { return "size-" + (i+1); })  
            .attr("x", cellMargin)  
            .attr("y", function(d) {  return 2*parseInt($('.treemap-text').css('font-size'))+2*cellMargin; })
            .text(function(d) {return (d.size);});

另外,我考虑过要在每个rect元素周围创建线并绘制四条线,但想知道是否有更简单的方法。 谢谢。

我没有完全检查您的来源,与jsbin,codepen,jsfiddle或其他在线平台一起使用也可以帮助您显示问题。

实际上,我认为您只是误解了SVG表示属性及其使用CSS的样式。 对于SVG元素,只有SVG表示属性在CSS中有效。 这意味着您的代码中没有边框属性。 还要注意,对于<text>元素,填充颜色是字体-主体颜色,笔触是字体的轮廓。 考虑到笔触和填充是继承到子元素的,这意味着,如果您有一个带有笔触样式的矩形,并且包含一些包含文本元素的矩形,则它们会将笔触应用为轮廓线,并且您需要覆盖那里的样式。

希望您能解决您的问题。

干杯G园

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM