svg-旋转< text>与< rect>

发布于 2025-02-11 18:09:10 字数 1380 浏览 0 评论 0原文

我正在为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. enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月如刀 2025-02-18 18:09:11

如果我正确读取您的代码,您当前正在对齐&lt; text&gt; eylements并垂直旋转 - 更好地使用水平偏移量

,可以将标签元素与特定x一起放置。相对于您当前的RECT中心进行协调。

示例第二个单元格

  <rect x="10" y="0" width="10" height="10" fill="magenta"/>
  <text x="15" y="5" width="10" dominant-baseline="central" text-anchor="middle" transform="rotate(-45, 15, 5)">02</text>

文本元素的X值(15)是当前单元格/rect的中心点。
主导baseline =“ Central”text-andor =“中间”只是简化了相对于先前的&lt; rect&gt; 。
我们将此X值复制到转换:

transform="rotate(-45, 15, 5)"

这样,我们确保标签围绕RECT中心旋转。
另外,考虑到某些浏览器仍然存在transform-origintransform-b​​ox(尤其是Safari的某些版本)

静态SVG示例

的问题,此方法仍然很健壮。

svg {
  display: block;
  border: 1px solid #ccc;
}

text {
  font-size: 5px
}
<svg width="50%" viewBox="0 0 30 10">
  <rect x="0" y="0" width="10" height="10" fill="green"/>
  <text x="5" y="5" width="10" dominant-baseline="central" text-anchor="middle" transform="rotate(-45, 5, 5)">01</text>
  
  <rect x="10" y="0" width="10" height="10" fill="magenta"/>
  <text x="15" y="5" width="10" dominant-baseline="central" text-anchor="middle" transform="rotate(-45, 15, 5)">02</text>
  
  <rect x="20" y="0" width="10" height="10" fill="cyan"/>
  <text x="25" y="5" width="10" dominant-baseline="central" text-anchor="middle" transform="rotate(-45, 25, 5)">03</text>
</svg>

<p>Add gaps</p>
<svg width="50%" viewBox="0 0 32 10">
  <rect x="0" y="0" width="10" height="10" fill="green"/>
  <text x="5" y="5" width="10" dominant-baseline="central" text-anchor="middle" transform="rotate(-45, 5, 5)">01</text>
  
  <rect x="11" y="0" width="10" height="10" fill="magenta"/>
  <text x="16" y="5" width="10" dominant-baseline="central" text-anchor="middle" transform="rotate(-45, 16, 5)">02</text>
  
  <rect x="22" y="0" width="10" height="10" fill="cyan"/>
  <text x="27" y="5" width="10" dominant-baseline="central" text-anchor="middle" transform="rotate(-45, 27, 5)">03</text>

</svg>

If I read your code correctly you're currently aligning your <text> elements vertically and rotate them - better use horizontal offsets

You could instead place your label elements with specific x coordinates relative to your current rect's center.

Example 2nd cell

  <rect x="10" y="0" width="10" height="10" fill="magenta"/>
  <text x="15" y="5" width="10" dominant-baseline="central" text-anchor="middle" transform="rotate(-45, 15, 5)">02</text>

The text element's x-value (15) is the center point of the current cell/rect.
dominant-baseline="central" and text-anchor="middle" just simplify the horizontal and vertical alignment relative to the preceding <rect>.
We copy this x-value to the transformation:

transform="rotate(-45, 15, 5)"

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 and transform-box (especially some versions of Safari)

Static svg example

svg {
  display: block;
  border: 1px solid #ccc;
}

text {
  font-size: 5px
}
<svg width="50%" viewBox="0 0 30 10">
  <rect x="0" y="0" width="10" height="10" fill="green"/>
  <text x="5" y="5" width="10" dominant-baseline="central" text-anchor="middle" transform="rotate(-45, 5, 5)">01</text>
  
  <rect x="10" y="0" width="10" height="10" fill="magenta"/>
  <text x="15" y="5" width="10" dominant-baseline="central" text-anchor="middle" transform="rotate(-45, 15, 5)">02</text>
  
  <rect x="20" y="0" width="10" height="10" fill="cyan"/>
  <text x="25" y="5" width="10" dominant-baseline="central" text-anchor="middle" transform="rotate(-45, 25, 5)">03</text>
</svg>

<p>Add gaps</p>
<svg width="50%" viewBox="0 0 32 10">
  <rect x="0" y="0" width="10" height="10" fill="green"/>
  <text x="5" y="5" width="10" dominant-baseline="central" text-anchor="middle" transform="rotate(-45, 5, 5)">01</text>
  
  <rect x="11" y="0" width="10" height="10" fill="magenta"/>
  <text x="16" y="5" width="10" dominant-baseline="central" text-anchor="middle" transform="rotate(-45, 16, 5)">02</text>
  
  <rect x="22" y="0" width="10" height="10" fill="cyan"/>
  <text x="27" y="5" width="10" dominant-baseline="central" text-anchor="middle" transform="rotate(-45, 27, 5)">03</text>

</svg>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文