Published
Edited
Feb 20, 2021
Fork of Untitled
2 forks
Insert cell
import {select} from '@jashkenas/inputs'
Insert cell
viewof selection = {
const files = new Map([
FileAttachment("map@1.json"),
FileAttachment("map@2.json")
].map(f => [f.name, f]));
const form = select({
description: "Select Dataset",
options: Array.from(files.keys()),
value: "map@2.json"
});
return Object.defineProperty(html`<div>${form}`, 'value', {get() { return files.get(form.value) }});
}
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
data = selection.json()

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 => (d.value/10));
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=>d.size)
.attr("fill", color)
//.call(drag(simulation));
let zoomLvl = 1;
let lastK = 0;
node.append("title")
.text(d => d.label);
const textElements = g.append('g')
.selectAll('text')
.data(nodes)
.enter().append('text')
.text(node => node.label)
.attr('font-size', 8)
.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([-10, 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));
textElements.attr('font-size', 10/zoomLvl);
textElements.attr('dx',10/zoomLvl);
textElements.attr('dy',5/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 = (await fetch("http://128.42.130.34/flask/graph")).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

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