Published
Edited
Aug 18, 2022
1 fork
Insert cell
# 基础的饼图练习
Insert cell
fruits = [
{name: "🍊", count: 21},
{name: "🍇", count: 13},
{name: "🍏", count: 8},
{name: "🍌", count: 5},
{name: "🍐", count: 3},
{name: "🍋", count: 2},
{name: "🍎", count: 1},
{name: "🍉", count: 1}
]
Insert cell
Insert cell
Insert cell
htl.html`<svg viewBox="-320 -320 640 640" style="max-width: 640px">
${Array.from({ length: n }).map((_, i) => htl.svg`<path stroke="black" fill="${d3.interpolateRainbow(i / n)}" d=${arc([i * perAngle, (i + 1) * perAngle])} />`)}
</svg>`
Insert cell
perAngle = 360 / n * R
Insert cell
R = Math.PI / 180
Insert cell
pieData = d3.pie()
.value(d => d.count)(fruits)
Insert cell
arc2 = d3.arc()
.innerRadius(200)
.outerRadius(300)
// 圆角
.cornerRadius(10)
// 间隔角度
.padRadius(300)
.padAngle(2 / 300)
Insert cell
htl.html`<svg viewBox="-320 -320 640 640" style="max-width: 640px">
${pieData.map((d, i) => htl.svg`<path stroke="black" fill="${d3.interpolateRainbow(i/pieData.length)}" d="${arc2(d)}" />
<text fill="white" transform="translate(${arc2.centroid(d).join(',')})" text-anchor="middle">
<tspan x="0" font-size="24">${d.data.name}</tspan>
<tspan x="0" font-size="12" dy="1.3em">${d.data.count}</tspan>
</text>
`)}
</svg>`
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