Growth Metrics Dashboard

Metric Trend

Manage Metric Types

Metric NameUnitAction

Manage Monthly Data

MetricDateValueActions

Error: A required library or the main container is missing. The tool cannot be initialized.

'; return; } const { jsPDF } = window.jspdf; // --- STATE & CONFIG --- let metricTypes = JSON.parse(localStorage.getItem('gmd_metricTypes')) || []; let dataPoints = JSON.parse(localStorage.getItem('gmd_dataPoints')) || []; let trendChart = null; const getSampleData = () => { const sampleMetricTypes = [ { id: 'm-1', name: 'Monthly Recurring Revenue', unit: '$' }, { id: 'm-2', name: 'Churn Rate', unit: '%' } ]; const sampleDataPoints = [ { id: 'dp-1', metricId: 'm-1', date: '2025-04', value: 42000 }, { id: 'dp-2', metricId: 'm-1', date: '2025-05', value: 45000 }, { id: 'dp-3', metricId: 'm-1', date: '2025-06', value: 51000 }, { id: 'dp-4', metricId: 'm-2', date: '2025-04', value: 4.5 }, { id: 'dp-5', metricId: 'm-2', date: '2025-05', value: 4.2 }, { id: 'dp-6', metricId: 'm-2', date: '2025-06', value: 3.9 } ]; return { sampleMetricTypes, sampleDataPoints }; }; if (metricTypes.length === 0 && dataPoints.length === 0) { const { sampleMetricTypes, sampleDataPoints } = getSampleData(); metricTypes = sampleMetricTypes; dataPoints = sampleDataPoints; } // --- ELEMENT REFERENCES --- const kpiCardsContainer = document.getElementById('gmd-kpi-cards'); const chartContainer = document.getElementById('gmd-trend-chart'); const chartSelector = document.getElementById('gmd-metric-chart-selector'); const tabButtons = document.querySelectorAll('.gmd-tab-button'); const tabContents = document.querySelectorAll('.gmd-tab-content'); const nextBtn = document.getElementById('gmd-next-btn'); const prevBtn = document.getElementById('gmd-prev-btn'); // Management Tab Elements const addMetricTypeBtn = document.getElementById('gmd-add-metric-type-btn'); const metricTypesTableBody = document.getElementById('gmd-metric-types-table-body'); const dataPointForm = document.getElementById('gmd-data-point-form'); const dataPointsTableBody = document.getElementById('gmd-data-points-table-body'); const clearFormBtn = document.getElementById('gmd-clear-form-btn'); // --- UTILITY FUNCTIONS --- const saveState = () => { localStorage.setItem('gmd_metricTypes', JSON.stringify(metricTypes)); localStorage.setItem('gmd_dataPoints', JSON.stringify(dataPoints)); }; const generateId = () => '_' + Math.random().toString(36).substr(2, 9); const formatValue = (value, unit) => { if (value === undefined || value === null) return 'N/A'; const num = Number(value); if (unit === '$') return `$${num.toLocaleString('en-US', { minimumFractionDigits: 0, maximumFractionDigits: 0 })}`; if (unit === '%') return `${num.toFixed(2)}%`; return num.toLocaleString('en-US'); }; // --- TAB NAVIGATION --- window.gmd_changeTab = (event, tabId) => { tabContents.forEach(content => content.classList.remove('active')); tabButtons.forEach(button => button.classList.remove('active')); document.getElementById(tabId).classList.add('active'); event.currentTarget.classList.add('active'); updateNavButtons(); }; const updateNavButtons = () => { const activeTabIndex = [...tabButtons].findIndex(btn => btn.classList.contains('active')); prevBtn.disabled = activeTabIndex === 0; nextBtn.disabled = activeTabIndex === tabButtons.length - 1; }; nextBtn.addEventListener('click', () => { const i = [...tabButtons].findIndex(b=>b.classList.contains('active')); if(i < tabButtons.length - 1) tabButtons[i+1].click(); }); prevBtn.addEventListener('click', () => { const i = [...tabButtons].findIndex(b=>b.classList.contains('active')); if(i > 0) tabButtons[i-1].click(); }); // --- RENDER FUNCTIONS --- const renderAll = () => { renderDashboard(); renderManagementUI(); updateNavButtons(); }; const renderDashboard = () => { renderKpiCards(); renderChartSelector(); renderTrendChart(); }; const renderKpiCards = () => { kpiCardsContainer.innerHTML = ''; if (metricTypes.length === 0) { kpiCardsContainer.innerHTML = '
No metrics defined. Please add a metric type in the "Manage Data" tab.
'; return; } metricTypes.forEach(metric => { const relevantPoints = dataPoints .filter(dp => dp.metricId === metric.id) .sort((a, b) => new Date(a.date) - new Date(b.date)); const latestPoint = relevantPoints[relevantPoints.length - 1]; const previousPoint = relevantPoints[relevantPoints.length - 2]; let changeHtml = 'No previous data'; if (latestPoint && previousPoint) { const change = ((latestPoint.value - previousPoint.value) / previousPoint.value) * 100; const changeClass = change >= 0 ? 'positive' : 'negative'; const sign = change >= 0 ? '+' : ''; changeHtml = `${sign}${change.toFixed(1)}% MoM`; } const card = document.createElement('div'); card.className = 'gmd-kpi-card'; card.innerHTML = `

${metric.name}

${latestPoint ? formatValue(latestPoint.value, metric.unit) : 'No Data'}

${changeHtml}
`; kpiCardsContainer.appendChild(card); }); }; const renderChartSelector = () => { chartSelector.innerHTML = metricTypes.map(m => ``).join(''); }; const renderTrendChart = () => { const selectedMetricId = chartSelector.value; if (!selectedMetricId) { chartContainer.innerHTML = '
Select a metric to display its trend.
'; return; } const metric = metricTypes.find(m => m.id === selectedMetricId); const chartData = dataPoints .filter(dp => dp.metricId === selectedMetricId) .sort((a, b) => new Date(a.date) - new Date(b.date)); const options = { chart: { type: 'area', height: '100%', toolbar: { show: false } }, series: [{ name: metric.name, data: chartData.map(dp => dp.value) }], xaxis: { categories: chartData.map(dp => new Date(dp.date + '-02').toLocaleString('default', { month: 'short', year: '2-digit' })), }, yaxis: { labels: { formatter: (value) => formatValue(value, metric.unit) } }, stroke: { curve: 'smooth' }, dataLabels: { enabled: false }, tooltip: { y: { formatter: (value) => formatValue(value, metric.unit) } } }; if (trendChart) trendChart.destroy(); chartContainer.innerHTML = ''; trendChart = new ApexCharts(chartContainer, options); trendChart.render(); }; const renderManagementUI = () => { // Render Metric Types Table metricTypesTableBody.innerHTML = ''; metricTypes.forEach(m => { const row = document.createElement('tr'); row.innerHTML = ` ${m.name} ${m.unit} `; metricTypesTableBody.appendChild(row); }); // Populate Metric Select document.getElementById('gmd-data-metric-type').innerHTML = metricTypes.map(m => ``).join(''); // Render Data Points Table dataPointsTableBody.innerHTML = ''; dataPoints.sort((a,b) => new Date(b.date) - new Date(a.date)).forEach(dp => { const metric = metricTypes.find(m => m.id === dp.metricId); if (!metric) return; const row = document.createElement('tr'); row.innerHTML = ` ${metric.name} ${dp.date} ${formatValue(dp.value, metric.unit)} `; dataPointsTableBody.appendChild(row); }); }; // --- EVENT LISTENERS --- chartSelector.addEventListener('change', renderTrendChart); addMetricTypeBtn.addEventListener('click', () => { const nameInput = document.getElementById('gmd-metric-type-name'); const unitInput = document.getElementById('gmd-metric-type-unit'); if (nameInput.value.trim()) { metricTypes.push({ id: generateId(), name: nameInput.value.trim(), unit: unitInput.value }); nameInput.value = ''; saveState(); renderAll(); } }); metricTypesTableBody.addEventListener('click', e => { if (e.target.tagName === 'BUTTON') { const id = e.target.dataset.id; if (confirm('Are you sure you want to delete this metric type and all its data?')) { metricTypes = metricTypes.filter(m => m.id !== id); dataPoints = dataPoints.filter(dp => dp.metricId !== id); saveState(); renderAll(); } } }); dataPointForm.addEventListener('submit', e => { e.preventDefault(); const id = document.getElementById('gmd-data-point-id').value; const data = { metricId: document.getElementById('gmd-data-metric-type').value, date: document.getElementById('gmd-data-date').value, value: parseFloat(document.getElementById('gmd-data-value').value) }; if (id) { // Update const index = dataPoints.findIndex(dp => dp.id === id); if (index > -1) dataPoints[index] = { ...dataPoints[index], ...data }; } else { // Add new dataPoints.push({ id: generateId(), ...data }); } saveState(); renderAll(); dataPointForm.reset(); document.getElementById('gmd-data-point-id').value = ''; }); clearFormBtn.addEventListener('click', () => { dataPointForm.reset(); document.getElementById('gmd-data-point-id').value = ''; }); dataPointsTableBody.addEventListener('click', e => { if(e.target.tagName !== 'BUTTON') return; const id = e.target.dataset.id; const action = e.target.dataset.action; if (action === 'delete') { if (confirm('Are you sure?')) { dataPoints = dataPoints.filter(dp => dp.id !== id); saveState(); renderAll(); } } else if (action === 'edit') { const dp = dataPoints.find(p => p.id === id); if (dp) { document.getElementById('gmd-data-point-id').value = dp.id; document.getElementById('gmd-data-metric-type').value = dp.metricId; document.getElementById('gmd-data-date').value = dp.date; document.getElementById('gmd-data-value').value = dp.value; } } }); // --- PDF EXPORT --- document.getElementById('gmd-download-pdf-btn').addEventListener('click', function () { const content = document.getElementById('gmd-pdf-capture-area'); const btn = this; btn.textContent = 'Generating...'; btn.disabled = true; html2canvas(content, { scale: 2 }).then(canvas => { const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF({ orientation: 'l', unit: 'mm', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = pdf.internal.pageSize.getHeight(); const imgWidth = canvas.width; const imgHeight = canvas.height; const ratio = imgWidth / imgHeight; let finalImgWidth = pdfWidth - 20; let finalImgHeight = finalImgWidth / ratio; if (finalImgHeight > pdfHeight - 20) { finalImgHeight = pdfHeight - 20; finalImgWidth = finalImgHeight * ratio; } const x = (pdfWidth - finalImgWidth) / 2; const y = (pdfHeight - finalImgHeight) / 2; pdf.addImage(imgData, 'PNG', x, y, finalImgWidth, finalImgHeight); pdf.save('Growth_Metrics_Report.pdf'); }).finally(() => { btn.textContent = 'Download Report as PDF'; btn.disabled = false; }); }); // --- INITIALIZATION --- renderAll(); });
Scroll to Top