svg-旋转< text>与< rect>
我正在为D3写一个传奇。从左到右绘制的十个矩形。我想要的结果是将文本放置在其相关单元格上方,垂直放置,但与右侧略微角度。我已经应用了旋转,但是我缺少一些东西,因为它将它们作为一个组并将它们全部旋转为一条线,而不是相对于他们的兄弟姐妹单元旋转。
有人可以推荐一些属性或样式提示,或者可能会分组元素的不同方式,以便文本元素在自己的中心周围单独旋转,而不是单行旋转?
这是我的代码的当前状态:
<svg preserveAspectRatio="xMinYMin meet" viewBox={`0 0 800 70`}>
<g transform={`translate(${[dms.marginTop, dms.marginLeft].join(',')})`}>
<g>
{range(10).map((d) => (
<>
<rect
key={`${d}_legendCell`}
width={cellSize - 1.5}
height={cellSize - 1.5}
fill={colorScale(Number(legendBands(String(d))) + interval)}
x={d * cellSize}
></rect>
<text
key={`${d}_legendLabel`}
fontWeight="300"
fontSize="12"
width="100"
y={cellSize * d}
transform="rotate(290)"
dy=".85rem"
>
{Number(legendBands(String(d))).toFixed(1)}
</text>
</>
))}
</g>
</g>
</svg>
tia!
I'm writing a legend for d3. Ten rectangles plotted from left to right. The outcome I would like is to position the text above its relevant cell, positioned vertically, but at a slight angle to the right. I have applied the rotation, but there's something about this that I'm missing, as it treats them as a group and rotates them all in a line, instead of rotating them relative to their sibling cell.
Can someone recommend some attributes or style tips or perhaps a different manner of grouping the elements so that the text elements rotate individually around their own centers, and not as a single line?
Here's the current state of my code:
<svg preserveAspectRatio="xMinYMin meet" viewBox={`0 0 800 70`}>
<g transform={`translate(${[dms.marginTop, dms.marginLeft].join(',')})`}>
<g>
{range(10).map((d) => (
<>
<rect
key={`${d}_legendCell`}
width={cellSize - 1.5}
height={cellSize - 1.5}
fill={colorScale(Number(legendBands(String(d))) + interval)}
x={d * cellSize}
></rect>
<text
key={`${d}_legendLabel`}
fontWeight="300"
fontSize="12"
width="100"
y={cellSize * d}
transform="rotate(290)"
dy=".85rem"
>
{Number(legendBands(String(d))).toFixed(1)}
</text>
</>
))}
</g>
</g>
</svg>
TIA!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确读取您的代码,您当前正在对齐
&lt; text&gt;
eylements并垂直旋转 - 更好地使用水平偏移量,可以将标签元素与特定
x
一起放置。相对于您当前的RECT中心进行协调。示例第二个单元格
文本元素的X值(15)是当前单元格/rect的中心点。
主导baseline =“ Central”
和text-andor =“中间”
只是简化了相对于先前的&lt; rect&gt; 。
我们将此X值复制到转换:
这样,我们确保标签围绕RECT中心旋转。
另外,考虑到某些浏览器仍然存在
transform-origin
和transform-box
(尤其是Safari的某些版本)静态SVG示例
的问题,此方法仍然很健壮。
If I read your code correctly you're currently aligning your
<text>
elements vertically and rotate them - better use horizontal offsetsYou could instead place your label elements with specific
x
coordinates relative to your current rect's center.Example 2nd cell
The text element's x-value (15) is the center point of the current cell/rect.
dominant-baseline="central"
andtext-anchor="middle"
just simplify the horizontal and vertical alignment relative to the preceding<rect>
.We copy this x-value to the transformation:
This way we ensure the label is rotated around the rect's center.
Also, this method is quite robust, considering that some browsers still have problems with
transform-origin
andtransform-box
(especially some versions of Safari)Static svg example