Published
Edited
Jun 2, 2019
Insert cell
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('x', d3.forceX().strength(2).x(d =>width/(maxLayer+1) * (d.LayerNum+1)))
.force("charge", d3.forceManyBody().strength(-250))
.force("link", d3.forceLink(links).id(d => d.id))
.force("center", d3.forceCenter(width / 2, height / 2));

const svg = d3.select(DOM.svg(width, height));

const link = svg.append("g")
.selectAll("line")
.data(links)
.join("line")
.attr("stroke", d => d.weight> 0? "#FF374C":"#6495ED")
.attr("stroke-opacity", d => Math.abs(d.weight)/maxwt)
.attr("stroke-width", d => Math.abs(d.weight)>wtcut? Math.abs(d.weight)*2 : 0);
const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", 5)
// .attr("fill", "#000000")
.attr("fill", color)
.call(drag(simulation));

function distance() {
return 180;
}
node.append("title")
.text(d => d.id);

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);

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

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

return svg.node();
}
Insert cell
md `## Data Import`
Insert cell
data = d3.json("https://raw.githubusercontent.com/cse512-19s/FP-Visualizing-neural-network-architecture/master/docs/NetworkGraph.json?token=AJUZNU6LRIOHZJDQQODFAAS47WANQ")
Insert cell
height = 800
Insert cell
width =1000
Insert cell
maxLayer = d3.max(data.nodes, d => d.LayerNum)
Insert cell
maxwt = d3.max(data.links, d => Math.abs(d.weight))
Insert cell
color = {
const scale = d3.scaleOrdinal(d3.schemeCategory10 );
return d => scale(d.LayerNum);
}
Insert cell
drag = simulation => {
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(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
import {number} from "@jashkenas/inputs"
Insert cell
viewof wtcut = number({placeholder: "0.055", title: "Wts cut", submit: true})
Insert cell
import {slider} from "@jashkenas/inputs"
Insert cell
viewof phi0= slider({
min: 0,
max: 100,
step: 1,
title: "phi0",
description: "input for phi0"
})
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