Built with blockbuilder.org
Last active
December 3, 2018 10:09
-
-
Save AndriiShtoiko/40863c35244ebc6ff211d7e9b85e9646 to your computer and use it in GitHub Desktop.
Circles: transitions
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 { | |
width: 800px; | |
height: 600px; | |
border: 1px solid gray; | |
} | |
circle { | |
opacity: 0.5; | |
} | |
</style> | |
</head> | |
<body> | |
<script> | |
let radius = 10; | |
let duration = 1500; | |
let width = 800; | |
let height = 600; | |
let svg = d3.select('body').append('svg'); | |
let xScale = d3.scaleBand() | |
.rangeRound([0, width]); | |
let yScale = d3.scaleLinear() | |
.range([height, 0]); | |
let colorScale = d3.scaleOrdinal(d3.schemeCategory10); | |
let years = 30; | |
let data = []; | |
let cities = ['Crookston', 'Duluth', 'GrandRapids', 'Morris', 'StPaul', 'Waseca']; | |
let villages =['vi1', 'vi2', 'vi3', 'vi4', 'vi5', 'vi6', 'vi7', 'vi8']; | |
function update(data, year) { | |
let t = d3.transition().duration(1000) | |
data = data.filter(d => d.date.getFullYear() === year) | |
// console.log(data) | |
let circles = svg.selectAll('circle') | |
.data(data, d => d.key); | |
circles.exit() | |
.transition(t) | |
.attr('r', 0) | |
.remove(); | |
circles.enter().append('circle') | |
.attr('r', radius) | |
// .attr('cy', d => yScale(d.yield)) | |
.merge(circles) | |
.attr('cx', d => xScale(d.site)) | |
.attr('fill', d => colorScale(d.village)) | |
.transition(t) | |
.attr('cy', d => yScale(d.yield)) | |
} | |
while (years > 0) { | |
cities.forEach(city => { | |
villages.forEach(village => { | |
data.push({ | |
date : new Date(`${2000-years}-01-01`), | |
yield : Math.round(70 * Math.random()) + 5, | |
key : `${village}${city}`, | |
site : city, | |
village: village, | |
}) | |
}) | |
}) | |
--years | |
} | |
let xDomain = data.map(d => d.site) | |
// console.log(xDomain) | |
xScale.domain(xDomain) | |
// console.log(xScale.domain()) | |
// let yExtent = d3.extent(data, d => d.yield) | |
let yMax = d3.max(data, d => d.yield) | |
// console.log(yExtent) | |
yScale.domain([0, yMax + 5]) | |
let startYear = 1990; | |
let index = 0; | |
let numYears = 9; | |
// de | |
// setInterval(() => { | |
update(data, startYear + (index % numYears)); | |
// index += 1 | |
// }, 1000) | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment