This is a basic force directed graph using version 4 of d3. Find the tutorial of how to make it here.
forked from puzzler10's block: d3-force: minimal working example
license: mit |
This is a basic force directed graph using version 4 of d3. Find the tutorial of how to make it here.
forked from puzzler10's block: d3-force: minimal working example
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.links line { | |
stroke: #999; | |
stroke-opacity: 0.6; | |
} | |
.nodes circle { | |
stroke: #fff; | |
stroke-width: 1.5px; | |
} | |
</style> | |
ID: <input type="number" id="data_id"><br> | |
Join: <input type="number" id="data_join"><br> | |
<button type="button" onclick="updateArray()"></button> | |
<svg width="960" height="600"></svg> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script> | |
function updateArray() { | |
var data_id = document.getElementById('data_id').value | |
var data_join = document.getElementById("data_join").value | |
dataArray.push({source: data_id, target: data_join}); | |
linksArray.push({name: data_id}); | |
console.log(dataArray); | |
console.log(linksArray); | |
} | |
var dataArray = [{sortBy: "Location", name: 1}, | |
{sortBy: "Location", name: 2}, | |
{sortBy: "Location", name: 3}, | |
{sortBy: "Location", name: 4}, | |
{sortBy: "Location", name: 5}, | |
{sortBy: "Location", name: 6} | |
]; | |
var linksArray = [{source: 1, target: 2}, | |
{source: 1, target: 3}, | |
{source: 1, target: 4} | |
]; | |
//create somewhere to put the force directed graph | |
var svg = d3.select("svg"), | |
width = +svg.attr("width"), | |
height = +svg.attr("height"); | |
var nodes_data = [ | |
{"name": "Travis", "sex" : "M"}, | |
{"name": "Rake", "sex": "M"}, | |
{"name": "Diana", "sex": "F"}, | |
{"name": "Rachel", "sex": "F"}, | |
{"name": "Shawn", "sex": "M"}, | |
{"name": "Emerald", "sex": "F"} | |
] | |
//set up the simulation | |
//nodes only for now | |
var simulation = d3.forceSimulation() | |
//add nodes | |
.nodes(dataArray); | |
//add forces | |
//we're going to add a charge to each node | |
//also going to add a centering force | |
simulation | |
.force("charge_force", d3.forceManyBody()) | |
.force("center_force", d3.forceCenter(width / 2, height / 2)); | |
//draw circles for the nodes | |
var node = svg.append("g") | |
.attr("class", "nodes") | |
.selectAll("circle") | |
.data(dataArray) | |
.enter() | |
.append("circle") | |
.attr("r", 5) | |
.attr("fill", "red"); | |
//add tick instructions: | |
simulation.on("tick", tickActions ); | |
//Time for the links | |
//Create links data | |
var links_data = [ | |
{"source": "Travis", "target": "Rake"}, | |
{"source": "Diana", "target": "Rake"}, | |
{"source": "Diana", "target": "Rachel"}, | |
{"source": "Rachel", "target": "Rake"}, | |
{"source": "Rachel", "target": "Shawn"}, | |
{"source": "Emerald", "target": "Rachel"} | |
] | |
//Create the link force | |
//We need the id accessor to use named sources and targets | |
var link_force = d3.forceLink(linksArray) | |
.id(function(d) { return d.name; }) | |
//Add a links force to the simulation | |
//Specify links in d3.forceLink argument | |
simulation.force("links",link_force) | |
//draw lines for the links | |
var link = svg.append("g") | |
.attr("class", "links") | |
.selectAll("line") | |
.data(linksArray) | |
.enter().append("line") | |
.attr("stroke-width", 2); | |
function tickActions() { | |
//update circle positions each tick of the simulation | |
node | |
.attr("cx", function(d) { return d.x; }) | |
.attr("cy", function(d) { return d.y; }); | |
//update link positions | |
//simply tells one end of the line to follow one node around | |
//and the other end of the line to follow the other node around | |
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; }); | |
} | |
</script> |