Published unlisted
Edited
1 star
Insert cell
Insert cell
Insert cell
//test_0.ancestors() // Returns the array of ancestors nodes, starting with this node, then followed by each parent up to the root.
Insert cell
//test_0.descendants() // Returns the array of descendant nodes, starting with this node, then followed by each child in topological order.
Insert cell
//test_0.leaves() // Returns the array of leaf nodes in traversal order; leaves are nodes with no children.
Insert cell
//test_0.links() // Returns an array of links for this node, where each link is an object that defines source and target properties. The source of each link is the parent node, and the target is a child node.
Insert cell
Insert cell
Insert cell
Insert cell
chartB = { // from https://observablehq.com/@d3/treemapdebugger
const root = treemap(rolledup);

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

const leaf = svg.selectAll("g")
.data(root.leaves())
.join("g")
.attr("transform", d => `translate(${d.x0},${d.y0})`); // x and y to draw eveything of the element at

leaf.append("title") // default mouse over image text
.text(d => `${d.ancestors().reverse().map(d => d.data[0]).join("\n")}\n${format(d.value)}`);
leaf.append("rect")
.attr("id", d => (d.leafUid = DOM.uid("leaf")).id)
.attr("fill", d => { while (d.depth > 1) d = d.parent;
return teColor[d.data[0]-1990];}) // colour based on year.
.attr("fill-opacity", 0.6)
.attr("width", d => d.x1 - d.x0)
.attr("height", d => d.y1 - d.y0);

leaf.append("clipPath")
.attr("id", d => (d.clipUid = DOM.uid("clip")).id)
.append("use").attr("xlink:href", d => d.leafUid.href);

leaf.append("text")
.attr("clip-path", d => d.clipUid)
.selectAll("tspan") // https://svgwg.org/svg2-draft/text.html#TextElement
.data(d => (d.parent.data[0]+emoji[d.data[0]]+'\n'+format(d.value)+' kg').split(/(?=[A-Z][a-z])|\s+/g) )

.join("tspan")
.attr("x", 3)
.attr("y", (d, i, nodes) => `${(i === nodes.length - 1) * 0.3 + 1.1 + i * 0.9}em`) /// return 0 or 1 * 0.3...
.attr('fill','black')
.text(d => d);

return svg.node();
}
Insert cell
treemap = data => d3.treemap()
.tile(d3.treemapBinary)
.size([width, height])
.padding(1)
.round(true)
(hierarchy(data))

Insert cell
hierarchy = data => (d3.hierarchy(data)
.sum(([,d]) => d)
// .sort((a, b) => b.value - a.value)
)
Insert cell

rolledup = d3.rollup(cans,
([v]) => +v[value],
...keys.map(k => d => d[k]))
Insert cell
keys = cans.columns.slice(0, -1) // grab all but the last key?
Insert cell
value = cans.columns.slice(-1) // last entry is the values data
Insert cell
Insert cell
Insert cell
format = d3.format("s")
Insert cell
Insert cell
Insert cell
cans_csv = FileAttachment("Canter no3 data - no3_Canterbury.csv").csv()
Insert cell
cans_csv.columns
Insert cell
cans = cans_csv.map(d => ({'year':d.year,"animal": d.animal,"no3": d.no3_kg_per_yr }))
Insert cell
cans.columns = ['year','animal','no3'] // add the columns property
Insert cell
Insert cell
Insert cell
teColor = ["#FF0000", "#FFFF00", "#00EAFF", "#AA00FF", "#FF7F00", "#BFFF00", "#0095FF", "#FF00AA", "#FFD400", "#6AFF00", "#0040FF", "#EDB9B9", "#B9D7ED", "#E7E9B9", "#DCB9ED", "#B9EDE0", "#8F2323", "#23628F", "#8F6A23", "#6B238F", "#4F8F23", "#004200", "#427373", "#CC42CC", "#f782c2", "#6592fd", "#f6d912", "#1ac0c6", "#ffa822", "#fdfa66", "#d527b7", "#efeeb4", "#f9b4ab", "#8f3b76", "#fefcbf", "#ddacf5", "#44c2fd", "#e74645", "#2a6fdb", "#f5487f", "#ed8a0a", "#fff29c", "#f56038", "#ffca7a", "#bae8e8", "#f7a325", "#58b368", "#e3f6f5", "#95adbe", "#f1e8e6", "#facd60", "#dad873", "#64379f", "#072448"]
Insert cell
color = d3.scaleOrdinal(d3.schemeCategory10)
Insert cell
Insert cell
d3 = require('d3')
Insert cell
_ = require('lodash')
Insert cell
<style>

@media (hover: hover) {
.katex-display,p,h1,h2,h3,table,li
{
max-width: 1100px;
min-width: 1100px;
}
}
.observablehq {
padding-left: 14px;
}

pre {
display: block;
font-family: monospace;
white-space: pre;
margin: 1em 0px;
}

body {font-family: Roboto;}

</style>
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