【问题标题】:How should I template this svg snippet?我应该如何模板化这个 svg 片段?
【发布时间】:2015-10-22 10:45:36
【问题描述】:

我需要制作一个模板,通过查找特殊标签并替换这些值来为下面的坐标插入新值。

我给出了一个 SVG,下面是我需要更改的一个小 sn-p,以便我可以模板/添加占位符,因为下面的值将是动态的。

<g id="Layer_1">
    <g id="shape1">
        <polygon class="poly1" points="6.7,105.4 -19.2,190.5 -20.4,123.5 1.6,143.6 "/>
    </g>
    <polygon id="shape2" class="poly1" points="-20.9,300.3 -37.7,103.3 -10.9,204.3 -0.1,233.6"/>
    <circle id="circleSolid" class="poly2" cx="6.7" cy="306.1" r="10.3"/>
    <g id="circleDashed">
        <circle class="poly3" cx="2.6" cy="304.7" r="10.3"/>
    </g>
    <text transform="matrix(1 0 0 1 -40.7301 191.3002)" class="poly1 poly3 poly4">Poly1</text>
    <text transform="matrix(1 0 0 1 -40.7302 200.3002)" class="poly1 poly3 poly4">Poly2</text>
</g>
<g id="Layer_7">
    <line id="newShape1" class="poly5" x1="-30.7" y1="170.7" x2="12.2" y2="171.7"/>
    <line id="newShape2" class="poly5" x1="-11.5" y1="160.7" x2="0.1" y2="200.3"/>
</g>>

我想知道我不能只在下面的 html 标记中添加[x1] 标记以进行模板化。我应该只使用类来插入新的动态值吗?我应该如何模板化 SVG 绘图?

我将在 C# 中动态插入新值。

【问题讨论】:

    标签: c# html css templates svg


    【解决方案1】:

    您必须使用 C# 函数 String.Format(),它将接收您的字符串并将字符串中的任何变量替换为实际输入的文本。

    例如:

    string name = "Scott";
    string output = String.Format("Hello {0}", name); ///outputs Hello Scott
    

    因此,如果您想在 SVG 中生成圆形元素,您可以编写如下代码:

    string cx = '6.7';
    string cy = '306.1';
    string r = 10.3';
    string result = String.Format("<circle id="circleSolid" class="poly2" cx="{0}" cy="{1}" r="{2}"/>", cx, cy, r);
    

    将输出以下内容:

    <circle id="circleSolid" class="poly2" cx="6.7" cy="306.1" r="10.3"/>
    

    【讨论】:

    • 我唯一关心的是 String.Format 是,如果我有一个巨大的字符串文件,那可能不是最好的方法。这就是为什么我在考虑模板并查找和替换 String.Format() 中的值。
    猜你喜欢
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多