Published
Edited
Dec 10, 2020
1 fork
Insert cell
md`# Lytics Wiz + d3 Playground`
Insert cell
d3 = require("d3@6")
Insert cell
Insert cell
example = wiz.enter({
entrypoint:'flow',
flows:[
{id: 'flow', steps: ['1','2','3','choice']}
],
choices: [
{
id: 'choice',
options: ["A","B","C"]
}
],
steps:[
{id:'1'},
{id:'2',alt:'2.5'},
{id:'2.5'},
{id:'3'},
{id:'A'},
{id:'B'},
{id:'C'}
]
})
Insert cell
graph(d3.hierarchy(example))
Insert cell
graph(d3.hierarchy(example.next()))
Insert cell
graph(d3.hierarchy(example.next().alt()))
Insert cell
graph(d3.hierarchy(example.next().next()))
Insert cell
graph(d3.hierarchy(example.next().alt().next().next()))
Insert cell
graph(d3.hierarchy(example.next().alt().next().next().next("B")))
Insert cell
graph(d3.hierarchy(example.next().next().next().next("C"), it => it.edges.in))
Insert cell
dx = 12
Insert cell
dy = 120
Insert cell
tree = d3.tree().nodeSize([dx, dy])
Insert cell
Insert cell
function graph(root, {
label = d => d.data.spec.id,
highlight = () => false,
marginLeft = 40
} = {}) {
root = tree(root);

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 + dx * 2])
.style("overflow", "visible");
const g = svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("transform", `translate(${marginLeft},${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("stroke", d => highlight(d.source) && highlight(d.target) ? "red" : null)
.attr("stroke-opacity", d => highlight(d.source) && highlight(d.target) ? 1 : null)
.attr("d", treeLink);
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 => highlight(d) ? "red" : d.children ? "#555" : "#999")
.attr("r", 2.5);

node.append("text")
.attr("fill", d => highlight(d) ? "red" : null)
.attr("dy", "0.31em")
.attr("x", d => d.children ? -6 : 6)
.attr("text-anchor", d => d.children ? "end" : "start")
.text(label)
.clone(true).lower()
.attr("stroke", "white");
return svg.node();
}
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