Spaces:
Sleeping
Sleeping
Update app.py
Browse files224 unmodified lines22522622722822923023112 unmodified lines24424524624724824925025125225325425525625725825926026126226326426526626726826927062 unmodified lines33333433533633733833934011 unmodified lines3523533543553563573583593603613623633643653663675 unmodified lines3733743753763773783793803813823831 unmodified line38538638738838939039139239310 unmodified lines40440540640740840941041141241341428 unmodified lines4434444454464474484494504514524534544554564574584 unmodified lines46346446546646746846927 unmodified lines497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531224 unmodified linesconst radius = Math.min(w, h) * 0.36;// Compute node positions with abbrevconst n = NODES.length;function angleFor(i){ return (i / n) * 2 * Math.PI; }12 unmodified linesconst nameToIndex = {};NODES.forEach((nm, i) => nameToIndex[nm] = i);// --------------------------------------------------------------// 1. Draw circles FIRST (bottom layer)// --------------------------------------------------------------const nodeCircleGroup = svg.append("g").attr("class", "node-circles").selectAll("g").data(nodePos).enter().append("g").attr("transform", d => `translate(${d.x},${d.y})`);nodeCircleGroup.append("circle").attr("r", 16).attr("fill", d => NODE_TYPE[d.name] === "amc" ? "#2b6fa6" : "#f2c88d").attr("stroke", "#222").attr("stroke-width", 1).style("cursor", "pointer");// --------------------------------------------------------------// ARC DRAWING (middle layers)// --------------------------------------------------------------function bezierPath(x0,y0,x1,y1,above=true){const mx = (x0+x1)/2;const my = (y0+y1)/2;62 unmodified lines.attr("data-tgt", tname);});// original loop arcs (keep logic but style to violet and slightly thicker)const loopGroup = svg.append("g").attr("class", "loops");LOOPS.forEach(lp => {const a = lp[0], c = lp[1], b = lp[2];11 unmodified linesloopGroup.append("path").attr("d", path).attr("fill", "none").attr("stroke", "#9b59b6") // violet to stand out.attr("stroke-width", 4.2).attr("opacity", 1).attr("stroke-linecap", "round").attr("stroke-linejoin", "round");});// --------------------------------------------------------------// Overlay group for dynamic highlighted loop paths (drawn on click)// --------------------------------------------------------------const loopPathGroup = svg.append("g").attr("class", "loop-path-highlight");function drawLoopPathsFor(nodeName) {5 unmodified linesconst pA = nodePos[nameToIndex[amcA]];const pC = nodePos[nameToIndex[company]];const pB = nodePos[nameToIndex[amcB]];// draw two-segment polyline that follows straight segments A->C and C->BloopPathGroup.append("path").attr("d", `M${pA.x},${pA.y} L${pC.x},${pC.y} L${pB.x},${pB.y}`).attr("fill", "none").attr("stroke", "#8e44ad") // slightly different violet for highlight.attr("stroke-width", 4.5).attr("opacity", 0.98).attr("stroke-linecap", "round")1 unmodified line});}// --------------------------------------------------------------// 2. LABELS AT THE TOP (always visible)// --------------------------------------------------------------const labelGroup = svg.append("g").attr("class", "node-labels").selectAll("text")10 unmodified linesconst deg = (d.angle * 180 / Math.PI);return (deg>-90 && deg<90) ? "start" : "end";}).text(d => d.abbrev);// --------------------------------------------------------------// LABEL SWITCHING + OPACITY CONTROLS// --------------------------------------------------------------function getConnections(nodeName){let buys = BUYS.filter(x=>x[0]===nodeName || x[1]===nodeName).map(x=>x[0]===nodeName?x[1]:x[0]);let sells = SELLS.filter(x=>x[0]===nodeName || x[1]===nodeName).map(x=>x[0]===nodeName?x[1]:x[0]);28 unmodified linessellGroup.selectAll("path").style("opacity", function(){ return isConn(this)?0.98:0.06; });transferGroup.selectAll("path").style("opacity", function(){ return isConn(this)?0.98:0.06; });// keep loopGroup visible but slightly dim non-relevant onesloopGroup.selectAll("path").style("opacity", function(){// loops don't have data-src/data-tgt; approximate by checking endpointstry {const dstr = this.getAttribute("d") || "";// simple heuristic: if path contains nodeName coordinate string, highlight it// (we'll just keep them at 0.9 for readability)return 0.9;} catch(e){ return 0.9; }});}function resetOpacity(){4 unmodified linestransferGroup.selectAll("path").style("opacity",0.7);loopGroup.selectAll("path").style("opacity",1);loopPathGroup.selectAll("*").remove(); // clear highlight overlayupdateLabels(null, new Set());document.getElementById("info-box").innerHTML ="<b>Click a node</b> to view details here.";27 unmodified linesshowInfo(name);const connected = getConnections(name);updateLabels(name, connected);drawLoopPathsFor(name); // draw overlay highlights for loops involving this node}nodeCircleGroup.on("click", function(event, d){selectNode(d);event.stopPropagation();});labelGroup.on("click", function(event, d){selectNode(d);event.stopPropagation();});document.getElementById("arc-reset").onclick = resetOpacity;svg.on("click", function(evt){// Only reset when clicking empty background, not circles, not labels, not pathsif (evt.target === this) {resetOpacity();}});}draw();window.addEventListener("resize", draw);</script>"""def make_arc_html(nodes, node_type, buys, sells, transfers, loops):nodes_json = json.dumps(nodes)node_type_json = json.dumps(node_type)224 unmodified lines22522622722822923012 unmodified lines24324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327462 unmodified lines33733833934034134211 unmodified lines3543553563573583593603613623633643653665 unmodified lines3723733743753763773783793803811 unmodified line38338438538638738838939039110 unmodified lines40240340440540640740840941041141241328 unmodified lines4424434444454464474484 unmodified lines45345445545645745845927 unmodified lines487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530224 unmodified linesconst radius = Math.min(w, h) * 0.36;const n = NODES.length;function angleFor(i){ return (i / n) * 2 * Math.PI; }12 unmodified linesconst nameToIndex = {};NODES.forEach((nm, i) => nameToIndex[nm] = i);//--------------------------------------------------------------------// NODES — FIX #1: disable pointer events on <g>//--------------------------------------------------------------------const nodeCircleGroup = svg.append("g").attr("class", "node-circles").selectAll("g").data(nodePos).enter().append("g").attr("transform", d => `translate(${d.x},${d.y})`).style("pointer-events", "none"); // ★ FIX ADDED//--------------------------------------------------------------------// CIRCLES — FIX #2: reenable pointer events on <circle>//--------------------------------------------------------------------nodeCircleGroup.append("circle").attr("r", 16).attr("fill", d => NODE_TYPE[d.name] === "amc" ? "#2b6fa6" : "#f2c88d").attr("stroke", "#222").attr("stroke-width", 1).style("cursor", "pointer").style("pointer-events", "all"); // ★ FIX ADDED//--------------------------------------------------------------------// ARC drawing (unchanged)//--------------------------------------------------------------------function bezierPath(x0,y0,x1,y1,above=true){const mx = (x0+x1)/2;const my = (y0+y1)/2;62 unmodified lines.attr("data-tgt", tname);});const loopGroup = svg.append("g").attr("class", "loops");LOOPS.forEach(lp => {const a = lp[0], c = lp[1], b = lp[2];11 unmodified linesloopGroup.append("path").attr("d", path).attr("fill", "none").attr("stroke", "#9b59b6").attr("stroke-width", 4.2).attr("opacity", 1).attr("stroke-linecap", "round").attr("stroke-linejoin", "round");});const loopPathGroup = svg.append("g").attr("class", "loop-path-highlight");function drawLoopPathsFor(nodeName) {5 unmodified linesconst pA = nodePos[nameToIndex[amcA]];const pC = nodePos[nameToIndex[company]];const pB = nodePos[nameToIndex[amcB]];loopPathGroup.append("path").attr("d", `M${pA.x},${pA.y} L${pC.x},${pC.y} L${pB.x},${pB.y}`).attr("fill", "none").attr("stroke", "#8e44ad").attr("stroke-width", 4.5).attr("opacity", 0.98).attr("stroke-linecap", "round")1 unmodified line});}//--------------------------------------------------------------------// LABELS — FIX #3: enable pointer events//--------------------------------------------------------------------const labelGroup = svg.append("g").attr("class", "node-labels").selectAll("text")10 unmodified linesconst deg = (d.angle * 180 / Math.PI);return (deg>-90 && deg<90) ? "start" : "end";}).style("pointer-events", "all") // ★ FIX ADDED.text(d => d.abbrev);//--------------------------------------------------------------------// CLICK / HIGHLIGHT logic//--------------------------------------------------------------------function getConnections(nodeName){let buys = BUYS.filter(x=>x[0]===nodeName || x[1]===nodeName).map(x=>x[0]===nodeName?x[1]:x[0]);let sells = SELLS.filter(x=>x[0]===nodeName || x[1]===nodeName).map(x=>x[0]===nodeName?x[1]:x[0]);28 unmodified linessellGroup.selectAll("path").style("opacity", function(){ return isConn(this)?0.98:0.06; });transferGroup.selectAll("path").style("opacity", function(){ return isConn(this)?0.98:0.06; });loopGroup.selectAll("path").style("opacity", 0.9);}function resetOpacity(){4 unmodified linestransferGroup.selectAll("path").style("opacity",0.7);loopGroup.selectAll("path").style("opacity",1);loopPathGroup.selectAll("*").remove();updateLabels(null, new Set());document.getElementById("info-box").innerHTML ="<b>Click a node</b> to view details here.";27 unmodified linesshowInfo(name);const connected = getConnections(name);updateLabels(name, connected);drawLoopPathsFor(name);}//--------------------------------------------------------------------// CLICK HANDLERS — working now//--------------------------------------------------------------------nodeCircleGroup.on("click", function(event, d){selectNode(d);event.stopPropagation();});labelGroup.on("click", function(event, d){selectNode(d);event.stopPropagation();});document.getElementById("arc-reset").onclick = resetOpacity;svg.on("click", function(event){if (event.target === this) resetOpacity();});}draw();//-----------------------------------------------------------// RESIZE DEBOUNCE (ANTI-DOUBLE-CLICK ISSUE)//-----------------------------------------------------------let rz;window.addEventListener("resize", () => {clearTimeout(rz);rz = setTimeout(draw, 120);});</script>"""def make_arc_html(nodes, node_type, buys, sells, transfers, loops):nodes_json = json.dumps(nodes)node_type_json = json.dumps(node_type)
app.py+42-43