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, Refactored
forked from sxywu's block: Film Flowers, Single 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: 800px;
height: 1600px;
}
</style>
</head>
<body>
<svg></svg>
<script>
const width = 800;
const height = 800;
const petalPaths = [[
'M0,0',
'C50,40 50,70 20,100',
'L0,85',
'L-20,100',
'C-50,70 -50,40 0,0'
],
[
'M0,0',
'C25,10 70,150 0,90',
'C-70,150 -25,10 0,0'
],
[
'M0,0',
'C-15,50 50,50 10,100',
'C5,95 -5,95 -10,100',
'C-60,65 -15,10 0,0'
],
[
'M-5,0',
'L5,0',
'C70,80 5,95 0,120',
'C-5,95 -70,80 -5,0',
]
];
// instantiate scales and petal path lookup
const sizeScale = d3.scaleLinear()
.range([0.1, 1]);
const numPetalsScale = d3.scaleQuantize()
.range(_.range(5, 13));
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,
genre: movie.Genre
}
});
console.log(movies)
// map rating -> sizeScale
var ratingExtent = d3.extent(movies, m => m.rating)
sizeScale.domain(ratingExtent)
// map votes -> numPetalsScale
var votesExtent = d3.extent(movies, m => m.votes)
numPetalsScale.domain(votesExtent)
var flowers = svg.selectAll('.flower')
.data(movies)
.enter().append('g')
.classed('flower', true)
.attr('transform', function(d, i) {
var x = (i % 4 + 0.5) * 200;
var y = (Math.floor(i / 4) + 0.5) * 200;
var size = sizeScale(d.rating)
return `translate(${x}, ${y}) scale(${size})`;
})
flowers.selectAll('.petal')
.data(d => {
var numPetals = numPetalsScale(d.votes);
return _.times(numPetals, i => {
return {
angle: 360 / numPetals * i,
path: pathLookup[d.pg]
}
});
})
.enter().append('path')
.classed('petal', true)
.attr('stroke', '#000')
.attr('stroke-width', 2)
.attr('fill', 'rgba(222,206,120, 0.8)')
.attr('d', (d) => d.path)
.attr('transform', (d) => `rotate(${d.angle})`)
});
</script>
</body>
https://d3js.org/d3.v4.min.js
https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.js