Published
Edited
Nov 15, 2019
1 fork
26 stars
Insert cell
md`# Shirley Wu D3 Extravaganza`
Insert cell
petalPath = 'M 0,0 C -10,-10 -10,-40 0,-50 C 10,-40 10,-10 0,0';
Insert cell
petalSize = 50;
Insert cell
html`<svg width="50" height="50"><path transform="translate(25,50)" d="${petalPath}"></svg>`
Insert cell
d3=require('d3');
Insert cell
data=d3.json('https://raw.githubusercontent.com/sxywu/filmflowers/master/movies.json').then(data => _.values(data));
Insert cell
{
const svg = DOM.svg(petalSize * 10, petalSize * 10);
const ratingMinMax = d3.extent(data, d => +d.imdbRating);
const votesMinMax = d3.extent(data, d => +d.imdbVotes.replace(',',''));
const sizeScale = d3.scaleLinear().domain(ratingMinMax).range([0.25, 1]);
const numPetalScale = d3.scaleQuantize().domain(votesMinMax).range([3,6,9,12,15,18]);
const flowersData = _.map(data, d => {
const numPetals = numPetalScale(+d.imdbVotes.replace(',',''));
console.log(numPetals);
const petSize = sizeScale(+d.imdbRating);
return {
petSize,
petals: _.times(numPetals, i => {return {angle: 360 * i / numPetals, petalPath}}),
numPetals,
}
});
console.log(flowersData)

const flowers = d3.select(svg)
.selectAll('g')
.data(flowersData)
.enter()
.append('g')
.attr('transform', (d, i) => `translate(${(i % 5) * petalSize},${Math.floor(i / 5) * petalSize})scale(${d.petSize})`);
flowers.selectAll('path')
.data(d => d.petals).enter().append('path')
.attr('d',d => d.petalPath)
.attr('transform',d=>`rotate(${d.angle})`)
.attr('fill', (d, i) => d3.interpolateWarm(d.angle / 360));
return svg;

}
Insert cell
_ = require('lodash')
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more