Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3.js Dynamic connector between objects

I'm very new to both JS and D3, and I've googled this a tonne but only found examples that are a bit too advanced.

I'm making a simple decision graph implementation, and I'm stuck trying to connect 2 nodes with a line / path. The objects can be moved around with the mouse, and the path should always update to reflect the positions of the objects.

This is my base source of knowledge: https://github.com/mbostock/d3/wiki/SVG-Shapes, but I don't quite understand how to do something smart with it.

Here is what I have so far: http://jsbin.com/AXEFERo/5/edit

Don't need the fancy stuff, just need to understand how to create connectors and have them update dynamically when the objects are being dragged around. Big thanks!

like image 928
babyjet Avatar asked Sep 07 '13 11:09

babyjet


People also ask

How to use D3 JS?

D3.js is a JavaScript library for manipulating HTML based on data. How to Use D3.js? To use D3.js in your web page, add a link to the library: D3.js is easy to use. This script selects the body element and appends a paragraph with the text "Hello World!": d3.select("body").append("p").text("Hello World!");

What are the dynamic properties of D3?

# Dynamic Properties. Despite their apparent simplicity, these functions can be surprisingly powerful; the d3.geoPath function, for example, projects geographic coordinates into SVG path data. D3 provides many built-in reusable functions and function factories, such as graphical primitives for area, line and pie charts.

Why did we choose react over D3 for our project?

After several research and comparison, we chose to use React as our primary user interface library which we would later integrate with D3 and a Meteor backend to serve all the data and other services. We chose D3 not just because of its popularity and bulletproof core, but especially because of the amazing physical tools provided by d3-force.

What is included in the D3 source file?

This file contains all of the source for the D3.js Web app, including script references, styles, basic HTML to create a general layout for the Web app, and JavaScript code using the D3.js library to dynamically populate the app and generate a bar chart based on data from the API Server.


1 Answers

To draw a line between the circles, you don't need anything special -- just the line element.

var line = svg.append("line")
        .style("stroke", "black")
        .attr("x1", 150)
        .attr("y1", 100)
        .attr("x2", 250)
        .attr("y2", 300);

Updating the position dynamically is a bit more difficult. At the moment, you have no means of distinguishing which of the circles is being dragged. One way of doing this is to add a distinguishing class to the g elements.

var g1 = svg.append("g")
        .attr("transform", "translate(" + 150 + "," + 100 + ")")
        .attr("class", "first")
        ...

and similarly for the other one. Now you can switch on the class in your dragmove function and update either the start or the end coordinates of the line.

if(d3.select(this).attr("class") == "first") {
            line.attr("x1", x);
            line.attr("y1", y);
} else {
            line.attr("x2", x);
            line.attr("y2", y);
}

Complete example here. There are other, more elegant ways of achieving this. In a real application, you would have data bound to the elements and could use that to distinguish between the different circles.

like image 159
Lars Kotthoff Avatar answered Sep 30 '22 12:09

Lars Kotthoff