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

let x0 = Infinity;
let x1 = -x0;
root.each(d => {
if (d.x > x1) x1 = d.x;
if (d.x < x0) x0 = d.x;
});

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, x1 - x0 + root.dx * 2]);
const g = svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("transform", `translate(${root.dy / 3},${root.dx - x0})`);
const link = g.append("g")
.attr("fill", "none")
.attr("stroke", "#555")
.attr("stroke-opacity", 0.4)
.attr("stroke-width", 1.5)
.selectAll("path")
.data(root.links())
.join("path")
.attr("d", d3.linkHorizontal()
.x(d => d.y)
.y(d => d.x));
const node = g.append("g")
.attr("stroke-linejoin", "round")
.attr("stroke-width", 3)
.selectAll("g")
.data(root.descendants())
.join("g")
.attr("transform", d => `translate(${d.y},${d.x})`);

node.append("circle")
.attr("fill", d => d.children ? "#555" : "#999")
.attr("r", 2.5);

node.append("text")
.attr("dy", "0.31em")
.attr("x", d => d.children ? -6 : 6)
.attr("text-anchor", d => d.children ? "end" : "start")
.text(d => d.data.name)
.clone(true).lower()
.attr("stroke", "white");
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 = []
var orgs = []
var collections = []
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)
if(orgs.indexOf(org) == -1){
data.push({
"name": org,
"parent": null
})
orgs.push(org)
}
if(collections.indexOf(collection) == -1){
data.push({
"name": collection,
"parent": org
})
collections.push(collection)
}
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
tree = data => {
const root = d3.hierarchy(data);
root.dx = 10;
root.dy = width / (root.height + 1);
return d3.tree().nodeSize([root.dx, root.dy])(root);
}
Insert cell
width = 932
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