Mucking about with d3.forceRadial(). idk anymore
Last active
December 2, 2017 02:57
-
-
Save fogonwater/30ded67dcd859e7b0a7c623931ebe8ff to your computer and use it in GitHub Desktop.
forceRadial tests II
This file contains 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 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> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<meta charset="utf-8"> | |
<svg width="960" height="600" viewBox="-480 -300 960 600"> | |
</svg> | |
<style> | |
body {background-color:#222; margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
</style> | |
</head> | |
<body> | |
<script> | |
var nodes = [].concat( | |
d3.range(1086).map(function() { return {type: "a", offset:(Math.random() * 20) - 10}; }), | |
d3.range(1645).map(function() { return {type: "b", offset:(Math.random() * 20) - 10}; }) | |
) | |
var colorScale1 = d3.scaleSequential(d3.interpolateRainbow) | |
.domain([300, 660]), | |
colorScale2 = d3.scaleSequential(d3.interpolateRainbow) | |
.domain([660, 300]) | |
var node = d3.select("svg") | |
.append("g") | |
.selectAll("circle") | |
.data(nodes) | |
.enter().append("circle") | |
.attr("r", 2.5) | |
//.attr("fill", function(d) { return d.type === "b" ? '#F24236' : '#5BC0EB'; }) | |
.style("fill", function(d) { return d.col; }) | |
var simulation = d3.forceSimulation(nodes) | |
.force("charge", d3.forceCollide().radius(function(d) { return d.type === "a" ? 3.4 : 6; })) | |
.force("r", d3.forceRadial(function(d) { return d.type === "a" ? 60 : 200; })) | |
.on("tick", ticked); | |
function ticked() { | |
node | |
.attr("cx", function(d) { return d.x; }) | |
.attr("cy", function(d) { return d.y; }) | |
.style("fill", function(d){ return d.type === "b" ? colorScale1(d.x + d.offset) : colorScale2(d.y + d.offset); }) | |
} | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment