Published
Edited
Jun 23, 2019
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const {nodes, links} = sankey(data);

svg.append("g")
.attr("stroke", "#000")
.selectAll("rect")
.data(nodes)
.join("rect")
.attr("x", d => d.x0)
.attr("y", d => d.y0)
.attr("height", d => d.y1 - d.y0)
.attr("width", d => d.x1 - d.x0)
.attr("fill", d => color(d.name))
.append("title")
.text(d => `${d.name}\n${format(d.value)}`);

const link = svg.append("g")
.attr("fill", "none")
.attr("stroke-opacity", 0.5)
.selectAll("g")
.data(links)
.join("g")
.style("mix-blend-mode", "multiply");

if (edgeColor === "path") {
const gradient = link.append("linearGradient")
.attr("id", d => (d.uid = DOM.uid("link")).id)
.attr("gradientUnits", "userSpaceOnUse")
.attr("x1", d => d.source.x1)
.attr("x2", d => d.target.x0);

gradient.append("stop")
.attr("offset", "0%")
.attr("stop-color", d => color(d.source.name));

gradient.append("stop")
.attr("offset", "100%")
.attr("stop-color", d => color(d.target.name));
}

link.append("path")
.attr("d", d3.sankeyLinkHorizontal())
.attr("stroke", d => edgeColor === "none" ? "#aaa"
: edgeColor === "path" ? d.uid
: edgeColor === "input" ? color(d.source.name)
: color(d.target.name))
.attr("stroke-width", d => Math.max(1, d.width));

link.append("title")
.text(d => `${d.source.name} → ${d.target.name}\n${format(d.value)}`);

svg.append("g")
.style("font", "10px sans-serif")
.selectAll("text")
.data(nodes)
.join("text")
.attr("x", d => d.x0 < width / 2 ? d.x1 + 6 : d.x0 - 6)
.attr("y", d => (d.y1 + d.y0) / 2)
.attr("dy", "0.35em")
.attr("text-anchor", d => d.x0 < width / 2 ? "start" : "end")
.text(d => d.name);

return svg.node();
}
Insert cell
sankey = {
const sankey = d3.sankey()
.nodeAlign(d3[`sankey${align[0].toUpperCase()}${align.slice(1)}`])
.nodeWidth(15)
.nodePadding(10)
.extent([[1, 5], [width - 1, height - 5]]);
return ({nodes, links}) => sankey({
nodes: nodes.map(d => Object.assign({}, d)),
links: links.map(d => Object.assign({}, d))
});
}
Insert cell
format = {
const f = d3.format(",.0f");
return d => `${f(d)} TWh`;
}
Insert cell
color = {
const color = d3.scaleOrdinal(d3.schemeCategory10);
return name => color(name.replace(/ .*/, ""));
}
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 WHERE { \n";
query += " ?sourceInfo <http://schema.org/provider> ?sp . \n";
query += " ?sp <http://schema.org/sourceOrganization> ?so .\n";
query += " ?so <http://www.w3.org/2000/01/rdf-schema#label> ?org . \n";
query += " ?s <https://jpsearch.go.jp/term/property#sourceInfo> ?sourceInfo . \n";
query += " ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?type \n";
query += " } \n";
var url = "https://jpsearch.go.jp/rdf/sparql?query="+encodeURIComponent(query)+"&output=json"
var data_ = await d3.json(url);
var nodes = []
var group = {}
var links = []
var nodes_ = []
var result = data_.results.bindings;

for (var i = 0; i < result.length; i++) {
var obj = result[i]
var org = obj.org.value
var type = obj.type.value.replace("https://jpsearch.go.jp/term/type/", "")

if (nodes.indexOf(org) == -1) {
nodes.push(org)
group[org] = "0"
nodes_.push({
name: org,
group: group[org]
})
}

if (nodes.indexOf(type) == -1) {
nodes.push(type)
group[type] = "1"
nodes_.push({
name: type,
group: group[type]
})
}

var org_index = nodes.indexOf(org)
var type_index = nodes.indexOf(type)

links.push({
source: org_index,
target: type_index,
value: Number(obj.c.value)
})
}

var data = {
nodes: nodes_,
links: links
}
return data;
}
Insert cell
width = 975
Insert cell
height = 600
Insert cell
d3 = require("d3@5", "d3-sankey@0.12")
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