Correlation Analysis: ${stockTicker} vs. ${cryptoName} (${periodText})
${correlationCoefficient.toFixed(2)}
${interpretation}
-1 (Strong Negative)
0 (No Correlation)
+1 (Strong Positive)
Key Observations:
${keyObservations}
Disclaimer: This analysis uses simulated data and provides a simplified correlation. It is for illustrative purposes only and does not constitute investment advice. Actual market correlations are complex and can change rapidly.
Download Analysis PDF
`;
resultsArea.style.display = 'block';
resultsArea.scrollIntoView({ behavior: 'smooth' });
}
async function generateCorrelationPdf() {
const pdfContent = document.getElementById('pdf-content');
const downloadBtn = event.target;
if (!pdfContent) {
console.error("PDF content area not found.");
if(typeof alert === 'function') alert("Error: Could not find content to generate PDF.");
return;
}
if (!html2canvas || !jsPDF) {
console.error("html2canvas or jsPDF library not found.");
if(typeof alert === 'function') alert("Error: PDF generation library not loaded.");
return;
}
const originalButtonText = downloadBtn.textContent;
downloadBtn.textContent = "Generating...";
downloadBtn.disabled = true;
const pdfHeaderElement = pdfContent.querySelector('.pdf-header');
const screenHeaderElement = pdfContent.querySelector('.screen-only');
if (pdfHeaderElement) pdfHeaderElement.style.display = 'block';
if (screenHeaderElement) screenHeaderElement.style.display = 'none';
try {
const canvas = await html2canvas(pdfContent, {
scale: 1.5,
useCORS: true,
logging: false,
onclone: (doc) => {
const clonedHeader = doc.querySelector('.pdf-header');
if (clonedHeader) clonedHeader.style.display = 'block';
const clonedScreenHeader = doc.querySelector('.screen-only');
if (clonedScreenHeader) clonedScreenHeader.style.display = 'none';
}
});
if (pdfHeaderElement) pdfHeaderElement.style.display = 'none';
if (screenHeaderElement) screenHeaderElement.style.display = 'block';
const imgData = canvas.toDataURL('image/jpeg', 0.8);
const pdf = new jsPDF({
orientation: 'p',
unit: 'mm',
format: 'a4'
});
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const imgProps = pdf.getImageProperties(imgData);
const margin = 10;
const contentWidth = pdfWidth - 2 * margin;
const contentHeight = (imgProps.height * contentWidth) / imgProps.width;
if (contentHeight <= pdfHeight - 2 * margin) {
pdf.addImage(imgData, 'JPEG', margin, margin, contentWidth, contentHeight);
} else {
let remainingImgHeight = imgProps.height;
let sourceY = 0;
const sourceWidth = imgProps.width;
const pageCanvasHeight = ((pdfHeight - 2 * margin) / contentWidth) * sourceWidth;
while (remainingImgHeight > 0) {
const currentChunkHeight = Math.min(remainingImgHeight, pageCanvasHeight);
const tempCanvas = document.createElement('canvas');
tempCanvas.width = sourceWidth;
tempCanvas.height = currentChunkHeight;
const tempCtx = tempCanvas.getContext('2d');
tempCtx.drawImage(canvas, 0, sourceY, sourceWidth, currentChunkHeight, 0, 0, sourceWidth, currentChunkHeight);
const chunkImgData = tempCanvas.toDataURL('image/jpeg', 0.8);
pdf.addImage(chunkImgData, 'JPEG', margin, margin, contentWidth, (currentChunkHeight * contentWidth) / sourceWidth);
remainingImgHeight -= currentChunkHeight;
sourceY += currentChunkHeight;
if (remainingImgHeight > 0) {
pdf.addPage();
}
}
}
const stockTicker = document.getElementById('stockTicker').value.toUpperCase() || "STOCK";
const crypto = document.getElementById('cryptocurrency').value || "CRYPTO";
pdf.save(`${stockTicker}_${crypto}_Correlation_Analysis.pdf`);
} catch (error) {
console.error("Error generating PDF:", error);
if(typeof alert === 'function') alert("An error occurred while generating the PDF: " + error.message);
} finally {
downloadBtn.textContent = originalButtonText;
downloadBtn.disabled = false;
if (pdfHeaderElement) pdfHeaderElement.style.display = 'none';
if (screenHeaderElement) screenHeaderElement.style.display = 'block';
}
}