Draw a line from one circle to another in d3.js

I will explain the problem that I have in my real project. I consume a web service and this returns me n points x, y. I mimic a custom web service. I want to put a circle in these coordinates, and for each circle I want to draw a line that connects them. eg:

enter image description here

I would like to add a line between the circles, but showing the animation. eg:

http://bl.ocks.org/duopixel/4063326

for example, this animation, but point by point

When I launch my application, I want the line to have an animation from the start circle to the end. and if I add a new circle, I want to create a line and create an animation for the circle. How can i do this?

http://jsfiddle.net/2rv0o8da/

  var svg = d3.select('svg');

  var dataSet = [10, 20, 30, 40];
  function display(data){
  var circle = svg.selectAll('circle')
      .data(data)
      .enter()
      .append('circle')
      .attr({
          r:function(d){ return d },
          cx:function(d, i){ return i * 100 + 50 },
          cy:50,
          fill: 'red'
      });
  } 
  display(dataSet);


  setTimeout(function(){
    display([5]);
  },2000)
+4
source share
2 answers

, :

, , , .

, 10 , x y:

var dataSet = d3.range(10).map(function(d) {
    return {x: someValue, y: someValue}
});

, , :

var lineGenerator = d3.svg.line()
    .x(function(d) {return d.x})
    .y(function(d) {return d.y})
    .interpolate("monotone")

bl.ocks:

var totalLength = path.node().getTotalLength();

path.attr("stroke-dasharray", totalLength + " " + totalLength)
    .attr("stroke-dashoffset", totalLength)
    .transition()
    .duration(2000)
    .ease("linear")
    .attr("stroke-dashoffset", 0);

:

var svg = d3.select('svg');

var backLayer = svg.append("g");
var frontLayer = svg.append("g");

var dataSet = d3.range(10).map(function(d) {
  return {
    x: d * 30 + 10,
    y: Math.random() * 130 + 10
  }
});

var lineGenerator = d3.svg.line()
  .x(function(d) {
    return d.x
  })
  .y(function(d) {
    return d.y
  })
  .interpolate("monotone")

function displayCircles(data) {
  var circle = frontLayer.selectAll(null)
    .data(data)
    .enter()
    .append('circle')
    .attr({
      r: 6,
      cx: function(d) {
        return d.x
      },
      cy: function(d) {
        return d.y
      },
      fill: 'white',
      stroke: "black",
      "stroke-width": "3px"
    });
};

function displayLine(data) {
  var line = backLayer.append("path")
    .datum(data)
    .attr({
      d: lineGenerator(data),
      fill: 'none',
      stroke: "red",
      "stroke-width": "3px",
      "shape-rendering": "geometricPrecision"
    });

  var totalLength = line.node().getTotalLength();

  line.attr("stroke-dasharray", totalLength + " " + totalLength)
    .attr("stroke-dashoffset", totalLength)
    .transition()
    .duration(2000)
    .ease("linear")
    .attr("stroke-dashoffset", 0);
}

displayCircles(dataSet);

setTimeout(function() {
  displayLine(dataSet)
}, 1000)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<svg></svg>
Hide result
+3

, , attrTween:

var w = 500, h = 100;
var data = d3.range(100).map(function(d){
    return Math.random() * 5 + 5;
});
var x = d3.scale.linear().domain([0, 50]).range([0, 500]);
var y = d3.scale.linear().domain([0, 10]).range([0, 100]);
var line = d3.svg.line()
      .interpolate("cardinal")
      .x(function(d,i) {return x(i);})
      .y(function(d) {return y(d);})

var svg = d3.select("#container")
    .append("svg")
    .attr({ width: w, height: h })
    .style("border", "1px solid #ccc")
    .append("path")
    .datum(data)
    .attr({
        d: line(data.slice(0,1)),
        fill: "none",
        stroke: "#CB6154",
        "stroke-width": 2
    })
    .transition()
    .duration(2000)
    .attrTween("d", function(d){
        var f = d3.interpolate(0,50);
        return function(t){
            return line(d.slice(0, Math.ceil(f(t))));
        };
    })

jsfiddle.

0

Source: https://habr.com/ru/post/1686277/


All Articles