Public
Edited
Apr 14, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
### Draw a petal
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
barData = [45, 67, 96, 84, 41]
Insert cell
Insert cell
Insert cell
{
const rectWidth = 50
const svg = html`
<svg width=${rectWidth * barData.length} height=100 style="border" 1 px dashed >
<rect />
<rect />
<rect />
<rect />
<rect />
</svg>
`
d3.select(svg).selectAll('rect')
.data(barData)
.attr('x', (d, i) => i * rectWidth)
// change the orientation
.attr('y', d => 100 - d)
.attr('height', d => d)
.attr('width', rectWidth)
.attr('stroke-width', 2)
.attr('stroke', 'blue')
.attr('fill', 'pink')

return svg
}
Insert cell
**exercise**: movie genre -> color of petal
Insert cell
Insert cell
Insert cell
movies
Insert cell
Insert cell
Insert cell
pathObj
Insert cell
Insert cell
movies
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
calculateGridPos(15)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
topGenres
Insert cell
movies[0]['genres'][0]
Insert cell
Insert cell
petalPaths
Insert cell
Insert cell
${textcolor('D3:', 'red')} Translate first and then scale
Insert cell
Insert cell
turn_into_flower = md`## Turn petals into flowers for each movie`
Insert cell
Insert cell
Insert cell
Insert cell
exercise time
Insert cell
updateBarsNew = (svg, data) => {
const rectWidth = 50

// ✨ OUR CODE HERE
const rect = d3.select(svg).selectAll('rect')
.data(data, d => d)
.join('rect')
.attr('x', (d, i) => i *rectWidth)
.attr('y', d => 100 - d)
.attr('height', d => d)
.attr('width', rectWidth)
.attr('fill', 'pink')
.attr('stroke', 'plum')
.attr('stroke-width', 2)
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
filteredFlowers = calculateData(filtered)
Insert cell
Insert cell
Insert cell
import {movies, colorObj, pathObj, slide, slide_style, scrollSVG, svgHeight, calculateGridPos, petalColors, topGenres, rated, petalPaths} from "@sxywu/workshop-utility-functions"
Insert cell
import {form} from "@mbostock/form-input"
Insert cell
import {textcolor} from "@observablehq/text-color-annotations-in-markdown"
Insert cell
import {calculateData} from '2683357905679f61'
Insert cell
import {checkbox} from "@jashkenas/inputs"
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