Repeat Customer Rate
${repeatCustomerRateElem.textContent}
`;
pdfContentWrapper.appendChild(summarySection);
// Add distribution charts
const chartsSection = document.createElement('div');
chartsSection.classList.add('grid', 'grid-cols-2', 'gap-6', 'mb-6');
chartsSection.innerHTML = `
Customers by Purchase Count
Revenue by Product/Service
`;
pdfContentWrapper.appendChild(chartsSection);
// Populate PDF charts (re-use logic but target PDF elements)
const uniqueCustomerIds = new Set(purchaseRecords.map(record => record.customerId));
const totalCustomers = uniqueCustomerIds.size;
const customerPurchaseCounts = {};
purchaseRecords.forEach(record => { customerPurchaseCounts[record.customerId] = (customerPurchaseCounts[record.customerId] || 0) + 1; });
const purchaseCountDistribution = { '1 Purchase': 0, '2 Purchases': 0, '3-5 Purchases': 0, '6+ Purchases': 0 };
for (const custId in customerPurchaseCounts) {
const count = customerPurchaseCounts[custId];
if (count === 1) { purchaseCountDistribution['1 Purchase']++; }
else if (count === 2) { purchaseCountDistribution['2 Purchases']++; }
else if (count >= 3 && count <= 5) { purchaseCountDistribution['3-5 Purchases']++; }
else if (count >= 6) { purchaseCountDistribution['6+ Purchases']++; }
}
renderBarChart(chartsSection.querySelector('#pdfCustomersByPurchaseCountChart'), purchaseCountDistribution, 'count');
const revenueByCustomerType = { 'New Customers': 0, 'Repeat Customers': 0 };
const customerPurchaseDetails = {};
purchaseRecords.forEach(record => {
if (!customerPurchaseDetails[record.customerId]) { customerPurchaseDetails[record.customerId] = { count: 0, totalRevenue: 0 }; }
customerPurchaseDetails[record.customerId].count++;
customerPurchaseDetails[record.customerId].totalRevenue += record.orderValue;
});
for (const custId in customerPurchaseDetails) {
if (customerPurchaseDetails[custId].count === 1) { revenueByCustomerType['New Customers'] += customerPurchaseDetails[custId].totalRevenue; }
else { revenueByCustomerType['Repeat Customers'] += customerPurchaseDetails[custId].totalRevenue; }
}
renderBarChart(chartsSection.querySelector('#pdfRevenueByCustomerTypeChart'), revenueByCustomerType, 'currency');
const revenueByProduct = purchaseRecords.reduce((acc, record) => { acc[record.productService] = (acc[record.productService] || 0) + record.orderValue; return acc; }, {});
renderBarChart(chartsSection.querySelector('#pdfRevenueByProductChart'), revenueByProduct, 'currency');
// Add detailed purchase list table
const purchaseListSection = document.createElement('div');
purchaseListSection.innerHTML = `
Detailed Purchase Records
| ID |
Customer Name |
Order Date |
Product/Service |
Order Value ($) |
Customer Purchases |
Is Repeat Customer? |
${dashboardPurchaseTableBody.innerHTML}
`;
pdfContentWrapper.appendChild(purchaseListSection);
// Options for html2pdf
const options = {
margin: 10,
filename: 'Customer_Purchase_Frequency_Dashboard.pdf',
image: { type: 'jpeg', quality: 0.98 },
html2canvas: { scale: 2, logging: true, dpi: 192, letterRendering: true },
jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' },
pagebreak: { mode: ['avoid-all', 'css', 'legacy'] }
};
// Generate PDF from the temporary content wrapper
html2pdf().from(pdfContentWrapper).set(options).save();
// Clean up the temporary div (optional, as it's not appended to the DOM)
pdfContentWrapper.remove();
}
/**
* Loads initial sample data for the dashboard.
* Relevant to USA context.
*/
function loadSampleData() {
purchaseRecords = [
{ id: 'PURCH001', customerId: 'CUST001', customerName: 'Alice Johnson', orderDate: '2024-01-10', productService: 'Product A', orderValue: 120.50 },
{ id: 'PURCH002', customerId: 'CUST002', customerName: 'Bob Williams', orderDate: '2024-01-15', productService: 'Service X', orderValue: 250.00 },
{ id: 'PURCH003', customerId: 'CUST001', customerName: 'Alice Johnson', orderDate: '2024-02-01', productService: 'Product B', orderValue: 80.00 },
{ id: 'PURCH004', customerId: 'CUST003', customerName: 'Charlie Brown', orderDate: '2024-02-10', productService: 'Product C', orderValue: 50.75 },
{ id: 'PURCH005', customerId: 'CUST002', customerName: 'Bob Williams', orderDate: '2024-02-20', productService: 'Service Y', orderValue: 150.00 },
{ id: 'PURCH006', customerId: 'CUST001', customerName: 'Alice Johnson', orderDate: '2024-03-05', productService: 'Product D', orderValue: 200.00 },
{ id: 'PURCH007', customerId: 'CUST004', customerName: 'Diana Miller', orderDate: '2024-03-10', productService: 'Product A', orderValue: 120.50 },
{ id: 'PURCH008', customerId: 'CUST001', customerName: 'Alice Johnson', orderDate: '2024-04-01', productService: 'Product E', orderValue: 30.00 },
{ id: 'PURCH009', customerId: 'CUST005', customerName: 'Ethan Davis', orderDate: '2024-04-05', productService: 'Service X', orderValue: 250.00 },
{ id: 'PURCH010', customerId: 'CUST002', customerName: 'Bob Williams', orderDate: '2024-04-15', productService: 'Product B', orderValue: 80.00 }
];
}