Force Layout d3js using JSON data

Developer
Size
2,774 Kb
Views
66,792

How do I make an force layout d3js using json data?

Force Layout d3js using JSON dataForked from Dzulfikar Adi Putra's Pen Force Layout d3js using JSON data.. What is a force layout d3js using json data? How do you make a force layout d3js using json data? This script and codes were developed by Rafszul on 14 September 2022, Wednesday.

Force Layout d3js using JSON data Previews

Force Layout d3js using JSON data - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Force Layout d3js using JSON data</title> <link rel='stylesheet prefetch' href='http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <h2>Force Layout JSON Directed Graph</h2>
<div id="main-content"> </div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js'></script>
<script src='https://d3js.org/d3.v3.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Force Layout d3js using JSON data - Script Codes CSS Codes

.link { fill: none; stroke: #b3b3b3; stroke-width: 1.5px;
}
circle { fill: #f9f9f9; stroke: #2a7fff; stroke-width: 2px;
}
text { font: 10px sans-serif; pointer-events: none; text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, 0 -1px 0 #fff, -1px 0 0 #fff;
}

Force Layout d3js using JSON data - Script Codes JS Codes

var width=800, height=400;
var color = d3.scale.category20();
var force = d3.layout.force() .size([width, height]) .linkDistance(100) .charge(-300) .start();
var svg = d3.select("#main-content").append("svg") .attr("width", width) .attr("height", height);
d3.json("https://codepen.io/superpikar/pen/KFein.js", function(error, graph){
var nodes = {};
var links = graph.arcs;
var pathtype = [];
// Compute the distinct nodes from the links.
links.forEach(function(link) { link.source = nodes[link.source] || (nodes[link.source] = {name: link.source}); link.target = nodes[link.target] || (nodes[link.target] = {name: link.target}); pathtype.push(link.dependency);
}); console.log(pathtype); force .nodes(d3.values(nodes)) .links(links) .on("tick", tick) .start(); svg.append("defs").selectAll("marker") .data(pathtype) .enter().append("marker") .attr("id", function(d) { return d; }) .attr("viewBox", "0 -5 10 10") .attr("refX", 25) .attr("refY", -1.5) .attr("markerWidth", 6) .attr("markerHeight", 6) .attr("orient", "auto") .append("path") .attr("d", "M0,-5L10,0L0,5");
var path = svg.append("g").selectAll("path") .data(force.links()) .enter().append("path") .attr("class", function(d) { return "link " + d.dependency; }) .attr("marker-end", function(d) { return "url(#" + d.dependency + ")"; });
var circle = svg.append("g").selectAll("circle") .data(force.nodes()) .enter().append("circle") .attr("r", 15) .call(force.drag);
var text = svg.append("g").selectAll("text") .data(force.nodes()) .enter().append("text") .attr("x", 15) .attr("y", ".31em") .text(function(d) { return d.name; });
// Use elliptical arc path segments to doubly-encode directionality.
function tick() { path.attr("d", linkArc); circle.attr("transform", transform); text.attr("transform", transform);
}
function linkArc(d) { var dx = d.target.x - d.source.x, dy = d.target.y - d.source.y, dr = Math.sqrt(dx * dx + dy * dy); return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
}
function transform(d) { return "translate(" + d.x + "," + d.y + ")";
}
});
Force Layout d3js using JSON data - Script Codes
Force Layout d3js using JSON data - Script Codes
Home Page Home
Developer Rafszul
Username rafszul
Uploaded September 14, 2022
Rating 3
Size 2,774 Kb
Views 66,792
Do you need developer help for Force Layout d3js using JSON data?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Rafszul (rafszul) Script Codes
Create amazing blog posts with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!