Built with blockbuilder.org
Created
January 26, 2018 07:07
-
-
Save parasquid/c53a524feb2fec021e686cad0084fda9 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> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.js'></script> | |
<link href='https://fonts.googleapis.com/css?family=Libre+Baskerville:400,700' rel='stylesheet' type='text/css'> | |
<style> | |
svg { | |
width: 600px; | |
height: 20000px; | |
} | |
</style> | |
</head> | |
<body> | |
<svg></svg> | |
<script> | |
const width = 600; | |
const height = 600; | |
const petalPaths = [[ | |
'M0,0', | |
"C50,50 50,100 0,100", | |
"C-50,100 -50,50 0,0" | |
], | |
[ | |
'M-35,0', | |
'C-25,25 25,25 35,0', | |
'C50,25 25,75 0,100', | |
'C-25,75 -50,25 -35,0' | |
], | |
[ | |
'M0,0', | |
'C50,40 50,70 20,100', | |
'L0,85', | |
'L-20,100', | |
'C-50,70 -50,40 0,0' | |
], | |
[ | |
'M0,0', | |
'C50,25 50,75 0,100', | |
'C-50,75 -50,25 0,0' | |
]]; | |
// instantiate scales and petal path lookup | |
const sizeScale = d3.scaleLinear() | |
.range([0.1, 1]); | |
const numPetalsScale = d3.scaleQuantize() | |
.range(_.range(5, 10)); | |
const pathLookup = { | |
G: petalPaths[0], | |
PG: petalPaths[1], | |
'PG-13': petalPaths[2], | |
R: petalPaths[3], | |
}; | |
// grab svg | |
const svg = d3.select(' svg'); | |
/***************************************************** | |
** get movie data | |
******************************************************/ | |
d3.json('movies.json', function(movies) { | |
movies = _.map(movies, movie => { | |
return { | |
rating: ++movie.imdbRating, | |
votes: parseInt(movie.imdbVotes.replace(/\,/g, '')), | |
year: ++movie.Year, | |
title: movie.Title, | |
pg: movie.Rated, | |
} | |
}); | |
// set domain for scales | |
const ratingExtent = d3.extent(movies, d => d.rating); | |
const votesExtent = d3.extent(movies, d => d.votes); | |
sizeScale.domain(ratingExtent); | |
numPetalsScale.domain(votesExtent); | |
// create petal data for the first movie | |
const numPetals = numPetalsScale(movies[0]); | |
const petalsData = _.times(numPetals, (i) => { | |
return { | |
numPetals, | |
size: sizeScale(movies[0].rating), | |
path: pathLookup[movies[0].pg] | |
}; | |
}); | |
// draw the petals | |
svg.selectAll('.petal') | |
.data(petalsData).enter().append('path') | |
.classed('petal', true) | |
.attr('transform', (d, i) => { | |
const angle = i * (360 / d.numPetals); | |
return `translate(100, 100)rotate(${angle})scale(${d.size})`; | |
}) | |
.attr('d', d => d.path) | |
.attr('fill', 'none') | |
.attr('stroke', '#444') | |
.attr('stroke-width', 2) | |
}); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment