javascript - 如何使用 D3JS 创建复杂的 SVG 形状?

标签 javascript vector svg d3.js

我正在尝试自定义 D3 JS 示例之一,但不知道如何向其中添加复杂的图形。

这是我试图修改的脚本:http://bl.ocks.org/jmgimeno/1665141

我想为底部的处理程序添加更好的图形。 就像 iPhone 视频编辑控件的样子:http://bit.ly/ZDjnOq

现在,我想,唯一的选择就是使用复杂的 SVG 图形。 因此,我为图形控件创建了一个 SVG 文件,看起来与 iPhone 控件类似。

我的 SVG 文件如下所示:

    <?xml version="1.0" encoding="iso-8859-1"?>
<!-- Generator: Adobe Illustrator 16.0.4, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="37.5px"
     height="20.25px" viewBox="0 0 37.5 20.25" enable-background="new 0 0 37.5 20.25" xml:space="preserve">
<g id="Layer_2">
</g>
<g id="Layer_1">
    <path fill="none" stroke="#F28A1A" stroke-miterlimit="10" d="M34.442,15.665c0,1.565-1.27,2.835-2.835,2.835H5.594
        c-1.565,0-2.835-1.27-2.835-2.835V3.834C2.759,2.269,4.028,1,5.594,1h26.014c1.565,0,2.835,1.269,2.835,2.834V15.665z"/>
    <g>
        <path fill="#FFFFFF" stroke="#626161" stroke-width="0.25" stroke-miterlimit="10" d="M4.342,12.298
            c0,0.874-0.709,1.583-1.584,1.583l0,0c-0.874,0-1.583-0.709-1.583-1.583V7.131c0-0.874,0.709-1.583,1.583-1.583l0,0
            c0.875,0,1.584,0.709,1.584,1.583V12.298z"/>
        <line fill="none" stroke="#626161" stroke-width="0.25" stroke-miterlimit="10" x1="1.883" y1="9.715" x2="3.466" y2="9.715"/>

            <line id="XMLID_16_" fill="none" stroke="#626161" stroke-width="0.25" stroke-miterlimit="10" x1="1.883" y1="8.976" x2="3.466" y2="8.976"/>

            <line id="XMLID_15_" fill="none" stroke="#626161" stroke-width="0.25" stroke-miterlimit="10" x1="1.883" y1="10.369" x2="3.466" y2="10.369"/>
    </g>
    <g>
        <path fill="#FFFFFF" stroke="#626161" stroke-width="0.25" stroke-miterlimit="10" d="M36.025,12.298
            c0,0.874-0.709,1.583-1.584,1.583l0,0c-0.874,0-1.583-0.709-1.583-1.583V7.131c0-0.874,0.709-1.583,1.583-1.583l0,0
            c0.875,0,1.584,0.709,1.584,1.583V12.298z"/>
        <line fill="none" stroke="#626161" stroke-width="0.25" stroke-miterlimit="10" x1="33.566" y1="9.715" x2="35.149" y2="9.715"/>

            <line id="XMLID_14_" fill="none" stroke="#626161" stroke-width="0.25" stroke-miterlimit="10" x1="33.566" y1="8.976" x2="35.149" y2="8.976"/>

            <line id="XMLID_13_" fill="none" stroke="#626161" stroke-width="0.25" stroke-miterlimit="10" x1="33.566" y1="10.369" x2="35.149" y2="10.369"/>
    </g>
</g>
</svg>

不知何故,当我使用上面的代码时,我无法看到 SVG 图形。

我需要某种更简单形式的 SVG 吗?或者还有更多的事情。

最佳答案

可以通过将 svg 元素信息放入 d3 可以用作数据的数组来渲染复杂路径:

http://bl.ocks.org/AndrewStaroscik/5220771

关于javascript - 如何使用 D3JS 创建复杂的 SVG 形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15564050/

相关文章:

c++ - 访问类的元素

r - 如何根据引用向量找到元素的位置(索引)?

html - 使用背景图像填充 SVG 路径元素,无需平铺或缩放

svg - 可以在 SVG 中创建(或伪造)二维渐变吗?

函数调用内的javascript内联注释

javascript - jQuery 在滚动时添加和删除类

javascript - "normal"表单上的 JQuery BlockUI

javascript - 通过值比较合并 JSON 对象

android - 能否将 VectorDrawable pathData 转换为 Path 对象

javascript - D3 用路径绘制时填充形状