Investment in Very High-Risk Areas
$${veryHighRiskInvestment.toLocaleString()}
${(veryHighRiskInvestment / totalInvestment * 100).toFixed(1)}% of portfolio
`;
}
// --- Detailed Analysis ---
function updateDetailedAnalysisTab() {
const container = document.getElementById('detailed-analysis-content');
container.innerHTML = '';
if (portfolio.length === 0) {
container.innerHTML = `
Add holdings to see detailed analysis.
`;
return;
}
portfolio.forEach(holding => {
const element = document.createElement('div');
element.classList.add('card');
element.innerHTML = `
${holding.name}
${holding.sector} - ${holding.country}
${holding.riskScore.toFixed(1)}
${getRiskLabel(holding.riskScore)} Risk
Investment: $${holding.investment.toLocaleString()}
Annual Water Consumption (Est.): ${holding.waterConsumption.toLocaleString()} m³
Key Risk Drivers: High baseline water stress, regulatory uncertainty.
Mitigation Factors: Public commitment to water stewardship (if applicable).
`;
container.appendChild(element);
});
}
// --- Mitigation & Reporting ---
function updateMitigationReportingTab() {
const container = document.getElementById('reporting-summary');
container.innerHTML = '';
if (portfolio.length === 0) {
container.innerHTML = `
Portfolio summary for the report will appear here.
`;
return;
}
const totalInvestment = portfolio.reduce((sum, h) => sum + h.investment, 0);
const portfolioRiskScore = portfolio.reduce((sum, h) => sum + (h.riskScore * h.investment), 0) / totalInvestment;
let tableHTML = `
Portfolio Summary
Overall Risk Score: ${portfolioRiskScore.toFixed(1)} (${getRiskLabel(portfolioRiskScore)})
| Company |
Sector |
Investment |
Risk Score |
`;
portfolio.forEach(h => {
tableHTML += `
| ${h.name} |
${h.sector} |
$${h.investment.toLocaleString()} |
${h.riskScore.toFixed(1)} |
`;
});
tableHTML += '
';
container.innerHTML = tableHTML;
}
async function generatePdf() {
const { jsPDF } = window.jspdf;
const pdfContent = document.getElementById('pdf-content');
// Temporarily make the pdf-content visible for rendering
const originalDisplay = pdfContent.style.display;
pdfContent.style.display = 'block';
try {
const canvas = await html2canvas(pdfContent, {
scale: 2, // Higher scale for better quality
backgroundColor: '#ffffff'
});
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({
orientation: 'p',
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 newImgWidth = pdfWidth - 20;
let newImgHeight = newImgWidth / ratio;
if (newImgHeight > pdfHeight - 20) {
newImgHeight = pdfHeight - 20;
newImgWidth = newImgHeight * ratio;
}
const x = (pdfWidth - newImgWidth) / 2;
const y = 10;
pdf.addImage(imgData, 'PNG', x, y, newImgWidth, newImgHeight);
pdf.save('Water_Risk_Assessment_Report.pdf');
} catch (error) {
console.error("Error generating PDF:", error);
alert("Could not generate PDF. See console for details.");
} finally {
// Restore original display property
pdfContent.style.display = originalDisplay;
}
}
// --- Helper Functions ---
function getRiskColor(score) {
if (score < 5) return '#2a9d8f'; // Low
if (score < 7.5) return '#e9c46a'; // Medium
if (score < 9) return '#f4a261'; // High
return '#e76f51'; // Very High
}
function getRiskClass(score) {
if (score < 5) return 'risk-low';
if (score < 7.5) return 'risk-medium';
if (score < 9) return 'risk-high';
return 'risk-very-high';
}
function getRiskLabel(score) {
if (score < 5) return 'Low';
if (score < 7.5) return 'Medium';
if (score < 9) return 'High';
return 'Very High';
}
// --- Start the application ---
init();
});