【发布时间】:2018-10-25 18:34:24
【问题描述】:
我需要使用 D3Js 创建一个 SVG,
我尝试使用 D3JS 创建 SVG 图像
以上是使用 D3JS 生成的。
<svg width="236.6" height="158" viewBox="0, 0, 236.6, 158"><image href="/img/icon_threat_level_high.png" width="23.1" height="23.1" x="49.5" y="30.5" style="fill: rgb(255, 51, 51);"></image><image href="/img/icon_threat_level_high.png" width="23.1" height="23.1" x="49.5" y="65.5" style="fill: rgb(255, 51, 51);"></image><image href="/img/icon_threat_level_high.png" width="23.1" height="23.1" x="49.5" y="100.5" style="fill: rgb(255, 51, 51);"></image><g><text x="85.19999999999999" y="30.5" style="font-family: Arial; font-size: 28px; font-weight: normal; font-style: normal; font-stretch: normal; line-height: 1; letter-spacing: normal; fill: rgb(0, 103, 171); dominant-baseline: central;">10</text></g><g><text x="85.19999999999999" y="65.5" style="font-family: Arial; font-size: 28px; font-weight: normal; font-style: normal; font-stretch: normal; line-height: 1; letter-spacing: normal; fill: rgb(0, 103, 171); dominant-baseline: central;">08</text></g><g><text x="85.19999999999999" y="100.5" style="font-family: Arial; font-size: 28px; font-weight: normal; font-style: normal; font-stretch: normal; line-height: 1; letter-spacing: normal; fill: rgb(0, 103, 171); dominant-baseline: central;">03</text></g></svg>
查询:
- 第一个图像标签的y为“30.5”,同样设置为第一个文本标签的y。但是文本标签在图标上方一点点。
这是我第一次使用 D3JS 处理 SVG。 使用 D3 设计 SVG 的最佳方法是什么
我的想法是
<svg><g><image></image><text>10</text><text>GB</text><text>Name</text></g></svg>
或者
<svg><g><image></image><text><tspan>10</tspan><tspan>GB</tspan><tspan>Name</tspan></text></g></svg>
由于“35”、“GB”和“BitTorrent”有不同的字体大小、颜色,我更喜欢在文本中选择 tspan 并设置这些 tspan 的样式
请建议设计 SVG
【问题讨论】:
-
文本元素上的
y属性定位“基线”或文本底部(不包括悬挂在该线下方的下降)。因此,如果您使用与圆心 + 半径相匹配的 y 值,那么圆的底部将与文本的底部对齐。 -
是的,使用
<tspan>元素是个好主意,这样它们就可以有不同的样式。每个都可以有自己的dy属性,以相对于文本框的其余部分向上或向下移动它。 -
感谢史蒂夫,将半径 11.55 添加到文本的 y 后它可以工作
标签: javascript d3.js svg