Public
Edited
May 25, 2023
Insert cell
Insert cell
{
const data = todayData
const svg = d3.select(DOM.svg(width, height))
const radius = (height - margin.top - margin.bottom) / 2
const w = width - margin.left - margin.right
const h = height - margin.top - margin.bottom
let color = d3.scaleOrdinal(colors)
let pie = d3.pie()
.sort((a, b) => b.count - a.count)
.value(d => d.count)
.padAngle(0.01)
let path = d3.arc()
.outerRadius(radius)
.innerRadius(70)
let label = d3.arc()
.outerRadius(radius - 30)
.innerRadius(radius - 30)
let total = d3.sum(data, d => d.count)
function percent(val) {
return `${(100 * val / total).toFixed(2)}%`
}
let g = svg.append('g').attr('transform', `translate(${margin.left}, ${margin.top})`)
// 绘制饼图
let pieGroup = g.append('g')
var arc = pieGroup.selectAll('.arc')
.data(pie(data))
.enter().append('g')
.attr('class', 'arc')
.attr('transform', `translate(${w/2}, ${radius + 10})`)
arc.append('path')
.attr('d', path)
.attr('fill', d => color(d.data.label))
// 绘制中心文字
// 绘制图例


return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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