Public
Edited
Apr 25, 2023
Insert cell
Insert cell
chart = {
const root = partition(data);

root.each(d => d.current = d);

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, width])
.style("font", "10px sans-serif");

const g = svg.append("g")
.attr("transform", `translate(${width / 2},${width / 2})`);

const path = g.append("g")
.selectAll("path")
.data(root.descendants().slice(1))
.join("path")
.attr("fill", d => { while (d.depth > 1) d = d.parent; return color(d.data.name); })
.attr("fill-opacity", d => arcVisible(d.current) ? (d.children ? 0.6 : 0.4) : 0)
.attr("pointer-events", d => arcVisible(d.current) ? "auto" : "none")

.attr("d", d => arc(d.current));

path.filter(d => d.children)
.style("cursor", "pointer")
.on("click", clicked);

path.append("title")
.text(d => `${d.ancestors().map(d => d.data.name).reverse().join("/")}\n${format(d.value)}`);

const label = g.append("g")
.attr("pointer-events", "none")
.attr("text-anchor", "middle")
.style("user-select", "none")
.selectAll("text")
.data(root.descendants())
.join("text")
.attr("dy", "40px")
.attr("fill-opacity", d => +labelVisible(d.current))
.attr("transform", d => labelTransform(d.current))
.text(d => d.data.name);

const parent = g.append("circle")
.datum(root)
.attr("r", radius)
.attr("fill", "none")
.attr("pointer-events", "all")
.on("click", clicked);

function clicked(event, p) {
parent.datum(p.parent || root);

root.each(d => d.target = {
x0: Math.max(0, Math.min(1, (d.x0 - p.x0) / (p.x1 - p.x0))) * 2 * Math.PI,
x1: Math.max(0, Math.min(1, (d.x1 - p.x0) / (p.x1 - p.x0))) * 2 * Math.PI,
y0: Math.max(0, d.y0 - p.depth),
y1: Math.max(0, d.y1 - p.depth)
});

const t = g.transition().duration(750);

// Transition the data on all arcs, even the ones that aren’t visible,
// so that if this transition is interrupted, entering arcs will start
// the next transition from the desired position.
path.transition(t)
.tween("data", d => {
const i = d3.interpolate(d.current, d.target);
return t => d.current = i(t);
})
.filter(function(d) {
return +this.getAttribute("fill-opacity") || arcVisible(d.target);
})
.attr("fill-opacity", d => arcVisible(d.target) ? (d.children ? 0.6 : 0.4) : 0)
.attr("pointer-events", d => arcVisible(d.target) ? "auto" : "none")

.attrTween("d", d => () => arc(d.current));

label.filter(function(d) {
return +this.getAttribute("fill-opacity") || labelVisible(d.target);
}).transition(t)
.attr("fill-opacity", d => +labelVisible(d.target))
.attrTween("transform", d => () => labelTransform(d.current));
}
function arcVisible(d) {
return d.y1 <= 3 && d.y0 >= 1 && d.x1 > d.x0;
}

function labelVisible(d) {
return d.y1 <= 3 && d.y0 >= 1 && (d.y1 - d.y0) * (d.x1 - d.x0) > 0.03;
}

function labelTransform(d) {
const x = (d.x0 + d.x1) / 2 * 180 / Math.PI;
const y = (d.y0 + d.y1) / 2 * radius;
return `rotate(${x - 90}) translate(${y},0) rotate(${x < 180 ? 0 : 180})`;
}

return svg.node();
}
Insert cell
partition = data => {
const root = d3.hierarchy(data)
.sum(d => d.value)
.sort((a, b) => b.value - a.value);
return d3.partition()
.size([2 * Math.PI, root.height + 1])
(root);
}
Insert cell
color = d3.scaleOrdinal(d3.quantize(d3.interpolateRainbow, data.children.length + 1))
Insert cell
format = d3.format(",d")
Insert cell
radius = width / 6
Insert cell
arc = d3.arc()
.startAngle(d => d.x0)
.endAngle(d => d.x1)
.padAngle(d => Math.min((d.x1 - d.x0) / 2, 0.005))
.padRadius(radius * 1.5)
.innerRadius(d => d.y0 * radius)
.outerRadius(d => Math.max(d.y0 * radius, d.y1 * radius - 1))
Insert cell
Shark Tank India.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
rawdata = FileAttachment("Shark Tank India.csv").csv()
Insert cell
data =
{
let data = table2Tree(rawdata, ["Received Offer", "Accepted Offer", "Industry"])
let Offerlabels = { 0 : "No offers received",1:"Received offer"}
let recivedOfferLabels = {1:"Accepted",0:"Not Accepted"}
data.children[0]["name"]=Offerlabels[data.children[0]["name"]]
data.children[0].children[0]["name"]=recivedOfferLabels[data.children[0].children[0]["name"]]
data.children[0].children[1]["name"]=recivedOfferLabels[data.children[0].children[1]["name"]]
data.children[1]["name"]=Offerlabels[data.children[1]["name"]]
data.children[1].children[0]["name"] = "No offers received"
data["name"]="Total pitches"
return data

}
Insert cell
table2Tree(rawdata, ["Received Offer", "Accepted Offer", "Industry"])
Insert cell
import { table2Tree } from "@john-guerra/table-2-tree"
Insert cell
trees = TreeValue(data, {
label: (d) => d.name,
value: d=>d.value,
width: 1152,
height: 800
})
Insert cell
import {TreeValue} from "@john-guerra/tree-value"
Insert cell
chart3 = {
const root = partition3(data);
let focus = root;

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height3])
.style("font", "18px sans-serif");

const cell = svg
.selectAll("g")
.data(root.descendants())
.join("g")
.attr("transform", d => `translate(${d.y0},${d.x0})`);

const rect = cell.append("rect")
.attr("width", d => d.y1 - d.y0 - 1)
.attr("height", d => rectHeight(d))
.attr("fill-opacity", 0.6)
.attr("fill", d => {
if (!d.depth) return "#bbb0cd";
while (d.depth > 1) d = d.parent;
return color3(d.data.name);
})
.style("cursor", "pointer")
.on("click", clicked);

const text = cell.append("text")
.style("user-select", "none")
.attr("pointer-events", "none")
.attr("x", 4)
.attr("y", 13)
.attr("fill-opacity", d => +labelVisible(d));

text.append("tspan")
.text(d => d.data.name);

const tspan = text.append("tspan")
.attr("fill-opacity", d => labelVisible(d) * 0.7)
.text(d => ` ${format(d.value)}`);

cell.append("title")
.text(d => `${d.ancestors().map(d => d.data.name).reverse().join("/")}\n${format(d.value)}`);

function clicked(event, p) {
focus = focus === p ? p = p.parent : p;

root.each(d => d.target = {
x0: (d.x0 - p.x0) / (p.x1 - p.x0) * height3,
x1: (d.x1 - p.x0) / (p.x1 - p.x0) * height3,
y0: d.y0 - p.y0,
y1: d.y1 - p.y0
});

const t = cell.transition().duration(750)
.attr("transform", d => `translate(${d.target.y0},${d.target.x0})`);

rect.transition(t).attr("height", d => rectHeight(d.target));
text.transition(t).attr("fill-opacity", d => +labelVisible(d.target));
tspan.transition(t).attr("fill-opacity", d => labelVisible(d.target) * 0.7);
}
function rectHeight(d) {
return d.x1 - d.x0 - Math.min(1, (d.x1 - d.x0) / 2);
}

function labelVisible(d) {
return d.y1 <= width && d.y0 >= 0 && d.x1 - d.x0 > 16;
}
return svg.node();
}
Insert cell
partition3 = data => {
const root = d3.hierarchy(data)
.sum(d => d.value)
.sort((a, b) => b.height - a.height || b.value - a.value);
return d3.partition()
.size([height3, (root.height + 1) * width / 3])
(root);
}
Insert cell
color4 = d3.scaleOrdinal(d3.quantize(d3.interpolateRainbow, data.children.length + 1))
Insert cell
d3.quantize(d3.interpolateRainbow, data.children.length + 1)
Insert cell
color3 = d3.scaleOrdinal().domain(['']).range(['#f04e52', '#005257'])
Insert cell
Swatches(color3)
Insert cell
import {Swatches} from "@d3/color-legend"
Insert cell
height3 = 800
Insert cell
Insert cell
import {SankeyChart} from "@d3/sankey"
Insert cell
chart10 = Icicle(data, {
value: d => d.size, // size of each node (file); null for internal nodes (folders)
label: d => d.name, // display name for each cell
title: (d, n) => `${n.ancestors().reverse().map(d => d.data.name).join(".")}\n${n.value.toLocaleString("en")}`, // hover text
width: 1152,
height: 2400
})
Insert cell
import {Icicle} from "@d3/icicle"
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