简体   繁体   English

SVG矩形边框,不描边

[英]SVG rect border, not stroke

I am making a d3 graph and trying to put a border around my rect elements. 我正在制作d3图,并尝试在我的rect元素周围放置边框。 The rect elements are appended to a cell and the text elements are appended to the same cell. rect元素被附加到一个单元格,而text元素被附加到同一单元格。 Thus if I change the stroke in the rect I lose all the text for some reason, and if I change the stroke in the cell the borders and fonts change too. 因此,如果我在矩形中更改笔划,由于某种原因我会丢失所有文本,并且如果在单元格中更改笔划,边框和字体也会更改。

This is a portion of my code for drawing the graph. 这是我用于绘制图形的代码的一部分。

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);});

Additionally, I thought about creating lines and drawing four lines around each rect element, but was wondering if there is an easier way. 另外,我考虑过要在每个rect元素周围创建线并绘制四条线,但想知道是否有更简单的方法。 Thanks. 谢谢。

I didn't check fully through your source, it would also be helpful to work with jsbin, codepen, jsfiddle or other online platforms to show your problem. 我没有完全检查您的来源,与jsbin,codepen,jsfiddle或其他在线平台一起使用也可以帮助您显示问题。

Actually I think you just have misinterpreted the SVG presentation attributes and their styling with CSS. 实际上,我认为您只是误解了SVG表示属性及其使用CSS的样式。 For SVG elements only SVG presentation attributes are valid in CSS. 对于SVG元素,只有SVG表示属性在CSS中有效。 This means there is no border property as you have it in your code. 这意味着您的代码中没有边框属性。 Also note that for <text> elements the fill color is the font-body color and the stroke is the outline of the font. 还要注意,对于<text>元素,填充颜色是字体-主体颜色,笔触是字体的轮廓。 Consider that stroke and fill are inherited down to child element which means that if you have a rectangle with a stroke style and some containing text element that they will have the stroke applied as outline and you'd need to override the styles there. 考虑到笔触和填充是继承到子元素的,这意味着,如果您有一个带有笔触样式的矩形,并且包含一些包含文本元素的矩形,则它们会将笔触应用为轮廓线,并且您需要覆盖那里的样式。

Hope you can solve your issue. 希望您能解决您的问题。

Cheers Gion 干杯G园

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

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