feat: attribute profile panel — click a node to see its correlation bar chart
Browse files- build_network.py +98 -3
build_network.py
CHANGED
|
@@ -211,6 +211,7 @@ def generate_network_html(network_data, threshold, max_l1_nodes,
|
|
| 211 |
all_nodes=all_nodes,
|
| 212 |
all_edges=all_edges,
|
| 213 |
driver_list=driver_list,
|
|
|
|
| 214 |
threshold=threshold,
|
| 215 |
attribute_count=len(attribute_names),
|
| 216 |
reward_drivers=reward_drivers,
|
|
@@ -235,7 +236,8 @@ def generate_network_html(network_data, threshold, max_l1_nodes,
|
|
| 235 |
|
| 236 |
def _build_html_template(*, chart_title, client_name, country_name, brand_name,
|
| 237 |
category_name, all_nodes, all_edges, driver_list,
|
| 238 |
-
threshold, attribute_count,
|
|
|
|
| 239 |
corr_count, avg_corr, max_corr):
|
| 240 |
"""Build the standalone HTML string. Separated for readability."""
|
| 241 |
|
|
@@ -324,12 +326,37 @@ body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans
|
|
| 324 |
.detail-card h4 {{ font-size: 12px; font-weight: 600; color: #888; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 6px; }}
|
| 325 |
.detail-card p {{ font-size: 13px; line-height: 1.5; color: #444; }}
|
| 326 |
.detail-card .val {{ font-size: 20px; font-weight: 600; color: #1a1a1a; }}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
</style>
|
| 328 |
<script>
|
| 329 |
var allNetworkNodes = {json.dumps(all_nodes)};
|
| 330 |
var allNetworkEdges = {json.dumps(all_edges)};
|
| 331 |
var driverList = {json.dumps(driver_list)};
|
|
|
|
| 332 |
var network = null;
|
|
|
|
|
|
|
| 333 |
var currentSort = 'index'; // 'index' or 'name'
|
| 334 |
var selectedIdx = 0;
|
| 335 |
|
|
@@ -362,8 +389,8 @@ function draw(id) {{
|
|
| 362 |
interaction: {{ zoomView: true, dragView: true, hover: true, tooltipDelay: 200, selectConnectedEdges: true, hoverConnectedEdges: true, hideEdgesOnDrag: false }},
|
| 363 |
layout: {{ hierarchical: false }}
|
| 364 |
}};
|
| 365 |
-
|
| 366 |
-
|
| 367 |
var netData = {{ nodes: nodesDS, edges: edgesDS }};
|
| 368 |
network = new vis.Network(document.getElementById('mynetwork'), netData, options);
|
| 369 |
document.getElementById('nodeCount').textContent = data.nodes.length;
|
|
@@ -420,7 +447,61 @@ function draw(id) {{
|
|
| 420 |
edgesDS.update(eUp);
|
| 421 |
}});
|
| 422 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 423 |
highlightSidebarRow(id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 424 |
}}
|
| 425 |
|
| 426 |
function highlightSidebarRow(id) {{
|
|
@@ -524,6 +605,20 @@ document.addEventListener('DOMContentLoaded', function() {{
|
|
| 524 |
<span class="stat">Connections: <strong id="edgeCount">0</strong></span>
|
| 525 |
</div>
|
| 526 |
<div id="mynetwork"></div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 527 |
<div class="tabs">
|
| 528 |
<button class="tab-btn" id="detailsBtn" onclick="toggleDetails()">Details</button>
|
| 529 |
</div>
|
|
|
|
| 211 |
all_nodes=all_nodes,
|
| 212 |
all_edges=all_edges,
|
| 213 |
driver_list=driver_list,
|
| 214 |
+
nonrewards_list=sorted(nonrewards_attrs),
|
| 215 |
threshold=threshold,
|
| 216 |
attribute_count=len(attribute_names),
|
| 217 |
reward_drivers=reward_drivers,
|
|
|
|
| 236 |
|
| 237 |
def _build_html_template(*, chart_title, client_name, country_name, brand_name,
|
| 238 |
category_name, all_nodes, all_edges, driver_list,
|
| 239 |
+
nonrewards_list, threshold, attribute_count,
|
| 240 |
+
reward_drivers, nonreward_drivers,
|
| 241 |
corr_count, avg_corr, max_corr):
|
| 242 |
"""Build the standalone HTML string. Separated for readability."""
|
| 243 |
|
|
|
|
| 326 |
.detail-card h4 {{ font-size: 12px; font-weight: 600; color: #888; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 6px; }}
|
| 327 |
.detail-card p {{ font-size: 13px; line-height: 1.5; color: #444; }}
|
| 328 |
.detail-card .val {{ font-size: 20px; font-weight: 600; color: #1a1a1a; }}
|
| 329 |
+
|
| 330 |
+
/* --- Profile panel (right) --- */
|
| 331 |
+
.profile-panel {{ position: absolute; top: 0; right: 0; bottom: 0; width: 320px; background: #fff; border-left: 1px solid #e0e0e0; box-shadow: -4px 0 20px rgba(0,0,0,0.06); transform: translateX(100%); transition: transform 0.25s ease; z-index: 20; display: flex; flex-direction: column; }}
|
| 332 |
+
.profile-panel.open {{ transform: translateX(0); }}
|
| 333 |
+
.profile-header {{ display: flex; align-items: flex-start; justify-content: space-between; padding: 14px 16px 10px; border-bottom: 1px solid #eee; flex-shrink: 0; }}
|
| 334 |
+
.profile-header-info h3 {{ font-size: 15px; font-weight: 600; color: #1a1a1a; line-height: 1.3; }}
|
| 335 |
+
.profile-meta {{ display: flex; gap: 8px; margin-top: 4px; align-items: center; }}
|
| 336 |
+
.profile-badge {{ font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 3px; }}
|
| 337 |
+
.profile-badge.reward {{ background: {COLORS['reward_bg']}; color: {COLORS['reward_border']}; border: 1px solid {COLORS['reward_border']}; }}
|
| 338 |
+
.profile-badge.nonreward {{ background: {COLORS['nonreward_bg']}; color: {COLORS['nonreward_border']}; border: 1px solid {COLORS['nonreward_border']}; }}
|
| 339 |
+
.profile-cat-idx {{ font-size: 11px; color: #888; }}
|
| 340 |
+
.profile-close {{ background: none; border: none; font-size: 18px; cursor: pointer; color: #888; padding: 0 4px; border-radius: 4px; flex-shrink: 0; }}
|
| 341 |
+
.profile-close:hover {{ background: #f0f0f0; }}
|
| 342 |
+
.profile-subtitle {{ padding: 8px 16px 4px; font-size: 11px; font-weight: 600; color: #999; text-transform: uppercase; letter-spacing: 0.5px; flex-shrink: 0; }}
|
| 343 |
+
.profile-bars {{ flex: 1; overflow-y: auto; padding: 4px 16px 16px; }}
|
| 344 |
+
.bar-row {{ display: flex; align-items: center; gap: 8px; padding: 5px 0; border-bottom: 1px solid #f5f5f5; }}
|
| 345 |
+
.bar-label {{ width: 110px; font-size: 12px; font-weight: 500; color: #444; flex-shrink: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }}
|
| 346 |
+
.bar-track {{ flex: 1; height: 16px; background: #f5f5f5; border-radius: 3px; position: relative; overflow: hidden; }}
|
| 347 |
+
.bar-fill {{ height: 100%; border-radius: 3px; transition: width 0.3s ease; }}
|
| 348 |
+
.bar-fill.reward {{ background: {COLORS['reward_color']}; opacity: 0.7; }}
|
| 349 |
+
.bar-fill.nonreward {{ background: {COLORS['nonreward_color']}; opacity: 0.7; }}
|
| 350 |
+
.bar-val {{ font-size: 11px; font-weight: 600; color: #888; min-width: 30px; text-align: right; flex-shrink: 0; }}
|
| 351 |
</style>
|
| 352 |
<script>
|
| 353 |
var allNetworkNodes = {json.dumps(all_nodes)};
|
| 354 |
var allNetworkEdges = {json.dumps(all_edges)};
|
| 355 |
var driverList = {json.dumps(driver_list)};
|
| 356 |
+
var nonrewardsSet = new Set({json.dumps(nonrewards_list)});
|
| 357 |
var network = null;
|
| 358 |
+
var nodesDS = null;
|
| 359 |
+
var edgesDS = null;
|
| 360 |
var currentSort = 'index'; // 'index' or 'name'
|
| 361 |
var selectedIdx = 0;
|
| 362 |
|
|
|
|
| 389 |
interaction: {{ zoomView: true, dragView: true, hover: true, tooltipDelay: 200, selectConnectedEdges: true, hoverConnectedEdges: true, hideEdgesOnDrag: false }},
|
| 390 |
layout: {{ hierarchical: false }}
|
| 391 |
}};
|
| 392 |
+
nodesDS = new vis.DataSet(data.nodes);
|
| 393 |
+
edgesDS = new vis.DataSet(data.edges);
|
| 394 |
var netData = {{ nodes: nodesDS, edges: edgesDS }};
|
| 395 |
network = new vis.Network(document.getElementById('mynetwork'), netData, options);
|
| 396 |
document.getElementById('nodeCount').textContent = data.nodes.length;
|
|
|
|
| 447 |
edgesDS.update(eUp);
|
| 448 |
}});
|
| 449 |
|
| 450 |
+
network.on('click', function(params) {{
|
| 451 |
+
if (params.nodes.length > 0) {{
|
| 452 |
+
showProfile(params.nodes[0]);
|
| 453 |
+
}} else {{
|
| 454 |
+
closeProfile();
|
| 455 |
+
}}
|
| 456 |
+
}});
|
| 457 |
+
|
| 458 |
highlightSidebarRow(id);
|
| 459 |
+
closeProfile();
|
| 460 |
+
}}
|
| 461 |
+
|
| 462 |
+
function showProfile(nodeId) {{
|
| 463 |
+
var panel = document.getElementById('profilePanel');
|
| 464 |
+
var node = nodesDS.get(nodeId);
|
| 465 |
+
if (!node) return;
|
| 466 |
+
|
| 467 |
+
var connEdges = network.getConnectedEdges(nodeId);
|
| 468 |
+
var bars = [];
|
| 469 |
+
connEdges.forEach(function(eid) {{
|
| 470 |
+
var e = edgesDS.get(eid);
|
| 471 |
+
if (!e) return;
|
| 472 |
+
var other = e.from === nodeId ? e.to : e.from;
|
| 473 |
+
bars.push({{ name: other, value: e.value }});
|
| 474 |
+
}});
|
| 475 |
+
bars.sort(function(a, b) {{ return b.value - a.value; }});
|
| 476 |
+
|
| 477 |
+
var maxVal = bars.length > 0 ? bars[0].value : 1;
|
| 478 |
+
var isNR = nonrewardsSet.has(nodeId);
|
| 479 |
+
|
| 480 |
+
// Find category index from driverList or node value
|
| 481 |
+
var catIdx = node.value || 0;
|
| 482 |
+
|
| 483 |
+
document.getElementById('profileName').textContent = nodeId;
|
| 484 |
+
var badge = document.getElementById('profileBadge');
|
| 485 |
+
badge.textContent = isNR ? 'Functional' : 'Emotional';
|
| 486 |
+
badge.className = 'profile-badge ' + (isNR ? 'nonreward' : 'reward');
|
| 487 |
+
document.getElementById('profileCatIdx').textContent = 'Category Index: ' + catIdx;
|
| 488 |
+
|
| 489 |
+
var html = '';
|
| 490 |
+
bars.forEach(function(b) {{
|
| 491 |
+
var pct = Math.round((b.value / maxVal) * 100);
|
| 492 |
+
var cls = nonrewardsSet.has(b.name) ? 'nonreward' : 'reward';
|
| 493 |
+
html += '<div class="bar-row">'
|
| 494 |
+
+ '<span class="bar-label" title="' + b.name + '">' + b.name + '</span>'
|
| 495 |
+
+ '<div class="bar-track"><div class="bar-fill ' + cls + '" style="width:' + pct + '%"></div></div>'
|
| 496 |
+
+ '<span class="bar-val">' + b.value.toFixed(1) + '</span>'
|
| 497 |
+
+ '</div>';
|
| 498 |
+
}});
|
| 499 |
+
document.getElementById('profileBars').innerHTML = html;
|
| 500 |
+
panel.classList.add('open');
|
| 501 |
+
}}
|
| 502 |
+
|
| 503 |
+
function closeProfile() {{
|
| 504 |
+
document.getElementById('profilePanel').classList.remove('open');
|
| 505 |
}}
|
| 506 |
|
| 507 |
function highlightSidebarRow(id) {{
|
|
|
|
| 605 |
<span class="stat">Connections: <strong id="edgeCount">0</strong></span>
|
| 606 |
</div>
|
| 607 |
<div id="mynetwork"></div>
|
| 608 |
+
<div class="profile-panel" id="profilePanel">
|
| 609 |
+
<div class="profile-header">
|
| 610 |
+
<div class="profile-header-info">
|
| 611 |
+
<h3 id="profileName"></h3>
|
| 612 |
+
<div class="profile-meta">
|
| 613 |
+
<span class="profile-badge" id="profileBadge"></span>
|
| 614 |
+
<span class="profile-cat-idx" id="profileCatIdx"></span>
|
| 615 |
+
</div>
|
| 616 |
+
</div>
|
| 617 |
+
<button class="profile-close" onclick="closeProfile()">×</button>
|
| 618 |
+
</div>
|
| 619 |
+
<div class="profile-subtitle">Connections in network</div>
|
| 620 |
+
<div class="profile-bars" id="profileBars"></div>
|
| 621 |
+
</div>
|
| 622 |
<div class="tabs">
|
| 623 |
<button class="tab-btn" id="detailsBtn" onclick="toggleDetails()">Details</button>
|
| 624 |
</div>
|