如何在圆之间画线

How to draw lines between circles?

本文关键字:之间      更新时间:2023-09-26

我试图画三个圆,并在每个圆之间画连线。

最终目标是使用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/

更新:

这是更新后的代码,它在数据集图形中配置的圆圈之间绘制连接线。代码:

<!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>

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

 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 })