// ===== Screen 8: Volume & Revenue Drivers =====
function fetchPageData() {
const filters = getFilterState();
fetch('/diagnostics/api/diagnostics/data', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(filters)
})
.then(r => r.json())
.then(data => {
renderKPICards('diagKpiGrid', data.kpis);
// Section 0: Marketing ROI Scorecard Table
renderRoiScorecard(data.tables.marketing_roi_scorecard);
// Section 1: Revenue Decomposition
renderChart('chart_decomposition', data.charts.chart_decomposition);
// Section 2: Volume Contribution by Period + Donut + Support Table
renderChart('chart_vol_contribution', data.charts.chart_vol_contribution);
renderChart('chart_base_incremental', data.charts.chart_base_incremental);
renderSupportContribTable(data.tables.support_contribution);
// Section 3: Investment Mix + Efficiency Trend
renderChart('chart_inv_mix', data.charts.chart_inv_mix);
renderChart('chart_efficiency_trend', data.charts.chart_efficiency_trend);
// Section 4: Contribution Waterfall + Period Change
renderChart('chart_contribution', data.charts.chart_contribution);
renderChart('chart_waterfall', data.charts.chart_waterfall);
// Section 5: Seasonality + Summary Table
renderChart('chart_seasonality', data.charts.chart_seasonality);
renderOverallSummary(data.tables.overall_summary);
updateTimestamp();
})
.catch(err => console.error('Error loading diagnostics data:', err));
}
// ---- helpers ----
function changeClass(val) {
if (val > 0) return 'positive';
if (val < 0) return 'negative';
return 'neutral';
}
function changeArrow(val) {
if (val > 0) return '▲';
if (val < 0) return '▼';
return '—';
}
// ---- Section 0: Marketing ROI Scorecard Table ----
function renderRoiScorecard(rows) {
const tbody = document.getElementById('roiScorecardBody');
if (!tbody || !rows) return;
if (rows.length === 0) {
tbody.innerHTML = '
| No data available |
';
return;
}
tbody.innerHTML = rows.map(r => {
const invCls = changeClass(r.inv_chg);
const roiCls = changeClass(r.roi_chg);
const effCls = changeClass(r.efficiency_cur - r.efficiency_prev);
const isTotal = r.driver === 'Total Marketing';
const rowStyle = isTotal ? 'style="font-weight:700;border-top:2px solid rgba(255,255,255,0.12);background:rgba(255,255,255,0.03);"' : '';
const nameStyle = `style="font-weight:${isTotal ? '700' : '600'};color:${r.color};"`;
return `
|
${r.driver}
|
${r.inv_cur_fmt} |
${r.inv_prev_fmt} |
${changeArrow(r.inv_chg)} ${Math.abs(r.inv_chg).toFixed(1)}%
|
${r.vol_uplift_pct.toFixed(1)}% |
${r.efficiency_cur_fmt} |
${r.efficiency_prev_fmt} |
${r.roi_cur_fmt} |
${r.roi_prev_fmt} |
${changeArrow(r.roi_chg)} ${Math.abs(r.roi_chg).toFixed(1)}%
|
`;
}).join('');
if (typeof feather !== 'undefined') feather.replace();
}
// ---- Section 2: Support vs Contribution Table ----
function renderSupportContribTable(rows) {
const tbody = document.getElementById('supportContribBody');
if (!tbody || !rows) return;
if (rows.length === 0) {
tbody.innerHTML = '| No data available |
';
return;
}
const catColors = {
'Media': '#3987e5',
'Promo': '#22c55e',
'Base': '#737373',
};
tbody.innerHTML = rows.map(r => {
const catColor = catColors[r.category] || '#9ca3af';
const isBase = r.category === 'Base';
const rowStyle = isBase ? 'style="color:#737373;"' : '';
return `
| ${r.driver} |
${r.category}
|
${r.support} |
${r.rev_contribution} |
${r.vol_contribution} |
${r.efficiency} |
${r.pct_of_total} |
`;
}).join('');
}
// ---- Section 5: Overall KPI Summary Table ----
function renderOverallSummary(rows) {
const tbody = document.getElementById('overallSummaryBody');
if (!tbody || !rows) return;
if (rows.length === 0) {
tbody.innerHTML = '| No data available |
';
return;
}
tbody.innerHTML = rows.map(r => {
const cls = changeClass(r.change_pct);
const indent = r.indent ? 'style="padding-left: 28px; color: #737373;"' : '';
return `
|
${r.metric}
|
${r.current_fmt} |
${r.previous_fmt} |
${r.change_abs_fmt} |
${changeArrow(r.change_pct)} ${Math.abs(r.change_pct).toFixed(1)}%
|
`;
}).join('');
if (typeof feather !== 'undefined') feather.replace();
}
document.addEventListener('DOMContentLoaded', fetchPageData);