// ===== Theme Toggle =====
function toggleTheme() {
const html = document.documentElement;
const current = html.getAttribute('data-theme');
const next = current === 'light' ? 'dark' : 'light';
html.setAttribute('data-theme', next);
localStorage.setItem('mmm-theme', next);
updateThemeButton(next);
// Re-fetch page data to update chart colors
if (typeof fetchPageData === 'function') fetchPageData();
}
function updateThemeButton(theme) {
const icon = document.getElementById('themeIcon');
const label = document.getElementById('themeLabel');
if (theme === 'light') {
icon.innerHTML = '';
label.textContent = 'Dark';
} else {
icon.innerHTML = '';
label.textContent = 'Light';
}
}
function getThemeColors() {
const isLight = document.documentElement.getAttribute('data-theme') === 'light';
return {
paper: 'rgba(0,0,0,0)',
plot: 'rgba(0,0,0,0)',
grid: isLight ? 'rgba(0, 0, 0, 0.06)' : 'rgba(255, 255, 255, 0.06)',
text: isLight ? '#525252' : '#a3a3a3',
textPrimary: isLight ? '#171717' : '#fafafa',
hover: isLight ? 'rgba(245,245,245,0.95)' : 'rgba(23, 23, 23, 0.95)',
hoverText: isLight ? '#171717' : '#fafafa',
hoverBorder: isLight ? 'rgba(0,0,0,0.08)' : 'rgba(255, 255, 255, 0.1)',
};
}
// Initialize theme on load
(function() {
const saved = localStorage.getItem('mmm-theme') || 'dark';
document.documentElement.setAttribute('data-theme', saved);
document.addEventListener('DOMContentLoaded', () => updateThemeButton(saved));
})();
// ===== Sidebar Toggle =====
function toggleSidebar() {
document.getElementById('sidebar').classList.toggle('collapsed');
}
// ===== Format Helpers =====
function formatCurrency(val) {
if (Math.abs(val) >= 1e6) return '$' + (val / 1e6).toFixed(1) + 'M';
if (Math.abs(val) >= 1e3) return '$' + (val / 1e3).toFixed(1) + 'K';
return '$' + val.toFixed(2);
}
function formatNumber(val) {
if (Math.abs(val) >= 1e6) return (val / 1e6).toFixed(1) + 'M';
if (Math.abs(val) >= 1e3) return (val / 1e3).toFixed(1) + 'K';
return val.toLocaleString();
}
function formatValue(val, format) {
switch (format) {
case 'currency': return formatCurrency(val);
case 'percent': return val.toFixed(1) + '%';
case 'ratio': return val.toFixed(2) + 'x';
case 'number': return formatNumber(val);
default: return val;
}
}
// ===== KPI Icon SVG Paths =====
const KPI_ICONS = {
'dollar-sign': '',
'credit-card': '',
'trending-up': '',
'percent': '',
'pie-chart': '',
'users': '',
'eye': '',
'mouse-pointer': '',
'check-circle': '',
'target': '',
'activity': '',
'bar-chart-2': '',
'link': '',
'sun': '',
'database': '',
'award': '',
'tag': '',
'layers': '',
'cpu': '',
};
// ===== KPI Card Rendering =====
function renderKPICards(containerId, kpis) {
const grid = document.getElementById(containerId);
grid.innerHTML = kpis.map(kpi => {
const changeClass = kpi.inverse
? (kpi.change <= 0 ? 'positive' : 'negative')
: (kpi.change >= 0 ? 'positive' : 'negative');
const arrow = kpi.change >= 0 ? '▲' : '▼';
const changeHtml = kpi.change !== undefined
? `${arrow} ${Math.abs(kpi.change)}%`
: '';
const iconPath = KPI_ICONS[kpi.icon] || KPI_ICONS['trending-up'];
return `
${formatValue(kpi.value, kpi.format)}
${kpi.name}
`;
}).join('');
}
// ===== Table Sort =====
let sortState = {};
function sortTable(tableId, colIndex) {
const table = document.getElementById(tableId);
const tbody = table.querySelector('tbody');
const rows = Array.from(tbody.querySelectorAll('tr'));
const key = `${tableId}-${colIndex}`;
const asc = sortState[key] = !sortState[key];
rows.sort((a, b) => {
let aVal = a.cells[colIndex].textContent.replace(/[$,%x]/g, '').trim();
let bVal = b.cells[colIndex].textContent.replace(/[$,%x]/g, '').trim();
let aNum = parseFloat(aVal), bNum = parseFloat(bVal);
if (!isNaN(aNum) && !isNaN(bNum)) return asc ? aNum - bNum : bNum - aNum;
return asc ? aVal.localeCompare(bVal) : bVal.localeCompare(aVal);
});
rows.forEach(r => tbody.appendChild(r));
table.querySelectorAll('th').forEach((th, i) => {
th.classList.toggle('sorted', i === colIndex);
});
}
// ===== Table Search =====
function filterTable(input, tableId) {
const filter = input.value.toLowerCase();
const rows = document.querySelectorAll(`#${tableId} tbody tr`);
rows.forEach(row => {
const text = row.textContent.toLowerCase();
row.style.display = text.includes(filter) ? '' : 'none';
});
}
// ===== Chart Fullscreen =====
let currentChartData = {};
function expandChart(chartId) {
const data = currentChartData[chartId];
if (!data) return;
const overlay = document.getElementById('fullscreenOverlay');
overlay.classList.add('active');
const layout = Object.assign({}, data.layout, { width: null, height: null, autosize: true });
Plotly.newPlot('fullscreenChart', data.data, layout, { responsive: true, displayModeBar: true });
}
function closeFullscreen(e, force) {
if (force || e.target.id === 'fullscreenOverlay') {
document.getElementById('fullscreenOverlay').classList.remove('active');
Plotly.purge('fullscreenChart');
}
}
document.addEventListener('keydown', e => { if (e.key === 'Escape') closeFullscreen(null, true); });
// ===== Chart Download =====
function downloadChart(chartId) {
Plotly.downloadImage(chartId, { format: 'png', width: 1200, height: 600, filename: chartId });
}
// ===== Export Page =====
function exportPage() {
window.print();
}
// ===== Timestamp =====
function updateTimestamp() {
const el = document.getElementById('lastRefresh');
if (el) el.textContent = 'Last updated: ' + new Date().toLocaleString();
}