D3.js - path使用

path

• path元素是SVG基本形状中最强大的一个,它不仅能创建其他基本形状,还能创建更多其他形状。你可以用path元素绘制矩形(直角矩形或者圆角矩形)、圆形、椭圆、折线形、多边形,以及一些其他的形状,例如贝塞尔曲线、2次曲线等曲线。
• path元素的形状是通过属性d来定义的,属性d的值是一个“命令+参数”的序列(见下页)
• Path作为SVG提供的标签之一,是实现众多可视化方案的基础
• 每一个命令都用一个关键字母来表示,比如,字母“M”表示的是“Move to”命令,当解析器读到这个命令时,它就知道你是打算移动到某个点。跟在命令字母后面的,是你需要移动到的那个点的x和y轴坐标。比如移动到(10,10)这个点的命令,应该写成“M 10 10”。这一段字符结束后,解析器就会去读下一段命令。每一个命令都有两种表示方式,一种是用大写字母,表示采用绝对定位。另一种是用小写字母,表示采用相对定位。
• 因为属性d采用的是用户坐标系统,所以不需标明单位。

案列如下:

<!DOCTYPE html>
<html>
  <head>
    <title>Data Visualization!</title>
  </head>
  <body>
    <svg width="100px" height="100px">
        <path d="M0 0 H 90 V 90 H 10 L 10 10" /> 
        <!-- Points -->
        <circle cx="10" cy="10" r="2" fill="red"/>
        <circle cx="90" cy="90" r="2" fill="red"/>
        <circle cx="90" cy="10" r="2" fill="red"/>
        <circle cx="10" cy="90" r="2" fill="red"/>
    </svg>
    <svg width="100px" height="100px" fill="green">
        <path d="M10 10 H 90 V 90 H 10 Z" fill="green" stroke="black"/>
         <!-- Points -->
         <circle cx="10" cy="10" r="2" fill="red"/>
         <circle cx="90" cy="90" r="2" fill="red"/>
         <circle cx="90" cy="10" r="2" fill="red"/>
         <circle cx="10" cy="90" r="2" fill="red"/>
    </svg>
    <svg width="100px" height="100px">
        <path d="M10 10 h 80 v 80 h -80 Z" fill="pink" stroke="black" stroke-width="3"/>
        <!-- Points -->
        <circle cx="10" cy="10" r="2" fill="red"/>
        <circle cx="90" cy="90" r="2" fill="red"/>
        <circle cx="90" cy="10" r="2" fill="red"/>
        <circle cx="10" cy="90" r="2" fill="red"/>
    </svg>
    <svg height="210" width="400">
      <path d="M150 0 L75 200 L225 200 Z" transform="translate(150,100)"/>
      <path d="M150 0 L75 200 L225 200 Z" />
    </svg> 
    <br>
    <!--三次贝塞尔曲线-->
    <svg width="190px" height="160px">
        <path d="M130 110 C 120 140, 180 140, 170 110" stroke="black" fill="transparent"/>
        <circle cx="130" cy="110" r="2" fill="red"/>
        <circle cx="120" cy="140" r="2" fill="red"/>
        <line x1="130" y1="110" x2="120" y2="140" style="stroke:rgb(255,0,0);stroke-width:2"/>
        <circle cx="180" cy="140" r="2" fill="red"/>
        <circle cx="170" cy="110" r="2" fill="red"/>
        <line x1="180" y1="140" x2="170" y2="110" style="stroke:rgb(255,0,0);stroke-width:2"/>
    </svg>
    <!--三次贝塞尔曲线简写-->
    <svg width="190px" height="160px">
        <path d="M10 80 C 40 10, 65 10, 95 80 S 150 150, 180 80" stroke="black" fill="transparent"/>
        <circle cx="10" cy="80" r="2" fill="red"/>
        <circle cx="40" cy="10" r="2" fill="red"/>
        <line x1="10" y1="80" x2="40" y2="10" style="stroke:rgb(255,0,0);stroke-width:1"/>

        <circle cx="65" cy="10" r="2" fill="red"/>
        <circle cx="95" cy="80" r="2" fill="red"/>
        <line x1="65" y1="10" x2="95" y2="80" style="stroke:rgb(255,0,0);stroke-width:1"/>

        <circle cx="125" cy="150" r="2" fill="blue"/>
        <circle cx="180" cy="80" r="2" fill="red"/>
        <circle cx="150" cy="150" r="2" fill="red"/>
        <line x1="95" y1="80" x2="125" y2="150" style="stroke:blue;stroke-width:1"/>
        <line x1="180" y1="80" x2="150" y2="150" style="stroke:rgb(255,0,0);stroke-width:1"/>
    </svg>
    <!--二次贝塞尔曲线-->
    <svg width="190px" height="160px">
        <path d="M10 80 Q 95 10 180 80" stroke="black" fill="transparent"/>
        <!--Points-->
        <circle cx="10" cy="80" r="2" fill="red"/>
        <circle cx="95" cy="10" r="2" fill="red"/>
        <circle cx="180" cy="80" r="2" fill="red"/>
        <line x1="10" y1="80" x2="95" y2="10" style="stroke:rgb(255,0,0);stroke-width:1"/>
        <line x1="95" y1="10" x2="180" y2="80" style="stroke:rgb(255,0,0);stroke-width:1"/>
    </svg>  
    <!--二次贝塞尔曲线简写-->
    <svg width="190px" height="160px">
        <path d="M10 80 Q 52.5 10, 95 80 T  180 80" stroke="black" fill="transparent"/>

        <circle cx="10" cy="80" r="2" fill="red"/>
        <circle cx="52.5" cy="10" r="2" fill="red"/>
        <line x1="10" y1="80" x2="52.5" y2="10" style="stroke:rgb(255,0,0);stroke-width:1"/>

        <circle cx="95" cy="80" r="2" fill="red"/>
        <line x1="95" y1="80" x2="52.5" y2="10" style="stroke:rgb(255,0,0);stroke-width:1"/>

        <circle cx="180" cy="80" r="2" fill="blue"/>
        <circle cx="137.5" cy="150" r="2" fill="blue"/>
        <line x1="95" y1="80" x2="137.5" y2="150" style="stroke:rgb(0,0,255);stroke-width:1"/>
        <line x1="137.5" y1="150" x2="180" y2="80" style="stroke:rgb(0,0,255);stroke-width:1"/>
    </svg>
    <svg width="320px" height="320px">
        <path d="M10 315
                L 110 215
                A 30 50 0 0 1 162.55 162.45
                L 172.55 152.45
                A 30 50 -45 0 1 215.1 109.9
                L 315 10" stroke="black" fill="green" stroke-width="2" fill-opacity="0.5"/>
    </svg>
    <svg height="400" width="450">
      <path id="lineAB" d="M 100 350 l 150 -300" stroke="red" stroke-width="3" fill="none" />
      <path id="lineBC" d="M 250 50 l 150 300" stroke="red" stroke-width="3" fill="none" />
      <path d="M 175 200 l 150 0" stroke="green" stroke-width="3" fill="none" />
      <path d="M 100 350 q 150 -300 300 0" stroke="blue" stroke-width="5" fill="none" />
      <!-- Mark relevant points -->
      <g stroke="black" stroke-width="3" fill="black">
        <circle id="pointA" cx="100" cy="350" r="3" />
        <circle id="pointB" cx="250" cy="50" r="3" />
        <circle id="pointC" cx="400" cy="350" r="3" />
      </g>
      <!-- Label the points -->
      <g font-size="30" font="sans-serif" fill="black" stroke="none" text-anchor="middle">
        <text x="100" y="350" dx="-30">A</text>
        <text x="250" y="50" dy="-10">B</text>
        <text x="400" y="350" dx="30">C</text>
      </g>
    </svg>
  </body>
</html>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
d3.js是一个用于数据可视化的JavaScript库,它提供了丰富的功能和工具来创建交互式和动态的数据可视化图表。其中,svg的path标签在d3.js中被称为"可以组成任何形状的形状"。d3.js提供了多种path生成器,可以用来创建不同类型的路径。 以下是一些常用的d3.js svg path生成器: 1. d3.svg.line() - 线path生成器 2. d3.svg.line.radial() - 径向path生成器 3. d3.svg.area() - 区域path生成器 4. d3.svg.area.radial() - 径向区域path生成器 5. d3.svg.arc() - 圆与圆弧path生成器 6. d3.svg.symbol() - 符号path生成器 7. d3.svg.chord() - chord path生成器 8. d3.svg.diagonal() - diagonal path生成器 9. d3.svg.diagonal.radial() - diagonal radial path生成器 这些生成器可以通过调用相应的方法来生成路径,并且还可以使用其他方法来改变其属性。此外,这些生成器还支持链式调用,可以方便地进行多个操作。 范例: ```javascript // 创建一个svg元素 var svg = d3.select("body") .append("svg") .attr("width", 500) .attr("height", 500); // 创建一个线path生成器 var lineGenerator = d3.svg.line(); // 创建一个路径并设置其属性 var path = svg.append("path") .attr("d", lineGenerator([[0, 0], [100, 100], [200, 50]])) .attr("stroke", "black") .attr("fill", "none"); // 创建一个圆与圆弧path生成器 var arcGenerator = d3.svg.arc() .innerRadius(50) .outerRadius(100) .startAngle(0) .endAngle(Math.PI); // 创建一个路径并设置其属性 var path = svg.append("path") .attr("d", arcGenerator()) .attr("transform", "translate(250, 250)") .attr("fill", "red"); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值