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
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", d=>d.size)
.attr("fill", color)
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;
}