Published
Edited
Jun 23, 2019
Insert cell
Insert cell
chart = {
const root = treemap(data);

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

const shadow = DOM.uid("shadow");

svg.append("filter")
.attr("id", shadow.id)
.append("feDropShadow")
.attr("flood-opacity", 0.3)
.attr("dx", 0)
.attr("stdDeviation", 3);

const node = svg.selectAll("g")
.data(d3.nest().key(d => d.height).entries(root.descendants()))
.join("g")
.attr("filter", shadow)
.selectAll("g")
.data(d => d.values)
.join("g")
.attr("transform", d => `translate(${d.x0},${d.y0})`);

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

node.append("rect")
.attr("id", d => (d.nodeUid = DOM.uid("node")).id)
.attr("fill", d => color(d.height))
.attr("width", d => d.x1 - d.x0)
.attr("height", d => d.y1 - d.y0);

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

node.append("text")
.attr("clip-path", d => d.clipUid)
.selectAll("tspan")
.data(d => d.data.name.split(/(?=[A-Z][^A-Z])/g).concat(format(d.value)))
.join("tspan")
.attr("fill-opacity", (d, i, nodes) => i === nodes.length - 1 ? 0.7 : null)
.text(d => d);

node.filter(d => d.children).selectAll("tspan")
.attr("dx", 3)
.attr("y", 13);

node.filter(d => !d.children).selectAll("tspan")
.attr("x", 3)
.attr("y", (d, i, nodes) => `${(i === nodes.length - 1) * 0.3 + 1.1 + i * 0.9}em`);

return svg.node();
}
Insert cell
data = {
var query = " PREFIX dcterms: <http://purl.org/dc/terms/> \n";
query += " PREFIX dcndl: <http://ndl.go.jp/dcndl/terms/> \n";
query += " SELECT DISTINCT count(?s) as ?c ?type ?org ?collection WHERE { \n";
query += " ?s dcndl:materialType ?type . \n";
query += " ?s dcndl:digitizedPublisher ?org . \n";
query += " filter(lang(?org) = \"ja\") \n";
query += " ?s dcterms:isPartOf ?collection . \n";
query += " filter(lang(?collection) = \"ja\") \n";
query += " } \n";
var url = "https://sparql.dl.itc.u-tokyo.ac.jp/?query=" + encodeURIComponent(query) + "&output=json"
var data_ = await d3.json(url);
var result = data_.results.bindings;
var data = []
for (var i = 0; i < result.length; i++) {
var obj = result[i]
var collection = obj.collection.value
var org = obj.org.value
var type = obj.type.value.replace("http://ndl.go.jp/ndltype/", "")
var c = Number(obj.c.value)
data.push({
"name": org,
"parent": null
})
data.push({
"name": collection,
"parent": org
})
data.push({
"name": type,
"parent": collection,
"value": c
})
}

// create a name: node map
var dataMap = {};
data.forEach(function (node) {
dataMap[node.name] = node;
});
// create the tree array
var tree = [];
data.forEach(function (node) {
// add to parent
var parent = dataMap[node.parent];
if (parent) {
// create child array if it doesn't exist
(parent.children || (parent.children = []))
// add node to child array
.push(node);
} else {
// parent is null or missing
tree.push(node);
}
});
tree = {
"name": "UTokyo Academic Archives Portal",
"children": tree
}
return tree;
}
Insert cell
treemap = data => d3.treemap()
.size([width, height])
.paddingOuter(3)
.paddingTop(19)
.paddingInner(1)
.round(true)
(d3.hierarchy(data)
.sum(d => d.value)
.sort((a, b) => b.value - a.value))
Insert cell
width = 975
Insert cell
height = 1060
Insert cell
format = d3.format(",d")
Insert cell
color = d3.scaleSequential([8, 0], d3.interpolateMagma)
Insert cell
d3 = require("d3@5")
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