简体   繁体   English

在SVG中将对象与动态宽度对齐

[英]Align objects with dynamic width in SVG

I am generating an SVG that needs to include some dynamic text elements. 我正在生成一个需要包含一些动态文本元素的SVG。 The general structure is as follows: 总体结构如下:

--------------------------------------------
|               <rect> <text> <rect> <text>|
|                                          |
|                                          |
|                                          |
--------------------------------------------

or to be a bit clearer - it's the legend at the top of a chart: 或者更清楚一点-它是图表顶部的图例:

在此处输入图片说明

The rectangles are just 5px by 5px colored boxes, however both text elements are dynamic in width. 矩形只有5px x 5px的彩色框,但是两个文本元素的宽度都是动态的。 The 4 elements then need to be aligned to the right. 然后需要将这4个元素向右对齐。

Is there a way to somehow 'float' the 4 elements next to each other. 有没有办法使这四个元素彼此“浮动”。 Everything I have looked at so far seems to indicate that each of these elements needs a explicit X and Y coordinate which I don't actually know until the text is rendered. 到目前为止,我看过的所有内容似乎都表明这些元素中的每一个都需要一个显式的X和Y坐标,直到渲染文本后我才真正知道它们。

I'm aware that there are javascript options available ('getBBox()' etc) but wondered if there was anything I could do just using the SVG DOM itself? 我知道有可用的javascript选项('getBBox()'等),但想知道是否可以使用SVG DOM本身做任何事情?

Here's my solution that doesn't have Javascript. 这是我没有Javascript的解决方案 Uses font-awesome with <tspan> to draw the entire text. <tspan>一起使用font-awesome可以绘制整个文本。

update: Added "roboto" font and dy for better alignment. 更新:添加了“ roboto”字体和dy以获得更好的对齐方式。

<link href='http://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">

<svg viewBox="0 0 400 400" preserveAspectRatio="xMinYMin meet" width="400px" height="400px" >
  <text text-anchor="end" x="400" y="20">
    <tspan dy="1" font-size="8pt" font-family="roboto" class="fa fa-fw" fill="skyblue"> &#xf0c8;</tspan> 
    <tspan dy="-2" font-size="8pt" font-family="roboto" >Gross Profit % &nbsp;&nbsp;</tspan>
    <tspan dy="2" font-size="8pt" font-family="roboto" class="fa fa-fw" fill="green"> &#xf0c8;</tspan> 
    <tspan dy="-2" font-size="8pt" font-family="roboto" >Op. Profit % &nbsp;&nbsp;</tspan>
    <tspan dy="2" font-size="8pt" font-family="roboto" class="fa fa-fw" fill="orange"> &#xf0c8;</tspan> 
    <tspan dy="-2" font-size="8pt" font-family="roboto" >Net Profit after tax % </tspan>
  </text>
</svg>

You can right-align text using text-anchor="end" . 您可以使用text-anchor="end"右对齐文本。

 <svg width="100%" height="100%" viewBox="0 0 500 500" preserveAspectRatio="xMaxYMin"> <rect x="462" y="2" width="36" height="36" fill="blue"/> <rect x="422" y="2" width="36" height="36" fill="green"/> <rect x="382" y="2" width="36" height="36" fill="red"/> <text x="378" y="15" font-size="15px" text-anchor="end">Some right justified text</text> <text x="378" y="35" font-size="15px" text-anchor="end">Shorter text</text> </svg> 

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

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