【问题标题】:How to draw lines between circles?如何在圆圈之间画线?
【发布时间】:2014-09-21 12:06:03
【问题描述】:

我正在尝试绘制三个圆圈并在每个圆圈之间绘制连接线。

最终目标是使用 json 配置来配置连接哪些圆圈,但在此之前我只是尝试使用回调和硬代码值连接圆圈。

这是我目前所拥有的:

<!DOCTYPE html>
<html>
  <head>
      <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
   </head>
<body style="overflow: hidden;">
<div id="canvas"  style="overflow: hidden;"></div>

<script type="text/javascript">
    var graph = {
        "nodes":[
            {"name":"1","group":1, "x" : 100, "y" : 100 , r : 20},
            {"name":"2","group":1, "x" : 200, "y" : 150 ,r : 30},
            {"name":"3","group":2 , "x" : 300, "y" : 250 , r : 50}
        ],
        "links":[
            {"source":1,"target":0,"value":1}
        ]
    }

    var width = 2000;
    var height = 500;

    var svg = d3.select("#canvas").append("svg")
            .attr("width", width)
            .attr("height", height)
        .append("g");

    var lines = svg.attr("class", "line")
            .selectAll("line").data(graph.links)
            .enter().append("line")
            .attr("x1", function(d) { return 50 })
            .attr("y1", function(d) { return 50 })
            .attr("x2", function(d) { return 100 })
            .attr("y2", function(d) {  return 100 })

    var circles = svg.selectAll("circle")
            .data(graph.nodes)
            .enter().append("circle")
            .style("stroke", "gray")
            .style("fill", "white")
            .attr("r", function(d, i){ return d.r })
            .attr("cx", function(d, i){ return d.x })
            .attr("cy", function(d, i){ return d.y })

</script>
</body>
</html>

但是没有画线。每个圆圈应包含一条将其连接到另一个圆圈的线。 我只是对 x1,y1,x2,y2 坐标进行硬编码,但我将使用 其他圆的坐标以确定线的位置。为什么不画线?为了连接这些圆圈,我应该使用标准的 d3 方法吗?

小提琴:http://jsfiddle.net/zzz8svuq/10/

更新:

这是更新后的代码,它在数据集 graph.nodes 中配置的圆之间绘制连接线:

<!DOCTYPE html>
<html>
  <head>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
   </head>
<body style="overflow: hidden;">
<div id="canvas"  style="overflow: hidden;"></div>

<script type="text/javascript">

    var graph = {
        "nodes": [
            {name: "1", "group": 1, x: 100, y: 50, r: 10 , connected : "2"},
            {name: "2", "group": 1, x: 200, y: 90, r: 15, connected : "1"},
            {name: "3", "group": 2, x: 300, y: 230, r: 25, connected : "1"}
        ] 
    }


    $( document ).ready(function() {

        var width = 2000;
        var height = 500;

        var svg = d3.select("#canvas").append("svg")
                .attr("width", width)
                .attr("height", height)
                .append("g");

        var lines = svg.attr("class", "line")
                .selectAll("line").data(graph.nodes)
                .enter().append("line")
                .style("stroke", "gray") // <<<<< Add a color
                .attr("x1", function (d, i) {
                    return d.x
                })
                .attr("y1", function (d) {
                    return d.y
                })
                .attr("x2", function (d) {
                    return findAttribute(d.connected).x
                })
                .attr("y2", function (d) {
                    return findAttribute(d.connected).y
                })

        var circles = svg.selectAll("circle")
                .data(graph.nodes)
                .enter().append("circle")
                .style("stroke", "gray")
                .style("fill", "white")
                .attr("r", function (d, i) {
                    return d.r
                })
                .attr("cx", function (d, i) {
                    return d.x
                })
                .attr("cy", function (d, i) {
                    return d.y
                });

    });

    function findAttribute(name) {
        for (var i = 0, len = graph.nodes.length; i < len; i++) {
            if (graph.nodes[i].name === name)
                return graph.nodes[i]; // Return as soon as the object is found
        }
        return null; // The object was not found
    }


</script>
</body>
</html>

【问题讨论】:

    标签: javascript d3.js


    【解决方案1】:

    您需要确保线条具有笔触颜色,否则它们将被绘制为白色,您将无法看到它们。

     var lines = svg.attr("class", "line")
                .selectAll("line").data(graph.links)
                .enter().append("line")
                .style("stroke", "gray") // <<<<< Add a color
                .attr("x1", function(d) { return 50 })
                .attr("y1", function(d) { return 50 })
                .attr("x2", function(d) { return 100 })
                .attr("y2", function(d) {  return 100 })
    

    【讨论】:

    • 这是一个常见的疏忽 - 它仍然让我感到困惑! SVG 对象默认有黑色填充但没有描边。如果屏幕上没有出现某些内容,请检查元素以查看它是否存在但未显示。
    猜你喜欢
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多