my code stock.com

laughing

text label and circle for graph(d3.js)
by laughing

text label and circle for graph(d3.js)

Snippet options

Download: Download snippet as text-label-and-circle-for-graphd3js.js.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

var dumpObj = function(o){
    var str = "";
    for(var i in o) {
	str = str + "¥n" + i + "¥t"+ o[i];
    }
    alert(str);
}

var w = 960,
h = 500

var vis = d3.select("body").append("svg:svg")
    .attr("width", w)
    .attr("height", h);

d3.json("graph.json", function(json) {
    var force = self.force = d3.layout.force()
	.nodes(json.nodes)
	.links(json.links)
	.gravity(.05)
	.distance(100)
	.charge(-100)
	.size([w, h])
	.start();
    
    var link = vis.selectAll("line.link")
	.data(json.links)
	.enter().append("svg:line")
	.attr("class", "link")
	.attr("x1", function(d) { return d.source.x; })
	.attr("y1", function(d) { return d.source.y; })
	.attr("x2", function(d) { return d.target.x; })
	.attr("y2", function(d) { return d.target.y; });
    
    var node = vis.selectAll("g.node")
	.data(json.nodes)
	.enter().append("svg:g")
	.attr("class", "node")
	.call(force.drag);
    
    node.append("svg:circle")
	.attr("class", "node")
	.attr("x", function(d) { return d.x; })
	.attr("y", function(d) { return d.y; })
	.attr("r", 5);
    
    node.append("svg:text")
	.attr("class", "nodetext")
	.attr("dx", 12)
	.attr("dy", ".35em")
	.text(function(d) { return d.name });
    
    force.on("tick", function() {
	link.attr("x1", function(d) { return d.source.x; })
	    .attr("y1", function(d) { return d.source.y; })
	    .attr("x2", function(d) { return d.target.x; })
	    .attr("y2", function(d) { return d.target.y; });
	
	node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
    });
});

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.