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,25 50,75 0,100',
'C-50,75 -50,25 0,0'
],
[
'M0,0',
'C50,40 50,70 20,100',
'L0,85',
'L-20,100',
'C-50,70 -50,40 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'
]
];
var color = d3.scaleLinear()
.domain([0, 360])
.range(["pink", "lightblue"]);
// instantiate scales and petal path lookup
const sizeScale = d3.scaleLinear()
.range([0.5, 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
// 2. create a <g> for each flower
// and translate+scale the whole flower
// instead of individual petals
// 2. for each flower calculate
// number of petals and their angle of rotation
// and draw them like before
// 1. set domain for scales
var ratingExtent = d3.extent(movies, d => d.rating);
sizeScale.domain(ratingExtent);
var numberRatingsExtent = d3.extent(movies, d => d.votes);
numPetalsScale.domain(numberRatingsExtent);
var firstMovie = movies[0];
var data = movies.map(movie => {
var numPetals = numPetalsScale(firstMovie);
var data = _.times(numPetals, (i) => {
var degrees = i/numPetals * 360;
return {
degrees,
scale: sizeScale(movie.rating),
path: pathLookup[movie.pg],
color: d3.interpolateRainbow(i/ numPetals),
}
});
return data;
})
console.log('data', data)
// 3. draw the petals using d3's data-binding and enter-append
// (hint: use SVG transform for translation, rotation, and scale)
var groups = svg.selectAll('g')
.data(data).enter().append('g')
.attr('transform', (d, i) => {
var x = (i%3) * 200;
var y = Math.floor(i/3) * 200;
return `translate(${x}, ${y})`;
});
groups.selectAll('path')
.data(d => d).enter().append('path')
.attr('stroke', '#000')
.attr('stroke-width', 2)
.attr('fill', d => d.color)
.attr('fill-opacity', .5)
.attr('d', function(d) {return d.path})
.attr('transform', function(d, i) {
var x = 150;
var y = 150;
return `translate(${x}, ${y}) rotate(${d.degrees}) scale(${d.scale})`;
})
});
</script>
</body>
https://d3js.org/d3.v4.min.js
https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.js