Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 | /**
* @module CIA/Visualizations
* @category Intelligence Platform - Visual Analytics Engine
*
* @description
* CIA Dashboard Renderer Module – Intelligence Visualization Engine.
* Primary rendering engine for Swedish parliamentary intelligence operations,
* transforming complex CIA-exported political data into actionable visual
* intelligence. Orchestrates 6+ specialized visualization types designed
* specifically for real-time political risk assessment and coalition
* forecasting analysis.
*
* @author Hack23 AB - Intelligence Analytics
* @license Apache-2.0
* @version 1.0.0
* @since 2024-01-15
*
* @intelligence Intelligence Visualization Engine — primary rendering engine transforming complex CIA-exported political data into actionable visual intelligence. Orchestrates 6+ specialized visualization types for real-time political risk assessment and coalition forecasting analysis.
*
* @business Visual analytics value driver — visualization quality directly determines user perception of platform intelligence depth. Each visualization type is a feature-marketable capability. Custom visualization engine enables white-label offerings for B2G/enterprise clients.
*
* @marketing Visual content factory — every rendered visualization is a potential marketing asset: embeddable widgets, shareable images, presentation slides, and report graphics. High-quality visualizations are the most shared content type, driving organic reach and brand recognition.
* */
import type {
OverviewDashboard,
PartyPerformance,
Top10Influential,
CommitteeNetwork,
VotingPatterns,
MPRanking,
CommitteeEntry,
} from './data-loader.js';
/* ------------------------------------------------------------------ */
/* Global library reference (loaded via <script> tag) */
/* ------------------------------------------------------------------ */
const Chart = (globalThis as any).Chart;
/* ------------------------------------------------------------------ */
/* Interfaces */
/* ------------------------------------------------------------------ */
/** Data bundle consumed by the renderer. */
export interface RendererData {
overview?: OverviewDashboard;
partyPerf?: PartyPerformance;
top10?: Top10Influential;
committees?: CommitteeNetwork;
votingPatterns?: VotingPatterns;
}
/** Chart.js instance reference map. */
interface ChartInstanceMap {
[key: string]: InstanceType<typeof Chart> | undefined;
}
/* ------------------------------------------------------------------ */
/* CIADashboardRenderer class */
/* ------------------------------------------------------------------ */
export class CIADashboardRenderer {
readonly data: RendererData;
private charts: ChartInstanceMap;
constructor(data: RendererData) {
this.data = data;
this.charts = {};
}
/** Render key metrics section. */
renderKeyMetrics(): void {
const { overview } = this.data || {};
if (!overview) {
console.warn('Invalid or missing overview data');
return;
}
// Update metric values with null checks
const totalMpsEl = document.getElementById('metric-total-mps');
if (totalMpsEl && overview.keyMetrics) {
totalMpsEl.textContent = String(overview.keyMetrics.totalMPs);
}
const totalPartiesEl = document.getElementById('metric-total-parties');
if (totalPartiesEl && overview.keyMetrics) {
totalPartiesEl.textContent = String(overview.keyMetrics.totalParties);
}
const riskRulesEl = document.getElementById('metric-risk-rules');
if (riskRulesEl && overview.keyMetrics) {
riskRulesEl.textContent = String(overview.keyMetrics.totalRiskRules);
}
const coalitionSeatsEl = document.getElementById('metric-coalition-seats');
if (coalitionSeatsEl && overview.keyMetrics) {
coalitionSeatsEl.textContent = String(overview.keyMetrics.coalitionSeats);
}
// Update risk alerts with null checks
const hasRiskAlerts = overview.riskAlerts && overview.riskAlerts.last90Days;
const alertCriticalEl = document.getElementById('alert-critical');
if (alertCriticalEl && hasRiskAlerts) {
alertCriticalEl.textContent = String(overview.riskAlerts.last90Days.critical);
}
const alertMajorEl = document.getElementById('alert-major');
if (alertMajorEl && hasRiskAlerts) {
alertMajorEl.textContent = String(overview.riskAlerts.last90Days.major);
}
const alertMinorEl = document.getElementById('alert-minor');
if (alertMinorEl && hasRiskAlerts) {
alertMinorEl.textContent = String(overview.riskAlerts.last90Days.minor);
}
}
/** Render party performance charts. */
renderPartyPerformance(): void {
const { partyPerf } = this.data;
// Defensive check for data structure
if (!partyPerf || !Array.isArray(partyPerf.parties)) {
console.warn('Invalid or missing party performance data');
return;
}
// Party Seats Chart
const seatsCtx = document.getElementById('party-seats-chart') as HTMLCanvasElement | null;
if (seatsCtx && typeof Chart !== 'undefined') {
// Defensive check for nested party properties
const hasValidMetrics = partyPerf.parties.every(
p => p && p.metrics && typeof p.metrics.seats === 'number'
);
if (!hasValidMetrics) {
console.warn('Some parties have invalid or missing metrics data');
}
this.charts.seats = new Chart(seatsCtx, {
type: 'bar',
data: {
labels: partyPerf.parties.map(p => p.shortName || 'Unknown'),
datasets: [
{
label: 'Current Seats',
data: partyPerf.parties.map(p =>
p && p.metrics && typeof p.metrics.seats === 'number' ? p.metrics.seats : 0
),
backgroundColor: [
'rgba(224, 32, 32, 0.8)',
'rgba(221, 171, 0, 0.8)',
'rgba(82, 126, 196, 0.8)',
'rgba(175, 8, 42, 0.8)',
'rgba(0, 150, 65, 0.8)',
'rgba(0, 90, 170, 0.8)',
'rgba(83, 160, 60, 0.8)',
'rgba(0, 106, 179, 0.8)',
],
borderColor: [
'rgb(224, 32, 32)',
'rgb(221, 171, 0)',
'rgb(82, 126, 196)',
'rgb(175, 8, 42)',
'rgb(0, 150, 65)',
'rgb(0, 90, 170)',
'rgb(83, 160, 60)',
'rgb(0, 106, 179)',
],
borderWidth: 2,
},
],
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
title: {
display: true,
text: 'Current Riksdag Seats by Party',
font: { size: 16, weight: 'bold' },
},
legend: { display: false },
},
scales: {
y: {
beginAtZero: true,
max: 120,
title: { display: true, text: 'Number of Seats' },
},
},
},
});
}
// Party Cohesion Chart
const cohesionCtx = document.getElementById('party-cohesion-chart') as HTMLCanvasElement | null;
if (cohesionCtx && typeof Chart !== 'undefined') {
const hasValidVoting = partyPerf.parties.every(
p =>
p &&
p.voting &&
typeof p.voting.cohesionScore === 'number' &&
typeof p.voting.rebellionRate === 'number'
);
if (!hasValidVoting) {
console.warn('Some parties have invalid or missing voting data');
}
this.charts.cohesion = new Chart(cohesionCtx, {
type: 'line',
data: {
labels: partyPerf.parties.map(p => p.shortName || 'Unknown'),
datasets: [
{
label: 'Voting Cohesion (%)',
data: partyPerf.parties.map(p =>
p && p.voting && typeof p.voting.cohesionScore === 'number'
? p.voting.cohesionScore
: 0
),
borderColor: 'rgb(0, 102, 51)',
backgroundColor: 'rgba(0, 102, 51, 0.1)',
tension: 0.4,
fill: true,
pointRadius: 5,
pointHoverRadius: 7,
},
{
label: 'Rebellion Rate (%)',
data: partyPerf.parties.map(p =>
p && p.voting && typeof p.voting.rebellionRate === 'number'
? p.voting.rebellionRate
: 0
),
borderColor: 'rgb(220, 53, 69)',
backgroundColor: 'rgba(220, 53, 69, 0.1)',
tension: 0.4,
fill: true,
pointRadius: 5,
pointHoverRadius: 7,
},
],
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
title: {
display: true,
text: 'Party Voting Cohesion vs Rebellion Rate',
font: { size: 16, weight: 'bold' },
},
},
scales: {
y: {
beginAtZero: true,
max: 100,
title: { display: true, text: 'Percentage (%)' },
},
},
},
});
}
}
/** Render Top 10 rankings. */
renderTop10Rankings(): void {
const { top10 } = this.data;
const container = document.getElementById('influential-mps');
if (!container) return;
if (!top10 || !Array.isArray(top10.rankings)) {
console.warn('Invalid or missing top 10 rankings data');
return;
}
container.textContent = '';
const fragment = document.createDocumentFragment();
top10.rankings.forEach((mp: MPRanking) => {
const item = document.createElement('div');
item.className = 'ranking-item';
const number = document.createElement('div');
number.className = 'ranking-number';
number.textContent = String(mp.rank);
const info = document.createElement('div');
info.className = 'ranking-info';
const name = document.createElement('div');
name.className = 'ranking-name';
name.textContent = `${mp.firstName} ${mp.lastName}`;
const party = document.createElement('div');
party.className = 'ranking-party';
party.textContent = mp.party;
const role = document.createElement('div');
role.className = 'ranking-role';
role.textContent = mp.role;
info.appendChild(name);
info.appendChild(party);
info.appendChild(role);
const score = document.createElement('div');
score.className = 'ranking-score';
const scoreValue = document.createElement('div');
scoreValue.className = 'score-value';
const influenceScore =
mp && typeof mp.influenceScore === 'number' && Number.isFinite(mp.influenceScore)
? mp.influenceScore
: null;
scoreValue.textContent = influenceScore !== null ? influenceScore.toFixed(1) : 'N/A';
const scoreLabel = document.createElement('div');
scoreLabel.className = 'score-label';
scoreLabel.textContent = 'Influence';
score.appendChild(scoreValue);
score.appendChild(scoreLabel);
item.appendChild(number);
item.appendChild(info);
item.appendChild(score);
fragment.appendChild(item);
});
container.appendChild(fragment);
}
/** Render voting patterns heatmap. */
renderVotingPatterns(): void {
const { votingPatterns } = this.data;
const ctx = document.getElementById('voting-heatmap') as HTMLCanvasElement | null;
if (!ctx || typeof Chart === 'undefined') return;
if (
!votingPatterns ||
!votingPatterns.votingMatrix ||
!votingPatterns.votingMatrix.labels ||
!votingPatterns.votingMatrix.partyNames ||
!Array.isArray(votingPatterns.votingMatrix.agreementMatrix)
) {
console.warn('Invalid or missing voting patterns data');
return;
}
const matrix = votingPatterns.votingMatrix;
this.charts.heatmap = new Chart(ctx, {
type: 'bar',
data: {
labels: matrix.labels,
datasets: matrix.agreementMatrix.map((row: number[], i: number) => ({
label: matrix.partyNames[i],
data: row,
backgroundColor: `hsla(${i * 45}, 70%, 50%, 0.6)`,
stack: 'Stack ' + i,
})),
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
title: {
display: true,
text: 'Party Agreement Matrix (%)',
font: { size: 16, weight: 'bold' },
},
legend: { display: true, position: 'right' },
},
scales: {
x: { title: { display: true, text: 'Parties' } },
y: {
beginAtZero: true,
max: 100,
title: { display: true, text: 'Agreement %' },
},
},
},
});
}
/** Render committee network. */
renderCommitteeNetwork(): void {
const { committees } = this.data;
const container = document.getElementById('committee-list');
if (!container) return;
if (!committees || !Array.isArray(committees.committees)) {
console.warn('Invalid or missing committee network data');
return;
}
container.textContent = '';
const fragment = document.createDocumentFragment();
committees.committees.forEach((committee: CommitteeEntry) => {
const card = document.createElement('div');
card.className = 'committee-card';
const nameEl = document.createElement('h3');
nameEl.className = 'committee-name';
nameEl.textContent = committee.name;
const stats = document.createElement('div');
stats.className = 'committee-stats';
const createStat = (label: string, value: string | number): HTMLDivElement => {
const stat = document.createElement('div');
stat.className = 'committee-stat';
const statLabel = document.createElement('span');
statLabel.className = 'stat-label';
statLabel.textContent = label + ':';
const statValue = document.createElement('span');
statValue.className = 'stat-value';
statValue.textContent = String(value);
stat.appendChild(statLabel);
stat.appendChild(statValue);
return stat;
};
const memberCount =
typeof committee.memberCount === 'number' ? committee.memberCount : 'N/A';
const influenceScore =
typeof committee.influenceScore === 'number' && Number.isFinite(committee.influenceScore)
? committee.influenceScore.toFixed(1)
: 'N/A';
const meetingsPerYear =
typeof committee.meetingsPerYear === 'number' ? committee.meetingsPerYear : 'N/A';
const documentsProcessed =
typeof committee.documentsProcessed === 'number' ? committee.documentsProcessed : 'N/A';
stats.appendChild(createStat('Members', memberCount));
stats.appendChild(createStat('Influence', influenceScore));
stats.appendChild(createStat('Meetings/Year', meetingsPerYear));
stats.appendChild(createStat('Documents', documentsProcessed));
const issues = document.createElement('div');
issues.className = 'committee-issues';
const issuesHeading = document.createElement('h4');
issuesHeading.textContent = 'Key Issues';
issues.appendChild(issuesHeading);
if (Array.isArray(committee.keyIssues)) {
committee.keyIssues.forEach((issue: string) => {
const tag = document.createElement('span');
tag.className = 'issue-tag';
tag.textContent = issue;
issues.appendChild(tag);
});
}
card.appendChild(nameEl);
card.appendChild(stats);
card.appendChild(issues);
fragment.appendChild(card);
});
container.appendChild(fragment);
// Add simple network visualization note
const networkViz = document.getElementById('network-visualization');
if (networkViz) {
networkViz.textContent = '';
const vizDiv = document.createElement('div');
const p1 = document.createElement('p');
const strong = document.createElement('strong');
strong.textContent = 'Network Graph:';
p1.appendChild(strong);
p1.appendChild(
document.createTextNode(
' Interactive committee network visualization would be rendered here using D3.js or similar library.'
)
);
const p2 = document.createElement('p');
p2.textContent = `Current data shows ${committees.networkGraph.nodes.length} committees with ${committees.networkGraph.edges.length} interconnections.`;
vizDiv.appendChild(p1);
vizDiv.appendChild(p2);
networkViz.appendChild(vizDiv);
}
}
/** Destroy all charts (for cleanup). */
destroy(): void {
Object.values(this.charts).forEach(chart => {
if (chart && typeof chart.destroy === 'function') {
chart.destroy();
}
});
this.charts = {};
}
}
|