Published
Edited
Jan 24, 2022
1 fork
Insert cell
md`# Doughnut Chart`
Insert cell
w = 450
Insert cell
h = 450
Insert cell
margin = 40
Insert cell
radius = Math.min(w,h) / 2 - margin
Insert cell
dataset = [
{ name: "はい", value: 62 },
{ name: "いいえ", value: 52 },
{ name: "どちらとも言えない", value: 22 },
{ name: "無回答", value: 10 }
]
Insert cell
color = d3.scaleOrdinal()
.range(["red", "blue", "green", "#C1D765"])
Insert cell
pie = d3.pie().value(d => d.value)
Insert cell
data_ready = pie(dataset)
Insert cell
svg = {
const svg = DOM.svg(w, h);
d3.select(svg)
.selectAll("path")
.data(pie(dataset))
.enter()
.append("path")
.attr(
"d",
d3
.arc()
.innerRadius(10) // This is the size of the donut hole
.outerRadius(radius / 2)
)
.attr("transform", "translate(" + w / 2 + "," + h / 2 + ")")
.attr("fill", (d) => color(d.data.name))
.attr("stroke", "black")
.style("stroke-width", "2px")
.style("opacity", 0)
.transition() // アニメーション
.duration(1000) // アニメーション時間
.delay((d, i) => i * 250) // 遅延時間(データ項目ごとに250msずらす)
.style("opacity", 0.7)
.attr(
"d",
d3
.arc()
.innerRadius(100) // This is the size of the donut hole
.outerRadius(radius)
);

return svg;
}
Insert cell
d3 = require('d3@5')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more