Published
Edited
Aug 27, 2021
1 star
Insert cell
Insert cell
Insert cell
fullscreen = {
const button = html`<button>Fullscreen`;
button.onclick = () => chart.requestFullscreen();
return button;
}

// code from https://observablehq.com/@mbostock/fullscreen-canvas
Insert cell
Insert cell
update = chart.update(data, counter)
Insert cell
md`# Input Parameters`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof speed = {

return slider({
min: .3,
max: 10,
step: .1,
title: "Playback Speed",
value: 1
})
}
Insert cell
Insert cell
md`# Data and Random Walk`
Insert cell
Insert cell
Insert cell
Insert cell
md` # Other Parameters`
Insert cell
md` ## Parameters for link selection`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mutable chosen_edge = null
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
palette_outer = d3.interpolateViridis
Insert cell
palette_inner = d3.interpolateRdBu
Insert cell
Insert cell
/*color = d3.scaleLinear().range([.1,1]).domain(d3.extent(data.nodes,d=>d.comm[0])) */
Insert cell
color_outer = {
const scale = d3.scaleOrdinal(d3.schemePastel1);
return d => scale(d);
}
Insert cell
color_inner = {
const scale = d3.scaleOrdinal(d3.schemeDark2);
return d => scale(d);
}
Insert cell
Insert cell
md `## Scale Link Color`
Insert cell
Insert cell
md`## Scale Node Opacity to show level 1 communities`
Insert cell
l2scale = {
const scale = d3.scaleLinear().range([.3, 1]);
return d => scale(d.comm[1]);
}
Insert cell
Insert cell
Insert cell
viewof counter = Inputs.button([["Increment", value => value + 1],["Reset", value=> -1]], {value: -1})
Insert cell
play(random_walk[counter].note);
Insert cell
counter
Insert cell
/*
counter = {
let i=0;
while(i < random_walk.length) {
start_walk;
play(random_walk[i].note);
yield ++i;
//yield Promises.delay(random_walk[i].duration / speed, ++i);
}

}
*/
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
/*code adapted from https://observablehq.com/@d3/collapsible-tree */
chart_dendro = {
const root = d3.hierarchy(data_dendro);

root.x0 = dy / 2;
root.y0 = 0;
root.descendants().forEach((d, i) => {
d.id = i;
d._children = d.children;
if (d.depth && d.data.name.length !== 7) d.children = null;
});

const svg = d3.create("svg")
.attr("viewBox", [-margin.left, -margin.top, width, dx])
.style("font", "15px sans-serif")
.style("user-select", "none");

const gLink = svg.append("g")
.attr("fill", "none")
.attr("stroke", "#555")
.attr("stroke-opacity", 0.4)
.attr("stroke-width", 1.5);

const gNode = svg.append("g")
.attr("cursor", "pointer")
.attr("pointer-events", "all");

function update(source) {
const duration = d3.event && d3.event.altKey ? 2500 : 250;
const nodes = root.descendants().reverse();
const links = root.links();

// Compute the new tree layout.
tree(root);

let left = root;
let right = root;
root.eachBefore(node => {
if (node.x < left.x) left = node;
if (node.x > right.x) right = node;
});

const height_dendro = right.x - left.x + margin.top + margin.bottom;

const transition = svg.transition()
.duration(duration)
.attr("viewBox", [-margin.left, left.x - margin.top, width, height_dendro])
.tween("resize", window.ResizeObserver ? null : () => () => svg.dispatch("toggle"));

// Update the nodes…
const node = gNode.selectAll("g")
.data(nodes, d => d.id);

// Enter any new nodes at the parent's previous position.
const nodeEnter = node.enter().append("g")
.attr("transform", d => `translate(${source.y0},${source.x0})`)
.attr("fill-opacity", 0)
.attr("stroke-opacity", 0)
.on("click", (event, d) => {
d.children = d.children ? null : d._children;
update(d);
});

nodeEnter.append("circle")
.attr("r", d=>d._children ? 8 : 4)//radius/4)
.attr("fill", function(d){
if(d.data.level == -1){
return 'gray'
}
else if(d.data.level == 0){
return color_inner(d.data.comm);
}
else{
return color_outer(d.data.comm);
}
})//color_outer) fill this with community color
.attr("stroke-width", 10);

nodeEnter.append("text")
.attr("dy", ".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-linejoin", "round")
.attr("stroke-width", 10)
.attr("stroke", "white");

// Transition nodes to their new position.
const nodeUpdate = node.merge(nodeEnter).transition(transition)
.attr("transform", d => `translate(${d.y},${d.x})`)
.attr("fill-opacity", 1)
.attr("stroke-opacity", 1);

// Transition exiting nodes to the parent's new position.
const nodeExit = node.exit().transition(transition).remove()
.attr("transform", d => `translate(${source.y},${source.x})`)
.attr("fill-opacity", 0)
.attr("stroke-opacity", 0);

// Update the links…
const link = gLink.selectAll("path")
.data(links, d => d.target.id);

// Enter any new links at the parent's previous position.
const linkEnter = link.enter().append("path")
.attr("d", d => {
const o = {x: source.x0, y: source.y0};
return diagonal({source: o, target: o});
});

// Transition links to their new position.
link.merge(linkEnter).transition(transition)
.attr("d", diagonal);

// Transition exiting nodes to the parent's new position.
link.exit().transition(transition).remove()
.attr("d", d => {
const o = {x: source.x, y: source.y};
return diagonal({source: o, target: o});
});

// Stash the old positions for transition.
root.eachBefore(d => {
d.x0 = d.x;
d.y0 = d.y;
});
}

update(root);

return svg.node();
}
Insert cell
dy = width / 4
Insert cell
dx=width/40
Insert cell
data_dendro = FileAttachment("dendrogram@1.json").json()
Insert cell
tree = d3.tree().nodeSize([dx, dy])
Insert cell
margin = ({top: 10, right: 10, bottom: 10, left: width/5})
Insert cell
diagonal = d3.linkHorizontal().x(d => d.y).y(d => d.x)
Insert cell
md `# Imports`
Insert cell
d3 = require("d3@6")
Insert cell
import {slider} from "@jashkenas/inputs"
Insert cell
Insert cell
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