Built with blockbuilder.org
Last active
December 5, 2016 13:38
-
-
Save vladimir-ivanov/dabb619e3ca5de7f7f485b6d39947a9b to your computer and use it in GitHub Desktop.
fresh block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"/> | |
<title>D3 v4 - force layout</title> | |
<style> | |
html, body, #graph { | |
width: 900px; | |
height: 500px; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="graph"></div> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/d3/4.1.1/d3.min.js"></script> | |
<script> | |
!(function(){ | |
"use strict" | |
var width,height | |
var chartWidth, chartHeight | |
var margin | |
var svg = d3.select("#graph").append("svg").call(d3.zoom().scaleExtent([0.1, 10]).on("zoom", zoomed)); | |
var chartLayer = svg.append("g").classed("chartLayer", true); | |
function zoomed() { | |
chartLayer.attr("transform", d3.event.transform); | |
} | |
main() | |
function main() { | |
var range = 200; | |
var data = { | |
nodes:d3.range(0, range).map(function(d){ return {label: "l"+d ,r:~~d3.randomUniform(8, 28)()}}), | |
links:d3.range(0, range).map(function(){ return {source:~~d3.randomUniform(range)(), target:~~d3.randomUniform(range)()} }) | |
} | |
setSize(data) | |
drawChart(data) | |
} | |
function setSize(data) { | |
width = document.querySelector("#graph").clientWidth | |
height = document.querySelector("#graph").clientHeight | |
margin = {top:0, left:0, bottom:0, right:0 } | |
chartWidth = width - (margin.left+margin.right) | |
chartHeight = height - (margin.top+margin.bottom) | |
svg.attr("width", width).attr("height", height) | |
chartLayer | |
.attr("width", chartWidth) | |
.attr("height", chartHeight) | |
.attr("transform", "translate("+[margin.left, margin.top]+")") | |
} | |
function drawChart(data) { | |
var forceLink = d3.forceLink().id(function(d) { return d.index }); | |
forceLink.distance(300); | |
var forceManyBody = d3.forceManyBody(); | |
var simulation = d3.forceSimulation() | |
.force("link", forceLink) | |
// .force("collide",d3.forceCollide( function(d){return d.r + 80 }).iterations(2) ) | |
//.force("charge", forceManyBody) | |
// .force("center", d3.forceCenter(chartWidth/2, chartHeight/2)) | |
.force("y", d3.forceY(function(d){console.log(d); return 100;}).strength(1)) | |
// .force("x", d3.forceX(100)) | |
var link = chartLayer.append("g") | |
.attr("class", "links") | |
.selectAll("line") | |
.data(data.links) | |
.enter() | |
.append("line") | |
.attr("stroke", "black") | |
var node = chartLayer.append("g") | |
.attr("class", "nodes") | |
.selectAll("circle") | |
.data(data.nodes) | |
.enter().append("circle") | |
.attr("r", function(d){ return d.r }) | |
.call(d3.drag() | |
.on("start", dragstarted) | |
.on("drag", dragged) | |
.on("end", dragended)); | |
var ticked = 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("cx", function(d) { return d.x; }) | |
.attr("cy", function(d) { return d.y; }); | |
} | |
simulation | |
.nodes(data.nodes) | |
.on("tick", ticked); | |
simulation.force("link") | |
.links(data.links); | |
function dragstarted(d) { | |
if (!d3.event.active) simulation.alphaTarget(0.3).restart(); | |
d.fx = d.x; | |
d.fy = d.y; | |
} | |
function dragged(d) { | |
d.fx = d3.event.x; | |
d.fy = d3.event.y; | |
} | |
function dragended(d) { | |
if (!d3.event.active) simulation.alphaTarget(0); | |
d.fx = null; | |
d.fy = null; | |
} | |
} | |
}()); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment