Demand Dashboard Tool

Demand Dashboard Tool

Demand Overview

Track and visualize demand for products or services over time or by category.

Total Demand (Units)

0

Average Monthly Demand (Units)

0.0

Number of Products/Categories

0
Demand Over Time

Configure Demand Data

Add, edit, or remove demand entries.

Add/Edit Demand Entry

ID Date Product/Category Quantity Notes Actions

No demand data available for charting. Add data in "Data Configuration".

'; return; } // Aggregate demand by date (sum quantities for same date) const aggregatedData = d3.rollup(demandData, v => d3.sum(v, d => d.demandQuantity), d => d.date); const chartData = Array.from(aggregatedData, ([date, quantity]) => ({ date: new Date(date), quantity })).sort((a, b) => a.date - b.date); const margin = { top: 20, right: 30, bottom: 50, left: 60 }; const width = demandChartContainer.clientWidth - margin.left - margin.right; const height = demandChartContainer.clientHeight - margin.top - margin.bottom - 30; // Account for title height const svg = d3.select(demandChartContainer) .append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", `translate(${margin.left},${margin.top})`); // X scale (Time scale for dates) const x = d3.scaleTime() .domain(d3.extent(chartData, d => d.date)) .range([0, width]); // Y scale (Linear scale for quantity) const y = d3.scaleLinear() .domain([0, d3.max(chartData, d => d.quantity) * 1.1]) // 10% buffer .range([height, 0]); // Line generator const line = d3.line() .x(d => x(d.date)) .y(d => y(d.quantity)); // Add X axis svg.append("g") .attr("class", "chart-axis x-axis") .attr("transform", `translate(0,${height})`) .call(d3.axisBottom(x).tickFormat(d3.timeFormat("%Y-%m-%d"))) .selectAll("text") .style("text-anchor", "end") .attr("dx", "-.8em") .attr("dy", ".15em") .attr("transform", "rotate(-45)"); // Add Y axis svg.append("g") .attr("class", "chart-axis y-axis") .call(d3.axisLeft(y).tickFormat(d3.format("~s"))); // Format large numbers // Add the line path svg.append("path") .datum(chartData) .attr("class", "line") .attr("d", line); // Tooltip const tooltip = d3.select("body").append("div") .attr("class", "chart-tooltip") .style("opacity", 0); // Add dots for each data point svg.selectAll(".dot") .data(chartData) .enter().append("circle") .attr("class", "dot") .attr("cx", d => x(d.date)) .attr("cy", d => y(d.quantity)) .attr("r", 5) .on("mouseover", function(event, d) { d3.select(this).attr("r", 7).style("fill", "orange"); tooltip.transition() .duration(200) .style("opacity", .9); tooltip.html(`Date: ${d3.timeFormat("%Y-%m-%d")(d.date)}
Demand: ${d.quantity.toLocaleString('en-US')} units`) .style("left", (event.pageX + 10) + "px") .style("top", (event.pageY - 28) + "px"); }) .on("mouseout", function(d) { d3.select(this).attr("r", 5).style("fill", "steelblue"); tooltip.transition() .duration(500) .style("opacity", 0); }); } // --- PDF Download Function --- /** * Generates and downloads a PDF of the current dashboard content. */ window.downloadPdf = async function() { if (!dashboardTab) { console.error('Error: Dashboard tab content not found for PDF generation.'); return; } // Temporarily hide elements not needed in PDF const elementsToHide = document.querySelectorAll('.tab-nav, .nav-buttons, #downloadPdfButton, .chart-tooltip'); elementsToHide.forEach(el => el.style.display = 'none'); // Ensure the dashboard tab is active for capture dashboardTab.classList.add('active'); // Capture summary metrics const summaryCanvas = await html2canvas(document.querySelector('.summary-metrics'), { scale: 2, useCORS: true, logging: false }); const summaryImgData = summaryCanvas.toDataURL('image/png'); // Capture chart const chartCanvas = await html2canvas(demandChartContainer, { scale: 2, useCORS: true, logging: false }); const chartImgData = chartCanvas.toDataURL('image/png'); // Re-show hidden elements immediately after capture elementsToHide.forEach(el => el.style.display = ''); const { jsPDF } = window.jspdf; const pdf = new jsPDF({ orientation: 'portrait', unit: 'px', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); let yOffset = 40; // Add title to PDF pdf.setFontSize(22); pdf.text("Demand Dashboard Report", pdfWidth / 2, yOffset, { align: 'center' }); yOffset += 20; // Add current date/time to PDF pdf.setFontSize(10); pdf.text(`Generated on: ${new Date().toLocaleString()}`, pdfWidth / 2, yOffset, { align: 'center' }); yOffset += 40; // Add Summary Metrics Image pdf.setFontSize(18); pdf.text("Demand Summary Metrics", pdfWidth / 2, yOffset, { align: 'center' }); yOffset += 10; const summaryImgHeight = (summaryCanvas.height * (pdfWidth - 40)) / summaryCanvas.width; pdf.addImage(summaryImgData, 'PNG', 20, yOffset, pdfWidth - 40, summaryImgHeight); yOffset += summaryImgHeight + 30; // Add Demand Chart Image pdf.setFontSize(18); pdf.text("Demand Over Time Chart", pdfWidth / 2, yOffset, { align: 'center' }); yOffset += 10; const chartImgHeight = (chartCanvas.height * (pdfWidth - 40)) / chartCanvas.width; // Check if chart image fits on current page, add new page if not if (yOffset + chartImgHeight > pdf.internal.pageSize.getHeight() - 20) { pdf.addPage(); yOffset = 40; // Reset yOffset for new page } pdf.addImage(chartImgData, 'PNG', 20, yOffset, pdfWidth - 40, chartImgHeight); yOffset += chartImgHeight + 30; // Add a section for detailed data (tabular format) pdf.addPage(); pdf.setFontSize(18); pdf.text("Detailed Demand Data", pdf.internal.pageSize.getWidth() / 2, 40, { align: 'center' }); const tableData = demandData.map(d => [ d.id, d.date, d.productCategory, d.demandQuantity.toLocaleString('en-US'), d.notes || 'N/A' ]); pdf.autoTable({ head: [['ID', 'Date', 'Product/Category', 'Quantity', 'Notes']], body: tableData, startY: 60, theme: 'grid', styles: { fontSize: 9, cellPadding: 4 }, headStyles: { fillColor: [242, 242, 242], textColor: [51, 51, 51], fontStyle: 'bold' }, alternateRowStyles: { fillColor: [251, 251, 251] }, margin: { top: 70, left: 20, right: 20 } }); pdf.save('demand_dashboard_report.pdf'); }; // --- Event Listeners and Initial Render --- downloadPdfButton.addEventListener('click', downloadPdf); // Initial setup // Sort demand data by date initially demandData.sort((a, b) => new Date(a.date) - new Date(b.date)); updateConfigTable(); renderDashboardMetrics(); renderDemandChart(); updateNavigationButtons(); // Set initial button states });
Scroll to Top