D3js(四):箭頭arrow

D3js方式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="http://d3js.org/d3.v3.min.js"></script>

</head>
<body>
    <svg width="300" height="200">
    </svg>
</body>

<script>

    // 獲取 svg
    var svg = d3.select('svg');

    //箭頭
    var marker =
        svg.append("marker")
            .attr("id", "arrow")
            .attr("markerUnits","strokeWidth")//設置爲strokeWidth箭頭會隨着線的粗細發生變化
            .attr("viewBox", "0 0 12 12")//座標系的區域
            .attr("refX", 6)//箭頭座標
            .attr("refY", 6)
            .attr("markerWidth", 12)
            .attr("markerHeight", 12)
            .attr("orient", "auto")//繪製方向,可設定爲:auto(自動確認方向)和 角度值
            .append("path")
            .attr("d", "M2,2 L10,6 L2,10 L6,6 L2,2")//箭頭的路徑
            .attr('fill', '#f00');//箭頭顏色

    // 繪製直線
    var line = svg.append("line")
        .attr("x1",50)
        .attr("y1",10)
        .attr("x2",200)
        .attr("y2",50)
        .attr("stroke","red")
        .attr("stroke-width",2)
        .attr("marker-end","url(#arrow)");

    // 繪製曲線
    var curve = svg.append("path")
        .attr("d","M20,70 T80,100 T160,80 T200,90")
        .attr("fill","white")
        .attr("stroke","blue")
        .attr("stroke-width",2)
        .attr("marker-start","url(#arrow)")
        .attr("marker-mid","url(#arrow)")
        .attr("marker-end","url(#arrow)");


</script>

</html>

html+d3方式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<svg width="300" height="200">
    <defs>
        <marker
                id="arrow"
                markerUnits="strokeWidth"
                markerWidth="12"
                markerHeight="12"
                viewBox="0 0 12 12"
                refX="6"
                refY="6"
                orient="auto">
            <path d="M2,2 L10,6 L2,10 L6,6 L2,2" style="fill: #f00;"></path>
        </marker>
    </defs>
    <line x1="0" y1="0" x2="200" y2="50"  stroke="red" stroke-width="2" marker-end="url(#arrow)"/> -->

    <path d="M20,70 T80,100 T160,80 T200,90" fill="white" stroke="red" stroke-width="2" marker-start="url(#arrow)" marker-mid="url(#arrow)" marker-end="url(#arrow)"/>
</svg>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章