Built with blockbuilder.org
Last active
June 23, 2017 10:55
-
-
Save areologist/24c0506211b161a72475ac7499bce5a1 to your computer and use it in GitHub Desktop.
mere circles
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> | |
<style> | |
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
svg { | |
border: 1px solid blue; | |
} | |
</style> | |
</head> | |
<body> | |
<script> | |
const svg = d3.select('body') | |
.append('svg') | |
.attr('width', 650) | |
.attr('height', 450); | |
const generateData = (n=10) => { | |
const d = []; | |
const f = () => Math.floor(Math.random() * 10 + 1); | |
for (let i = 0; i < n; i++) { | |
d.push({ | |
x: f(), | |
y: f(), | |
r: f() | |
}); | |
} | |
return d; | |
}; | |
let data = generateData(30); | |
const scaleX = d3.scaleLinear() | |
.domain([0, 10]) | |
.range([50, 550]); | |
const scaleY = d3.scaleLinear() | |
.domain([0, 10]) | |
.range([50, 350]); | |
const scaleR = d3.scaleLinear() | |
.domain([0, 10]) | |
.range([10, 100]); | |
const scaleC = d3.scaleLinear() | |
.domain([0, 10]) | |
.range([0, 1]); | |
//const colors = d3.scaleOrdinal(d3.schemeCategory20c); | |
const rainbow = d3.scaleSequential(d3.interpolateRainbow); | |
svg.selectAll('circle') | |
.data(data) | |
.enter().append('circle') | |
.attr('cx', d => scaleX(d.x)) | |
.attr('cy', d => scaleY(d.y)) | |
.attr('r', d => scaleR(d.r)) | |
.attr('fill', 'none') | |
.attr('stroke-width', 2) | |
.attr('stroke', d => { | |
// colors(d.r); | |
return rainbow(scaleC(d.r)); | |
}); | |
// for fun let's modify the data | |
const update = () => { | |
const delta = 10; | |
data = data | |
.slice(delta) | |
.concat(generateData(delta)); | |
const circles = svg.selectAll('circle') | |
.data(data, d => d.radius); | |
circles.exit() | |
.attr('opacity', 1) | |
.transition() | |
.attr('r', d => scaleR(d.r) * 2) | |
.attr('opacity', 0) | |
.remove(); | |
circles.enter().append('circle') | |
.attr('r', 0) | |
.attr('cx', d => scaleX(d.x)) | |
.attr('cy', d => scaleY(d.y)) | |
.attr('fill', 'none') | |
.attr('stroke-width', 2) | |
.attr('stroke', d => rainbow(scaleC(d.r))) | |
.attr('opacity', 0.3) | |
.transition() | |
.duration(450) | |
.ease(d3.easeLinear) | |
.attr('opacity', 1) | |
.attr('r', d => scaleR(d.r)); | |
}; | |
setInterval(update, 1000); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment