Public
Edited
Jan 21, 2024
Insert cell
Insert cell
donutChart
Insert cell
viewof clicks = Inputs.button("Click me")
Insert cell
donutChart = {
console.log("donutChart exec");

svg
.selectAll("path")
.data(pie(data))
.enter()
.append("path")
.attr("d", arc)
.attr("fill", (d) => color(d.data.fruit))
.each(function (d) {
this._current = d;
})

// Store the initial angles

return svgElem.node();
}
Insert cell
arc = d3
.arc()
.innerRadius(radius - 150)
.outerRadius(radius)
.cornerRadius(10)
Insert cell
pie = d3
.pie()

.value((d) => d.value)
.sort((a, b) => a.name - b.name)
.startAngle(Math.PI / 2) // Rotate start angle by 45 degrees
.endAngle(2 * Math.PI + Math.PI / 2)
.padAngle(0.03)
Insert cell
svg = svgElem
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.style("background-color", "black")

.append("g")
.attr(
"transform",
`translate(${width / 2 + margin.left}, ${height / 2 + margin.top})`
)
Insert cell
svgElem = d3.create("svg");

Insert cell
margin = ({ top: 20, right: 20, bottom: 20, left: 20 })
Insert cell
{
clicks;
if (clicks == 1) return;

console.log("clicked");
const data2 = [
{ fruit: "Apples", value: 5 },
{ fruit: "Bananas", value: 95 }
];

svg
.selectAll("path")
.data(pie(data2)) // Bind the new data
.transition() // Start the transition
.duration(5000) // Set the duration of the transition (in ms)
.attrTween("d", function (d) {
var interpolate = d3.interpolate(this._current, d);
this._current = interpolate(0);
return function (t) {
return arc(interpolate(t));
};
});
}
Insert cell
color = d3.scaleOrdinal(["#fde946", "#ff6bc3"])
Insert cell
radius = Math.min(width, height) / 2
Insert cell
height = 800 - margin.top - margin.bottom
Insert cell
width = 800 - margin.left - margin.right;
Insert cell
data = [
{ fruit: "Apples", value: 95 },
{ fruit: "Bananas", value: 5 }
]
Insert cell
d3 = require("d3@6")
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