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: 600px;
height: 20000px;
}
</style>
</head>
<body>
<svg></svg>
<script>
/* What to do:
LET'S MAKE A FLOWER
- Type of petal: parental guidance rating
- Size of flower: IMDb rating out of 10
- Number of petals: number of IMDb votes
1. Create scale (scaleLinear and scaleQuantize)
2. Get data ready, and use the first movie data
3. Create number of petals based on data
4. Rotate and scale petals
*/
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 Z'
],
// ADD 3 MORE PETAL PATHS
// [
// 'M-20,60',
// 'C-70,0 70,0 20,60',
// 'C60,-10 105,105 35,90',
// 'C100,125 -10,165 0,110',
// 'C10,165 -100,125 -35,90',
// 'C-105,105 -60,-10 -20,60 Z'
// ]
[
'M0,0',
'C50,25 50,75 0,100',
'C-50,75 -50,25 0,0'
],
[
'M0,0',
'C0,20 90,70 20,100',
'L-20,100',
'C-90,70 0,20 0,0'
],
[
'M0,50',
'C50,5 60,70 0,100',
'C-60,70 -50,5 0,50'
]
];
// instantiate scales and petal path lookup
const sizeScale = d3.scaleLinear()
// constructs a continuous scale
.range([0.1, 1]);
const numPetalsScale = d3.scaleQuantize()
// change scale from continuous to discrete
// map continuous 16,000 (votes) to discrete no of petals
.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');
// svg.selectAll('path')
// .data(petalPaths)
// .enter().append('path')
// .attr('stroke', '#000')
// .attr('stroke-width', 2)
// .attr('fill', 'none')
// .attr('d', function(d) {return d.join(' ')})
// .attr('transform', function(d, i) {
// var x = (i % 3 + 0.5) * 150;
// var y = (Math.floor(i / 3) + 0.5) * 150;
// return `translate(${x}, ${y})`;
// })
/*****************************************************
** 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
// since scales are initiated with range, they need a domain:
// domain(min, max) --> use extent
// size scale = rating
// no of petals scale = no of votes
const sizeExtent = d3.extent(movies, d => d.rating);
//console.log(sizeExtent); // min rating: 5.6, max rating: 10
const numPetalsExtent = d3.extent(movies, d => d.votes);
//console.log(numPetalsExtent); // min votes: 16794, max votes: 1670736
// set domain on scales (sizeScale and numPetalsScale)
sizeScale.domain(sizeExtent);
numPetalsScale.domain(numPetalsExtent);
// 2. create petal data for just the first movie
// find out no of petals for 1st movie
const numPetals = numPetalsScale(movies[0].votes);
//console.log(movies[0].votes, numPetals); // 354068(votes) 6(petals)
const petalData = _.times(numPetals, (i) => {
// _.times: pass in an integer and it does a for loop that no of times (lodash)
// what to know for each petal:
// 1. rotation of the petal
const rotate = (i / numPetals) * 360;
// 2. path of petal (which petal path you are using) (pg ratings)
// 3. size of petals (IMDB ratings)
return {
rotate,
path: pathLookup[movies[3].pg],
size:sizeScale(movies[3].rating)
}
});
//console.log(petalData);
// 3. draw the petals using d3's data-binding and enter-append
// (hint: use SVG transform for translation, rotation, and scale)
svg.selectAll('path')
.data(petalData).enter().append('path')
.attr('transform', d => {
return `translate(100,100)rotate(${d.rotate})scale(${d.size})`
})
.attr('d', d => d.path)
.attr('stroke', '#000')
.attr('stroke-width', 4)
.attr('fill','none')
});
</script>
</body>
https://d3js.org/d3.v4.min.js
https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.js