D3文本与直接对齐
我有矩形和描述文本上方图表 -
问题是描述文本与rects无法正确对齐。描述变成了矩形。我想在rect结束后启动文本。
我有以下代码 -
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为颜色ret和描述文本生成 -
<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>;
如何避免混合颜色rect和描述文本?并且只需启动描述颜色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.
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 -
<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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其更改为“开始”的文本锚。您将文本的锚定在最后,所以这就是为什么文本向左移动以结束的原因
尝试减少(padding.top * 2 + labheight * i)+110)以大约50-60px,并具有文本启动文本的启动元素。您可以根据所需的样式调整利润率
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. You can adjust the margins based on style you want