Built with blockbuilder.org
forked from sxywu's block: DS July: Code 1
forked from sxywu's block: DS July: Code 2
forked from sxywu's block: DS July: Code 3
forked from sxywu's block: DS July: Code 4
forked from sxywu's block: DS July: Code 5
forked from sxywu's block: DS July: Code 6
forked from sxywu's block: DS July: Code 7
forked from sxywu's block: Film Flowers, All
forked from anonymous's block: Film Flowers, Refactored
forked from sxywu's block: Film Flowers, Single Complete
forked from sxywu's block: Film Flowers, Full Complete
forked from sxywu's block: Film Flowers, Full Starter Code
xxxxxxxxxx
<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,40 50,70 20,100',
'L0,85',
'L-20,100',
'C-50,70 -50,40 0,0'
],
// ADD 3 MORE PETAL PATHS
[
'M0,0',
'C-50,65 -10,70 0,70',
'C10,70 60,65 0,0'
],
[
'M0,0',
'C-50,65 -10,70 0,70',
'C10,70 60,65 0,0'
],
[
'M0,0',
'C-50,65 -10,70 0,70',
'C10,70 60,65 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,
}
});
// 1. set domain for scales
const sizeExtent = d3.extent(movies, d => d.rating)
const numPetalsExtent = d3.extent(movies, d => d.votes)
sizeScale.domain(sizeExtent)
numPetalsScale.domain(numPetalsExtent)
// 2. create a <g> for each flower
// and translate+scale the whole flower
// instead of individual petals
const flowers = svg.selectAll('g')
.data(movies).enter().append('g')
.attr('transform', (d, i) => {
const x = ( i % 3 + 0.5) * 200
const y = Math.floor(i/3) * 100
const scale = sizeScale(d.rating)
return `translate(${x}, ${y})scale(${scale})`
})
// 2. for each flower calculate
// number of petals and their angle of rotation
// and draw them like before
const petals = flowers.selectAll('path')
.data(d => {
console.log(d)
const numPetals = numPetalsScale(d.votes)
console.log(numPetals)
return _.times(numPetals, i => ({
rotation: ( i / numPetals) * 360,
size: sizeScale(d.rating),
path: pathLookup[d.pg]
}))
}).enter()
.append('path')
.attr('transform', (d, i) => {
return `rotate(${d.rotation})scale(${d.size})`
})
.attr('d', d => d.path)
.attr('fill', 'rgba(0, 190, 255, 0.6)')
.attr('stroke', '#000000')
});
</script>
</body>
https://d3js.org/d3.v4.min.js
https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.js