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 | 1x 1x 1x 1x 1x 1x 1x 20x 20x 21x 21x 17x 17x 21x 21x 37x 17x 17x 36x 36x 34x 34x 34x 36x 36x 36x 36x 34x 17x 17x 17x 36x 17x 17x 36x 39x 36x 36x 36x 36x 17x 21x 21x 21x 20x 21x 21x 22x 21x 21x 21x 21x 21x 21x 21x 21x 21x 1x 1x 1x 17x 17x 23x 48x 21x 17x 20x 3x 17x 17x 20x 20x 20x 20x 20x | // SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
/**
* Generates a Sankey (flow) chart as inline SVG — no JavaScript or
* third-party libraries required.
*
* A Sankey diagram visualises quantities flowing from source nodes to target
* nodes. Node heights and flow-path widths are proportional to the values,
* making it easy to see dominant flows at a glance.
*
* Typical usage in articles:
* - Parliamentary flow: political group → document type → legislative outcome
* - Policy flow: policy domain → committee → outcome (adopted/rejected/pending)
* - Budget flow: programme area → committee → spending category
*
* @module generators/sankey-content
*/
import { escapeHTML } from '../utils/file-utils.js';
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
/** Pre-defined semantic color categories for Sankey nodes. */
export type SankeyNodeColor =
| 'cyan'
| 'magenta'
| 'yellow'
| 'green'
| 'purple'
| 'orange'
| 'blue'
| 'red';
/** A node in the Sankey diagram. */
export interface SankeyNode {
/** Unique identifier (used in flows to reference source/target). */
readonly id: string;
/** Display label. */
readonly label: string;
/** Semantic color. */
readonly color: SankeyNodeColor;
}
/** A directed flow between two nodes. */
export interface SankeyFlow {
/** Source node id. */
readonly source: string;
/** Target node id. */
readonly target: string;
/** Flow magnitude (relative — values are scaled to fit the SVG height). */
readonly value: number;
/** Optional label shown on the flow path. */
readonly label?: string | undefined;
}
/** Full Sankey diagram configuration. */
export interface SankeyConfig {
/** Array of nodes (must have at least two: one source, one target). */
readonly nodes: readonly SankeyNode[];
/** Array of directed flows between nodes. */
readonly flows: readonly SankeyFlow[];
/** Optional narrative summary. */
readonly summary?: string | undefined;
}
// ---------------------------------------------------------------------------
// Color palette
// ---------------------------------------------------------------------------
const NODE_COLORS: Readonly<
Record<SankeyNodeColor, { fill: string; stroke: string; text: string }>
> = {
cyan: { fill: '#e3f2fd', stroke: '#1565c0', text: '#1565c0' },
magenta: { fill: '#fce4ec', stroke: '#c62828', text: '#c62828' },
yellow: { fill: '#fff8e1', stroke: '#f57f17', text: '#f57f17' },
green: { fill: '#e8f5e9', stroke: '#2e7d32', text: '#2e7d32' },
purple: { fill: '#f3e5f5', stroke: '#7b1fa2', text: '#7b1fa2' },
orange: { fill: '#fff3e0', stroke: '#e65100', text: '#e65100' },
blue: { fill: '#e8eaf6', stroke: '#283593', text: '#283593' },
red: { fill: '#ffebee', stroke: '#b71c1c', text: '#b71c1c' },
};
// ---------------------------------------------------------------------------
// Section heading labels (14 languages)
// ---------------------------------------------------------------------------
const SANKEY_HEADINGS: Readonly<Record<string, string>> = {
en: 'Policy Flow',
sv: 'Politikflöde',
da: 'Politikflow',
no: 'Politikkflyt',
fi: 'Politiikkavirta',
de: 'Politikfluss',
fr: 'Flux législatif',
es: 'Flujo legislativo',
nl: 'Beleidsflow',
ar: 'تدفق السياسات',
he: 'זרימת מדיניות',
ja: '政策フロー',
ko: '정책 흐름',
zh: '政策流向图',
};
// ---------------------------------------------------------------------------
// SVG layout engine
// ---------------------------------------------------------------------------
const SVG_WIDTH = 600;
const NODE_WIDTH = 22;
const NODE_GAP = 14;
const COL_LEFT = 20;
const COL_RIGHT = SVG_WIDTH - NODE_WIDTH - 20;
interface LayoutNode {
id: string;
label: string;
color: SankeyNodeColor;
totalValue: number;
y: number;
height: number;
srcOffset: number;
tgtOffset: number;
}
/**
* Build a cubic bezier SVG path between two columns.
*
* @param x1 - Start x coordinate.
* @param y1 - Start y coordinate.
* @param x2 - End x coordinate.
* @param y2 - End y coordinate.
* @param h - Height of the flow path.
* @returns SVG path data string.
*/
function buildBezierPath(x1: number, y1: number, x2: number, y2: number, h: number): string {
const mx = (x1 + x2) / 2;
return [
`M${x1},${y1}`,
`C${mx},${y1} ${mx},${y2} ${x2},${y2}`,
`L${x2},${y2 + h}`,
`C${mx},${y2 + h} ${mx},${y1 + h} ${x1},${y1 + h}`,
'Z',
].join(' ');
}
/**
* Compute SVG element strings for the Sankey diagram.
*
* @param nodes - Array of Sankey nodes.
* @param flows - Array of directed flows between nodes.
* @param svgHeight - Height of the SVG canvas in pixels.
* @returns Array of SVG element strings.
*/
function layoutSankey(
nodes: readonly SankeyNode[],
flows: readonly SankeyFlow[],
svgHeight: number
): string[] {
const sourceIds = new Set(flows.map((f) => f.source));
const targetIds = new Set(flows.map((f) => f.target));
const valuePer: Record<string, number> = {};
for (const f of flows) {
valuePer[f.source] = (valuePer[f.source] ?? 0) + f.value;
valuePer[f.target] = (valuePer[f.target] ?? 0) + f.value;
}
const totalValue = Object.values(valuePer).reduce((a, b) => a + b, 0) || 1;
const usableHeight = svgHeight - 40;
const scaleFactor = usableHeight / (totalValue / 2 + (nodes.length - 1) * NODE_GAP);
const leftNodes = nodes.filter((n) => sourceIds.has(n.id) || !targetIds.has(n.id));
const rightNodes = nodes.filter((n) => targetIds.has(n.id) && !sourceIds.has(n.id));
function placeNodes(nds: readonly SankeyNode[]): LayoutNode[] {
const result: LayoutNode[] = [];
let yOffset = 20;
for (const n of nds) {
const total = valuePer[n.id] ?? 0;
const height = Math.max(18, Math.round(total * scaleFactor));
result.push({
id: n.id,
label: n.label,
color: n.color,
totalValue: total,
y: yOffset,
height,
srcOffset: 0,
tgtOffset: 0,
});
yOffset += height + NODE_GAP;
}
return result;
}
const leftLayout = placeNodes(leftNodes);
const rightLayout = placeNodes(rightNodes);
const allLayout = [...leftLayout, ...rightLayout];
const layoutMap = new Map<string, LayoutNode>(allLayout.map((n) => [n.id, n]));
const svgElements: string[] = [];
for (const ln of allLayout) {
const palette = NODE_COLORS[ln.color] ?? NODE_COLORS.cyan;
const isLeft = leftLayout.some((l) => l.id === ln.id);
const xPos = isLeft ? COL_LEFT : COL_RIGHT;
const labelX = isLeft ? xPos + NODE_WIDTH + 6 : xPos - 6;
const textAnchor = isLeft ? 'start' : 'end';
svgElements.push(
`<rect x="${xPos}" y="${ln.y}" width="${NODE_WIDTH}" height="${ln.height}" fill="${palette.fill}" stroke="${palette.stroke}" stroke-width="2" rx="3"/>`,
`<text x="${labelX}" y="${ln.y + ln.height / 2}" text-anchor="${textAnchor}" dominant-baseline="middle" font-size="11" fill="${palette.text}" font-family="sans-serif">${escapeHTML(ln.label)}</text>`
);
}
for (const flow of flows) {
const srcNode = layoutMap.get(flow.source);
const tgtNode = layoutMap.get(flow.target);
if (!srcNode || !tgtNode) continue;
const srcPalette = NODE_COLORS[srcNode.color] ?? NODE_COLORS.cyan;
const tgtVal = valuePer[tgtNode.id] ?? 1;
const scaledH = Math.max(
4,
Math.round(
((flow.value / Math.max(tgtVal, valuePer[srcNode.id] ?? 1)) *
(srcNode.height + tgtNode.height)) /
2
)
);
const srcIsLeft = leftLayout.some((l) => l.id === srcNode.id);
const x1 = srcIsLeft ? COL_LEFT + NODE_WIDTH : COL_RIGHT;
const x2 = COL_RIGHT;
const y1 = srcNode.y + srcNode.srcOffset;
const y2 = tgtNode.y + tgtNode.tgtOffset;
srcNode.srcOffset += scaledH + 2;
tgtNode.tgtOffset += scaledH + 2;
const pathD = buildBezierPath(x1, y1, x2, y2, scaledH);
svgElements.push(
`<path d="${pathD}" fill="${srcPalette.stroke}33" stroke="${srcPalette.stroke}" stroke-width="0.5" opacity="0.7"/>`
);
if (flow.label) {
const midX = SVG_WIDTH / 2;
const midY = (y1 + y2) / 2 + scaledH / 2;
svgElements.push(
`<text x="${midX}" y="${midY}" text-anchor="middle" dominant-baseline="middle" font-size="9" fill="#495057" font-family="sans-serif">${escapeHTML(flow.label)}</text>`
);
}
}
return svgElements;
}
// ---------------------------------------------------------------------------
// Accessible table fallback
// ---------------------------------------------------------------------------
function renderFallbackTable(nodes: readonly SankeyNode[], flows: readonly SankeyFlow[]): string {
const rows = flows
.map((f) => {
const srcNode = nodes.find((n) => n.id === f.source);
const tgtNode = nodes.find((n) => n.id === f.target);
return ` <tr>
<td>${escapeHTML(srcNode?.label ?? f.source)}</td>
<td>${escapeHTML(tgtNode?.label ?? f.target)}</td>
<td>${f.value}</td>
${f.label ? `<td>${escapeHTML(f.label)}</td>` : '<td></td>'}
</tr>`;
})
.join('\n');
return `<noscript>
<table class="sankey-fallback-table" aria-label="Sankey data">
<caption>Flow data</caption>
<thead><tr><th>Source</th><th>Target</th><th>Value</th><th>Note</th></tr></thead>
<tbody>
${rows}
</tbody>
</table>
</noscript>`;
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
* Generate a Sankey flow chart section as an HTML string with inline SVG.
*
* Returns an empty string when `config` is null/undefined or has no flows,
* following the same convention as `buildSwotSection` and
* `buildDashboardSection`.
*
* @param config - Sankey data (nodes, flows, optional summary).
* @param lang - BCP 47 language code for the section heading.
* @param heading - Optional heading override.
* @returns HTML string for the sankey section, or empty string.
*/
export function buildSankeySection(
config: SankeyConfig | null | undefined,
lang: string = 'en',
heading?: string
): string {
if (!config || !config.flows || config.flows.length === 0) {
return '';
}
const svgHeight = 340;
const titleText: string = heading?.trim() || SANKEY_HEADINGS[lang] || 'Policy Flow';
const summaryBlock = config.summary?.trim()
? ` <p class="sankey-summary">${escapeHTML(config.summary.trim())}</p>\n`
: '';
const svgElements = layoutSankey(config.nodes, config.flows, svgHeight);
const svgContent = svgElements.join('\n ');
const fallbackTable = renderFallbackTable(config.nodes, config.flows);
return `<section class="sankey-section" role="region" aria-label="${escapeHTML(titleText)}">
<h2>${escapeHTML(titleText)}</h2>
${summaryBlock} <div class="sankey-chart-wrapper">
<svg viewBox="0 0 ${SVG_WIDTH} ${svgHeight}" xmlns="http://www.w3.org/2000/svg"
role="img" aria-label="${escapeHTML(titleText)}"
style="width:100%;height:auto;max-width:${SVG_WIDTH}px;display:block;">
<title>${escapeHTML(titleText)}</title>
<rect width="${SVG_WIDTH}" height="${svgHeight}" fill="var(--surface, #f8f9fa)" rx="8"/>
${svgContent}
</svg>
</div>
${fallbackTable}
</section>`;
}
|