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 | /**
* @module Shared/ChartFactory
* @description Centralized Chart.js creation and configuration.
* Replaces 51+ independent `new Chart()` calls with consistent theming,
* responsive behavior, and accessibility features.
*
* @security No inline scripts — all Chart.js configuration is programmatic
*
* @intelligence Standardized intelligence visualization factory — ensures all analytical charts (risk heat maps, coalition networks, electoral forecasts) maintain consistent OSINT presentation standards with accessibility compliance for briefing-quality output.
*
* @business Development velocity multiplier — centralizing Chart.js configuration eliminates per-dashboard setup cost (reduced from 51+ `new Chart()` calls). Enables rapid prototyping of new intelligence products with predictable quality and performance.
*
* @marketing Visual quality assurance — every chart produced is screenshot-ready for social media, press releases, and reports. Consistent styling builds brand recognition. Responsive behavior ensures mobile-quality content for all distribution channels.
* */
import type { Chart as ChartType, ChartConfiguration, ChartTypeRegistry } from 'chart.js';
import { THEME_COLORS, CHART_PALETTE, BREAKPOINTS } from './theme.js';
import { showLoadingState, showErrorState, hideStateOverlays } from './dom-utils.js';
import { logger } from './logger.js';
// Re-export for convenience
export { THEME_COLORS, CHART_PALETTE, BREAKPOINTS };
/**
* Get the Chart constructor.
* Works with both global `Chart` (from script tag) and ES module import.
*/
function getChart(): typeof ChartType {
const g = globalThis as Record<string, unknown>;
if (g.Chart) return g.Chart as typeof ChartType;
throw new Error('Chart.js not loaded');
}
/**
* Get responsive chart options based on current viewport.
*/
export function getResponsiveOptions(): Record<string, unknown> {
const width = window.innerWidth;
const isMobile = width < BREAKPOINTS.tablet;
const isTablet = width >= BREAKPOINTS.tablet && width < BREAKPOINTS.desktop;
return {
responsive: true,
maintainAspectRatio: false,
animation: { duration: isMobile ? 0 : 400 },
plugins: {
legend: {
position: isMobile ? 'bottom' as const : 'top' as const,
labels: {
boxWidth: isMobile ? 8 : 12,
padding: isMobile ? 4 : 8,
font: { size: isMobile ? 10 : (isTablet ? 11 : 12) },
color: '#e0e0e0',
},
},
tooltip: {
backgroundColor: 'rgba(10, 14, 39, 0.95)',
titleColor: THEME_COLORS.cyan,
bodyColor: '#e0e0e0',
borderColor: THEME_COLORS.cyan,
borderWidth: 1,
padding: isMobile ? 6 : 10,
titleFont: { size: isMobile ? 11 : 13, weight: 'bold' as const },
bodyFont: { size: isMobile ? 10 : 12 },
cornerRadius: 4,
},
},
scales: {
x: {
ticks: {
color: '#a0a0a0',
font: { size: isMobile ? 9 : 11 },
maxRotation: isMobile ? 45 : 0,
},
grid: { color: 'rgba(255,255,255,0.05)' },
},
y: {
ticks: {
color: '#a0a0a0',
font: { size: isMobile ? 9 : 11 },
},
grid: { color: 'rgba(255,255,255,0.08)' },
},
},
};
}
/**
* Create a Chart.js chart with consistent theming and responsive behavior.
*
* @param canvas - Canvas element or its ID
* @param config - Chart.js configuration
* @param containerId - Optional container ID for loading/error states
* @returns The Chart instance
*/
export function createChart<T extends keyof ChartTypeRegistry>(
canvas: HTMLCanvasElement | string,
config: ChartConfiguration<T>,
containerId?: string,
): ChartType<T> {
const ChartCtor = getChart();
const canvasEl = typeof canvas === 'string'
? document.getElementById(canvas) as HTMLCanvasElement | null
: canvas;
if (!canvasEl) {
throw new Error(`Canvas element not found: ${canvas}`);
}
// Merge responsive options with user config
const responsive = getResponsiveOptions();
const mergedConfig: ChartConfiguration<T> = {
...config,
options: deepMerge(responsive, config.options ?? {}) as ChartConfiguration<T>['options'],
};
const container = containerId
? document.getElementById(containerId)
: canvasEl.parentElement;
if (container) {
hideStateOverlays(container);
}
try {
return new ChartCtor(canvasEl, mergedConfig);
} catch (error) {
logger.error(`Failed to create chart:`, error);
if (container) {
showErrorState(container, 'Failed to render chart');
}
throw error;
}
}
/**
* Safely initialize a dashboard section.
* Shows loading state, runs the initializer, shows error state on failure.
*/
export async function initDashboardSection(
containerId: string,
initializer: () => Promise<void>,
loadingMessage = 'Loading data...',
): Promise<void> {
const container = document.getElementById(containerId);
if (!container) {
logger.debug(`Container #${containerId} not found — skipping`);
return;
}
showLoadingState(container, loadingMessage);
try {
await initializer();
hideStateOverlays(container);
} catch (error) {
logger.error(`Dashboard section ${containerId} failed:`, error);
showErrorState(
container,
`Failed to load ${containerId.replace(/-/g, ' ')}`,
);
}
}
/**
* Add keyboard navigation to a Chart.js chart for accessibility.
*/
export function addChartKeyboardNav(
chart: ChartType,
canvas: HTMLCanvasElement,
): void {
canvas.setAttribute('tabindex', '0');
canvas.setAttribute('role', 'img');
canvas.addEventListener('keydown', (e: KeyboardEvent) => {
const meta = chart.getActiveElements();
const currentIndex = meta.length > 0 ? meta[0]!.index : -1;
const datasetIndex = meta.length > 0 ? meta[0]!.datasetIndex : 0;
const maxIndex = (chart.data.datasets[datasetIndex]?.data?.length ?? 1) - 1;
let newIndex = currentIndex;
if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
newIndex = Math.min(currentIndex + 1, maxIndex);
e.preventDefault();
} else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
newIndex = Math.max(currentIndex - 1, 0);
e.preventDefault();
}
if (newIndex !== currentIndex && newIndex >= 0) {
chart.setActiveElements([{ datasetIndex, index: newIndex }]);
chart.update();
}
});
}
/**
* Create a resize handler that re-creates charts on breakpoint changes.
*/
export function createResizeHandler(callback: () => void): () => void {
let currentBreakpoint = getBreakpoint();
let resizeTimer: ReturnType<typeof setTimeout>;
const handler = () => {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(() => {
const newBreakpoint = getBreakpoint();
if (newBreakpoint !== currentBreakpoint) {
currentBreakpoint = newBreakpoint;
callback();
}
}, 250);
};
window.addEventListener('resize', handler);
return () => window.removeEventListener('resize', handler);
}
function getBreakpoint(): string {
const w = window.innerWidth;
if (w < BREAKPOINTS.tablet) return 'mobile';
if (w < BREAKPOINTS.desktop) return 'tablet';
if (w < BREAKPOINTS.large) return 'desktop';
return 'large';
}
/**
* Deep merge two objects (simple version for chart configs).
*/
function deepMerge(
target: Record<string, unknown>,
source: Record<string, unknown>,
): Record<string, unknown> {
const result = { ...target };
for (const key of Object.keys(source)) {
const sourceVal = source[key];
const targetVal = result[key];
if (
sourceVal &&
typeof sourceVal === 'object' &&
!Array.isArray(sourceVal) &&
targetVal &&
typeof targetVal === 'object' &&
!Array.isArray(targetVal)
) {
result[key] = deepMerge(
targetVal as Record<string, unknown>,
sourceVal as Record<string, unknown>,
);
} else {
result[key] = sourceVal;
}
}
return result;
}
|