简体   繁体   English

与矩形对齐的 D3 文本

[英]D3 text in alignment with rect

I have rect and a description text above chart -我在图表上方有矩形和描述文本 -

在此处输入图像描述

Issue is description text is not getting properly aligned with rects.问题是描述文本没有与矩形正确对齐。 Description is getting mixed into rects.描述正在混入矩形。 I want to start text after rect ends.我想在 rect 结束后开始文本。

I have below code -我有以下代码 -

var legend = svgColorCode.selectAll(".legend")
             .data(keys)
             .enter().append("g")
             .attr("class", "legend")
             .attr("transform", function (d, i) { return "translate(" +  (((keys.length-(i))*-25)) + "," + (height -190) + ")"; })
             .attr("fill", function (d, i) { return colors[i]; });
                    
legend.append("rect")
            .attr("x", (x,i)=> (padding.top * 2 + labHeight * (i))+40)
            .attr("width", 18)
            .attr("height", 18)
            .style("fill", function (d, i) { return colors[i]; })

legend.append("text")
            .attr("x", (x,i)=> (padding.top * 2 + labHeight * i)+110)
            .attr("y", 9)
            .attr("font-size","0.5rem")
            .attr("dy", ".35em")
            .style("text-anchor", "end")
            .text(function (d) { return d; });

HTML getting generated for color rect and description text -为颜色矩形和描述文本生成 HTML -

<svg>
  <g class="legend" transform="translate(-100,10)">
    <rect x="100" width="18" height="18"></rect>
    <text x="170" y="9" font-size="0.5rem" dy=".35em" style="text-anchor: end;">
      description text1
    </text>
  </g>
  <g class="legend" transform="translate(-75,10)" style="/* width: 10rem; */">
    <rect x="150" width="18" height="18"></rect>
    <text
      x="220"
      y="9"
      font-size="0.5rem"
      dy=".35em"
      style="text-anchor: end;/* margin-left: 29rem; *//* padding-left: 1rem; */"
    >
      description text2
    </text>
  </g>
  <g class="legend" transform="translate(-50,10)">
    <rect x="200" width="18" height="18"></rect>
    <text x="270" y="9" font-size="0.5rem" dy=".35em" style="text-anchor: end;">
      description text3
    </text>
  </g>
  <g class="legend" transform="translate(-25,10)">
    <rect x="250" width="18" height="18"></rect>
    <text x="320" y="9" font-size="0.5rem" dy=".35em" style="text-anchor: end;">
      description text4
    </text>
  </g>
</svg>;

How can I avoid mixing of color rect and description text ?如何避免混合颜色矩形和描述文本? and simply start description text after color rect?并在颜色矩形后简单地开始描述文本?

Change this to have text anchor as 'start'.将此更改为将文本锚定为“开始”。 You are putting the anchor of text at end so that is why text shifted left to align to end您将文本的锚点放在末尾,这就是为什么文本向左移动以对齐到末尾

Try reducing (padding.top * 2 + labHeight * i)+110) by around 50-60px and have text-anchor start for the text element.尝试将 (padding.top * 2 + labHeight * i)+110) 减少大约 50-60px 并为文本元素设置文本锚点。 You can adjust the margins based on style you want您可以根据需要的样式调整边距

 <svg> <g class="legend" transform="translate(-100,10)"> <rect x="100" width="18" height="18"></rect> <text x="120" y="9" font-size="0.5rem" dy=".35em" style="text-anchor: start;"> description text1 </text> </g> <g class="legend" transform="translate(-75,10)" style="/* width: 10rem; */"> <rect x="150" width="18" height="18"></rect> <text x="170" y="9" font-size="0.5rem" dy=".35em" style="text-anchor: start;/* margin-left: 29rem; *//* padding-left: 1rem; */" > description text2 </text> </g> <g class="legend" transform="translate(-50,10)"> <rect x="200" width="18" height="18"></rect> <text x="220" y="9" font-size="0.5rem" dy=".35em" style="text-anchor: start;"> description text3 </text> </g> <g class="legend" transform="translate(-25,10)"> <rect x="250" width="18" height="18"></rect> <text x="270" y="9" font-size="0.5rem" dy=".35em" style="text-anchor: start;"> description text4 </text> </g> </svg>;

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

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