Disaster Management Dashboard

Disaster Management Dashboard

Disaster Overview

Track and manage various disaster incidents and their impact.

Total Incidents

0

Active Incidents

0

Total Affected Population

0

Total Estimated Damages ($)

$0

Current Incidents

Incidents by Type
Incidents by Status

Configure Disaster Incidents

Add, edit, or remove disaster incident entries.

Add/Edit Incident

ID Name Type Location Start Date Status Actions

Estimated Damages: $${i.estimatedDamages.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}

Notes: ${i.notes || 'N/A'}

${i.status} ${i.type} `; incidentCardsContainer.appendChild(card); }); } /** * Renders a bar chart of incidents by type using D3.js. */ function renderIncidentsByTypeChart() { if (!incidentsByTypeChartContainer) { console.error('Error: incidentsByTypeChartContainer not found.'); return; } // Clear previous chart, but keep the title div const chartSvg = incidentsByTypeChartContainer.querySelector('svg'); if (chartSvg) { chartSvg.remove(); } if (incidents.length === 0) { incidentsByTypeChartContainer.innerHTML = '
Incidents by Type

No incident data available.

'; return; } // Aggregate data by incident type const typeCounts = d3.rollup(incidents, v => v.length, d => d.type); const chartData = Array.from(typeCounts, ([type, count]) => ({ type, count })); // Sort by count descending chartData.sort((a, b) => b.count - a.count); const margin = { top: 20, right: 20, bottom: 40, left: 40 }; const width = incidentsByTypeChartContainer.clientWidth - margin.left - margin.right; const height = incidentsByTypeChartContainer.clientHeight - margin.top - margin.bottom - 30; // Account for title height const svg = d3.select(incidentsByTypeChartContainer) .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 const x = d3.scaleBand() .domain(chartData.map(d => d.type)) .range([0, width]) .padding(0.1); // Y scale const y = d3.scaleLinear() .domain([0, d3.max(chartData, d => d.count) * 1.2]) // 20% buffer .range([height, 0]); // Add X axis svg.append("g") .attr("class", "axis x-axis") .attr("transform", `translate(0,${height})`) .call(d3.axisBottom(x)) .selectAll("text") .style("text-anchor", "end") .attr("dx", "-.8em") .attr("dy", ".15em") .attr("transform", "rotate(-45)"); // Add Y axis svg.append("g") .attr("class", "axis y-axis") .call(d3.axisLeft(y).tickFormat(d3.format("d"))); // Integer ticks // Tooltip const tooltip = d3.select("body").append("div") .attr("class", "chart-tooltip") .style("opacity", 0); // Add bars svg.selectAll(".bar") .data(chartData) .enter().append("rect") .attr("class", "bar") .attr("x", d => x(d.type)) .attr("y", d => y(d.count)) .attr("width", x.bandwidth()) .attr("height", d => height - y(d.count)) .attr("fill", "steelblue") .on("mouseover", function(event, d) { d3.select(this).attr("fill", d3.rgb("steelblue").darker(0.5)); tooltip.transition() .duration(200) .style("opacity", .9); tooltip.html(`Type: ${d.type}
Count: ${d.count}`) .style("left", (event.pageX + 10) + "px") .style("top", (event.pageY - 28) + "px"); }) .on("mouseout", function(d) { d3.select(this).attr("fill", "steelblue"); tooltip.transition() .duration(500) .style("opacity", 0); }); } /** * Renders a pie chart of incidents by status using D3.js. */ function renderIncidentsByStatusChart() { if (!incidentsByStatusChartContainer) { console.error('Error: incidentsByStatusChartContainer not found.'); return; } // Clear previous chart, but keep the title div const chartSvg = incidentsByStatusChartContainer.querySelector('svg'); if (chartSvg) { chartSvg.remove(); } if (incidents.length === 0) { incidentsByStatusChartContainer.innerHTML = '
Incidents by Status

No incident data available.

'; return; } // Aggregate data by status const statusCounts = d3.rollup(incidents, v => v.length, d => d.status); const chartData = Array.from(statusCounts, ([status, count]) => ({ status, count })); const width = incidentsByStatusChartContainer.clientWidth; const height = incidentsByStatusChartContainer.clientHeight - 30; // Account for title height const radius = Math.min(width, height) / 2 - 20; // Adjusted radius const svg = d3.select(incidentsByStatusChartContainer) .append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("transform", `translate(${width / 2},${height / 2})`); const pie = d3.pie() .sort(null) .value(d => d.count); const arc = d3.arc() .innerRadius(0) .outerRadius(radius); // Define colors for statuses const statusColors = { 'Alert': '#dc3545', // Red 'Ongoing': '#ffc107', // Amber 'Contained': '#007bff', // Blue 'Resolved': '#28a745' // Green }; const color = d3.scaleOrdinal(Object.values(statusColors)); // Tooltip const tooltip = d3.select("body").append("div") .attr("class", "chart-tooltip") .style("opacity", 0); const arcs = svg.selectAll(".arc") .data(pie(chartData)) .enter().append("g") .attr("class", "arc"); arcs.append("path") .attr("d", arc) .attr("fill", d => color(d.data.status)) .on("mouseover", function(event, d) { d3.select(this).attr("fill", d3.rgb(color(d.data.status)).darker(0.5)); tooltip.transition() .duration(200) .style("opacity", .9); tooltip.html(`Status: ${d.data.status}
Count: ${d.data.count}
Percentage: ${(d.data.count / d3.sum(chartData, d => d.count) * 100).toFixed(1)}%`) .style("left", (event.pageX + 10) + "px") .style("top", (event.pageY - 28) + "px"); }) .on("mouseout", function(d) { d3.select(this).attr("fill", color(d.data.status)); // Restore original color tooltip.transition() .duration(500) .style("opacity", 0); }); // Add text labels arcs.append("text") .attr("transform", d => `translate(${arc.centroid(d)})`) .attr("dy", "0.35em") .text(d => `${d.data.status} (${(d.data.count / d3.sum(chartData, d => d.count) * 100).toFixed(0)}%)`) .style("text-anchor", "middle") .style("font-size", "10px") .style("fill", "white") .style("pointer-events", "none"); // Prevent text from blocking mouse events on arcs } // --- 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 incident cards const cardsCanvas = await html2canvas(incidentCardsContainer, { scale: 2, useCORS: true, logging: false }); const cardsImgData = cardsCanvas.toDataURL('image/png'); // Capture incidents by type chart const typeChartCanvas = await html2canvas(incidentsByTypeChartContainer, { scale: 2, useCORS: true, logging: false }); const typeChartImgData = typeChartCanvas.toDataURL('image/png'); // Capture incidents by status chart const statusChartCanvas = await html2canvas(incidentsByStatusChartContainer, { scale: 2, useCORS: true, logging: false }); const statusChartImgData = statusChartCanvas.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("Disaster Management 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("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 Incident Cards Image pdf.setFontSize(18); pdf.text("Current Incidents Overview", pdfWidth / 2, yOffset, { align: 'center' }); yOffset += 10; const cardsImgHeight = (cardsCanvas.height * (pdfWidth - 40)) / cardsCanvas.width; // Check if image fits on current page, add new page if not if (yOffset + cardsImgHeight > pdf.internal.pageSize.getHeight() - 20) { pdf.addPage(); yOffset = 40; // Reset yOffset for new page } pdf.addImage(cardsImgData, 'PNG', 20, yOffset, pdfWidth - 40, cardsImgHeight); yOffset += cardsImgHeight + 30; // Add Incidents by Type Chart Image pdf.setFontSize(18); pdf.text("Incidents by Type Chart", pdfWidth / 2, yOffset, { align: 'center' }); yOffset += 10; const typeChartImgHeight = (typeChartCanvas.height * (pdfWidth - 40)) / typeChartCanvas.width; // Check if image fits on current page, add new page if not if (yOffset + typeChartImgHeight > pdf.internal.pageSize.getHeight() - 20) { pdf.addPage(); yOffset = 40; // Reset yOffset for new page } pdf.addImage(typeChartImgData, 'PNG', 20, yOffset, pdfWidth - 40, typeChartImgHeight); yOffset += typeChartImgHeight + 30; // Add Incidents by Status Chart Image pdf.setFontSize(18); pdf.text("Incidents by Status Chart", pdfWidth / 2, yOffset, { align: 'center' }); yOffset += 10; const statusChartImgHeight = (statusChartCanvas.height * (pdfWidth - 40)) / statusChartCanvas.width; // Check if image fits on current page, add new page if not if (yOffset + statusChartImgHeight > pdf.internal.pageSize.getHeight() - 20) { pdf.addPage(); yOffset = 40; // Reset yOffset for new page } pdf.addImage(statusChartImgData, 'PNG', 20, yOffset, pdfWidth - 40, statusChartImgHeight); yOffset += statusChartImgHeight + 30; // Add a section for detailed incident data (tabular format) pdf.addPage(); pdf.setFontSize(18); pdf.text("Detailed Incident Information", pdf.internal.pageSize.getWidth() / 2, 40, { align: 'center' }); const tableData = incidents.map(i => [ i.id, i.name, i.type, i.location, i.startDate, i.endDate || 'N/A', i.status, i.affectedPopulation.toLocaleString('en-US'), `$${i.estimatedDamages.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`, i.notes || 'N/A' ]); pdf.autoTable({ head: [['ID', 'Name', 'Type', 'Location', 'Start Date', 'End Date', 'Status', 'Affected Pop.', 'Est. Damages ($)', 'Notes']], body: tableData, startY: 60, theme: 'grid', styles: { fontSize: 6, cellPadding: 2 }, // Smaller font for many columns headStyles: { fillColor: [242, 242, 242], textColor: [51, 51, 51], fontStyle: 'bold' }, alternateRowStyles: { fillColor: [251, 251, 251] }, margin: { top: 70, left: 5, right: 5 } // Adjust margins for more columns }); pdf.save('disaster_management_report.pdf'); }; // --- Event Listeners and Initial Render --- downloadPdfButton.addEventListener('click', downloadPdf); // Initial setup // Sort incidents by start date (descending) initially incidents.sort((a, b) => new Date(b.startDate) - new Date(a.startDate)); updateConfigTable(); renderDashboardMetrics(); renderIncidentCards(); renderIncidentsByTypeChart(); renderIncidentsByStatusChart(); updateNavigationButtons(); // Set initial button states });
Scroll to Top