Skip to content

Instantly share code, notes, and snippets.

@babette-landmesser
Created June 1, 2020 12:46
Show Gist options
  • Save babette-landmesser/e82e4a4e35b1a4d40cd3e0fb362206bc to your computer and use it in GitHub Desktop.
Save babette-landmesser/e82e4a4e35b1a4d40cd3e0fb362206bc to your computer and use it in GitHub Desktop.
D3.js sitemap creation
export default {
data: [
{"name":"mediaman.com/"},
{"name":"mediaman.com/leistungen/digitale-beratung"},
{"name":"mediaman.com/leistungen/data-sprint"},
{"name":"mediaman.com/leistungen/data-ideation-workshop"},
{"name":"mediaman.com/agentur"},
{"name":"mediaman.com/jobs"},
{"name":"mediaman.com/datenschutzhinweise"},
{"name":"mediaman.com/impressum"},
{"name":"mediaman.com/projekte/hyundai-online-showroom"},
{"name":"mediaman.com/leistungen/ux-research"},
{"name":"mediaman.com/jobs#c377"},
{"name":"mediaman.com/projekte"},
{"name":"mediaman.com/leistungen/ux-scorecard"},
{"name":"mediaman.com/leistungen/cpux-f-schulung"},
{"name":"mediaman.com/projekte/fahrzeugberatung-durch-digitale-assistenten"},
{"name":"mediaman.com/projekte/basler-website-relaunch"},
{"name":"mediaman.com/projekte/objekterkennung-in-der-wartung"},
{"name":"mediaman.com/projekte/suzuki-website"},
{"name":"mediaman.com/projekte/connected-car-app"},
{"name":"mediaman.com/projekte/haier-digitale-plattformstrategie"},
{"name":"mediaman.com/projekte/fascination-porsche"},
{"name":"mediaman.com/projekte/roewe-smart-store"},
{"name":"mediaman.com/projekte/retail-challenge-portal"},
{"name":"mediaman.com/projekte/daimler-credit-risk-digital"},
{"name":"mediaman.com/projekte/kaercher-b2b-ecommerce"},
{"name":"mediaman.com/projekte/kaeser-business-website-relaunch"},
{"name":"mediaman.com/projekte/fissler-content-commerce-plattform"},
{"name":"mediaman.com/projekte/hyundai-car-configurator"},
{"name":"mediaman.com/projekte/hyundai-i30n-online-presales"},
{"name":"mediaman.com/projekte/mercedes-me-portal"},
{"name":"mediaman.com/leistungen/conversational-user-interfaces"}
]
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tree</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="build/main.js"></script>
</head>
<body>
<img>
<script type="module">
var chart = window.treeChart();
var img = document.querySelector('img');
// get svg data
var xml = new XMLSerializer().serializeToString(chart);
// make it base64
var svg64 = btoa(xml);
var b64Start = 'data:image/svg+xml;base64,';
// prepend a "header"
var image64 = b64Start + svg64;
// set it as the source of the img element
img.src = image64;
</script>
</body>
</html>
import source from './data.js';
const data = source.data;
const width = 1440;
const processData = (data) => {
const base = data[0].name;
// assuming, the first entry of data is the parent of all
const newData = {
name: base,
children: [],
};
data.forEach(({ name }) => {
let path = name;
if (name.indexOf(base) !== -1) {
path = name.substring(base.length, name.length);
}
if (path === '') {
return;
}
if (path.indexOf('/') === -1) {
if (!newData.children.find(child => child === path)) {
newData.children.push({name: path, children: []});
};
} else {
const parent = path.substring(0, path.indexOf('/'));
let parentObj = newData.children.find((child) => child.name === parent);
if (!parentObj) {
let quickParent = data.find(d => d.name === base + parent);
if (!quickParent) {
quickParent = {
name: parent,
}
} else {
quickParent.name = quickParent.name.substring(base.length, quickParent.name.length)
}
const parentObjIndex = newData.children.push(quickParent);
parentObj = newData.children[parentObjIndex-1];
}
path = path.substring(path.indexOf('/') + 1, path.length);
if (parentObj.children) {
parentObj.children.push({name: path});
} else {
parentObj.children = [{name: path}];
}
}
});
return newData;
};
const tree = (treeData) => {
const root = d3.hierarchy(treeData);
root.dx = 10;
root.dy = width / (root.height + 1);
return d3.tree().nodeSize([root.dx, root.dy])(root);
};
const chart = () => {
const processedData = processData(data);
const root = tree(processedData);
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", 8)
.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();
}
window.treeChart = chart;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment