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>
const width = 600;
const height = 600;
const petalPaths = [
[
'M0,0',
"C50,50 50,100 0,100",
"C-50,100 -50,50 0,0"
],
[
'M-5,0',
'C-5,50 -5,50 -25,50',
'C0,100 0,100 25,50',
'C5,50 5,50 5,0',
'L-5,0',
'C-5,30 -5,30 -25,30',
'C0,100 0,100 25,30',
'C5,30 5,30 5,0',
'L-5,0',
'C-5,10 -5,10 -25,10',
'C0,100 0,100 25,10',
'C5,10 5,10 5,0',
'M0,0',
'L0,87'
],
[
'M0,0',
'C50,40 50,70 20,100',
'L0,0',
'L-20,100',
'C-50,70 -50,40 0,0',
'M25,25',
'L30,90',
'M-25,25',
'L-30,90',
'M0,0',
'L-22,100',
'M0,0',
'L18,100',
],
[
'M-5,0',
'L-5,50',
'L-25,20',
'L-25,100',
'C-5,50 5,50 25,100',
'M-25,100',
'C-10,50 10,50 25,100',
'M-25,100',
'C-20,40 20,40 25,100',
'M-25,100',
'C-30,30 30,30 25,100',
'M-25,100',
'C-40,20 40,20 25,100',
'M-25,100',
'C-50,10 50,10 25,100',
'L25,20',
'L5,50',
'L5,0',
'L-5,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 ratingExtent = d3.extent(movies, d => d.rating);
const votesExtent = d3.extent(movies, d => d.votes);
sizeScale.domain(ratingExtent);
numPetalsScale.domain(votesExtent);
// 2. create petal data for just the first movie
const groups = svg.selectAll('g')
.data(movies).enter().append('g')
.attr('transform', (d, i) => `translate(${i % 3 * 200},${Math.floor(i/3) * 200})`);
const circles = groups.selectAll('.petal')
.data(d => {
const numPetals = numPetalsScale(d);
const petalsData = _.times(numPetals, (i) => {
return {
numPetals,
size: sizeScale(d.rating),
path: pathLookup[d.pg],
color: d3.interpolateRainbow(i/numPetals)
};
});
return 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', d => d.color)
.attr('stroke-width', 2)
// // 3. draw the petals using d3's data-binding and enter-append
// // (hint: use SVG transform for translation, rotation, and scale)
// 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>
https://d3js.org/d3.v4.min.js
https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.js