Published
Edited
Aug 21, 2020
Insert cell
chart = {
const links = data.links.map(d => Object.create(d));
const nodes = data.nodes.map(d => Object.create(d));

const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));
let div = html`<div><h3>Network with search</h3>`;
div.append(searchf);

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
const g = svg.append("g")
const link = g.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width",d => 10*(d.value**8));
const globalNode = g.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 0.75);
const node = globalNode
//const node = g.append("g")
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", d => 3 + Math.sqrt(d.popularity)*0.9)
.attr("fill", color)
.call(drag(simulation));
let zoomLvl = 1;
let lastK = 0;
node.append("title")
.text(d => d.id);
const textElements = g.append('g')
.selectAll('text')
.data(nodes)
.enter().append('text')
.text(node => node.id)
.attr('font-size', 10)
.attr("font-family", "Nunito")
.attr("fill", "#555")
.attr('dx', 10)
.attr('dy', 4)
var searchNode = function(){
debugger;
if (selectedNode === "none") {
node.style("stroke", "white").style("stroke-width", "1");
} else {
let selected = nodes.filter( d => d.id != selectedNode );

selected.style("opacity", "0");
link.style("opacity", "0");

d3.selectAll(".node, .link").transition()
.duration(5000)
.style("opacity", 1);
}
};
simulation.on("tick", () => {
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);
textElements
.attr("x", d => d.x)
.attr("y", d => d.y);

node
.attr("cx", d => d.x)
.attr("cy", d => d.y);
});

invalidation.then(() => simulation.stop());

svg.call(d3.zoom()
.extent([[0, 0], [width, height]])
.scaleExtent([1, 80])
.on("zoom", zoomed));

function zoomed() {
let e = d3.event
if(e.transform.k > 2 && lastK != e.transform.k){
lastK = e.transform.k;
console.log("zoomed");
zoomLvl = Math.log2(e.transform.k);
g.append("g").attr("stroke-width", 1.5/zoomLvl );
link.attr("stroke-width", d => Math.sqrt(d.value)/(zoomLvl));
}
g.attr("transform", e.transform);
}
return svg.node();
div.appendChild(svg.node());
return div;
}
Insert cell
function autoSelect(config = {}) {
let {
value,
title,
description,
autocomplete = "off",
placeholder,
size,
options,
list = "options"
} = config;

if (Array.isArray(config)) options = config;

const optionsSet = new Set(options);

const form = input({
type: "text",
title,
description,
action: fm => {
fm.value = fm.input.value = value || "";
fm.onsubmit = e => e.preventDefault();
fm.input.oninput = function(e) {
e.stopPropagation();
fm.value = fm.input.value;
if (optionsSet.has(fm.input.value))
fm.dispatchEvent(new CustomEvent("input"));
};
},
form: html`
<form>
<input class="search-box" name="input" type="text" autocomplete="off"
placeholder="${placeholder}" style="font-size: 1em;" list=${list}>
<datalist id="${list}">
${options.map(d =>
Object.assign(html`<option>`, {
value: d
})
)}
</datalist>
</form>
`
});

form.output.remove();
return form;
}
Insert cell
searchf = html`<div>${viewof selectedNode}<button type="button" onclick="searchNode()">Search</button>`
Insert cell
viewof selectedNode = autoSelect({
options: data.nodes.map(d => d.id),
placeholder: "Search a name . . ."
})
Insert cell
import {input} from "@jashkenas/inputs"
Insert cell
data = FileAttachment("halakhachart@1.json").json()
Insert cell
height = 850
Insert cell
width = 1100
Insert cell
color = {
const scale = d3.scaleOrdinal(d3.schemeCategory10);
return d => scale(d.group);
}
Insert cell
drag = simulation => {
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
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