No data to display chart.
';
document.getElementById('hmitChartLegend').innerHTML = '';
document.getElementById('hmitDownloadPdfButton').style.display = 'none';
return;
}
hmit_displayDataTable(filteredData, selectedSeriesKeys);
hmit_displayDataChart(filteredData, selectedSeriesKeys);
document.getElementById('hmitDownloadPdfButton').style.display = 'block';
}
function hmit_displayDataTable(data, seriesKeysToDisplay) {
const table = document.getElementById('hmitResultsTable');
const thead = table.querySelector('thead');
const tbody = table.querySelector('tbody');
thead.innerHTML = '';
tbody.innerHTML = '';
const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
// Header Row
let headerRowHtml = '
| Date (Mon YYYY) | ';
seriesKeysToDisplay.forEach(key => {
headerRowHtml += `${window.hmiSeriesKeys[key]} | `;
});
headerRowHtml += '
|---|
';
thead.innerHTML = headerRowHtml;
// Data Rows
data.forEach(item => {
let rowHtml = `
| ${monthNames[item.month-1]} ${item.year} | `;
seriesKeysToDisplay.forEach(key => {
const value = item[key] !== undefined ? item[key] : 'N/A';
rowHtml += `${value} | `;
});
rowHtml += '
';
tbody.innerHTML += rowHtml;
});
}
// --- SVG Charting Logic (Adapted from GDP Monitor) ---
function hmit_displayDataChart(data, seriesKeysToDisplay) {
const chartContainer = document.getElementById('hmitChartContainer');
const legendContainer = document.getElementById('hmitChartLegend');
chartContainer.innerHTML = '';
legendContainer.innerHTML = '';
const margin = { top: 30, right: 40, bottom: 70, left: 50 }; // Increased bottom margin for date labels
const width = chartContainer.clientWidth - margin.left - margin.right;
const height = 400 - margin.top - margin.bottom;
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.setAttribute("width", chartContainer.clientWidth);
svg.setAttribute("height", 400);
svg.setAttribute("viewBox", `0 0 ${chartContainer.clientWidth} 400`);
svg.style.fontFamily = "'Segoe UI', Tahoma, Geneva, Verdana, sans-serif";
const chartG = document.createElementNS("http://www.w3.org/2000/svg", "g");
chartG.setAttribute("transform", `translate(${margin.left},${margin.top})`);
svg.appendChild(chartG);
const dates = data.map(d => new Date(d.year, d.month - 1));
let allValues = [];
seriesKeysToDisplay.forEach(key => {
allValues.push(...data.map(d => d[key]));
});
const minVal = 0; // HMI is typically 0-100
const maxVal = 100;
// Scales
const xScale = (date) => {
const dateIndex = dates.findIndex(d => d.getTime() === date.getTime());
if (dates.length <= 1) return width / 2;
return (dateIndex / (dates.length - 1)) * width;
};
const yScale = (value) => height - ((value - minVal) / (maxVal - minVal)) * height;
// Axes
const xAxisG = document.createElementNS("http://www.w3.org/2000/svg", "g");
xAxisG.setAttribute("transform", `translate(0,${height})`);
chartG.appendChild(xAxisG);
const xAxisLine = document.createElementNS("http://www.w3.org/2000/svg", "line");
xAxisLine.setAttribute("x1", 0); xAxisLine.setAttribute("x2", width);
xAxisLine.setAttribute("y1", 0); xAxisLine.setAttribute("y2", 0);
xAxisLine.setAttribute("stroke", "#333");
xAxisG.appendChild(xAxisLine);
// X-axis Ticks (show fewer labels if too many dates)
const monthNamesShort = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
const maxXTicks = Math.floor(width / 70); // Approx 1 tick every 70px
const tickIncrement = Math.max(1, Math.ceil(dates.length / maxXTicks));
dates.forEach((date, index) => {
if (index % tickIncrement === 0) {
const x = xScale(date);
const tick = document.createElementNS("http://www.w3.org/2000/svg", "line");
tick.setAttribute("x1", x); tick.setAttribute("x2", x);
tick.setAttribute("y1", 0); tick.setAttribute("y2", 5);
tick.setAttribute("stroke", "#333");
xAxisG.appendChild(tick);
const label = document.createElementNS("http://www.w3.org/2000/svg", "text");
label.setAttribute("x", x); label.setAttribute("y", 20);
label.setAttribute("text-anchor", "middle"); label.style.fontSize = "10px";
label.textContent = `${monthNamesShort[date.getMonth()]} '${String(date.getFullYear()).slice(-2)}`;
xAxisG.appendChild(label);
}
});
const xAxisLabel = document.createElementNS("http://www.w3.org/2000/svg", "text");
xAxisLabel.setAttribute("x", width / 2); xAxisLabel.setAttribute("y", height + margin.bottom - 25); // Adjust position
xAxisLabel.setAttribute("text-anchor", "middle"); xAxisLabel.style.fontSize = "12px"; xAxisLabel.textContent = "Date (Month 'YY)";
chartG.appendChild(xAxisLabel);
// Y-axis
const yAxisG = document.createElementNS("http://www.w3.org/2000/svg", "g");
chartG.appendChild(yAxisG);
const yAxisLine = document.createElementNS("http://www.w3.org/2000/svg", "line");
yAxisLine.setAttribute("x1", 0); yAxisLine.setAttribute("x2", 0);
yAxisLine.setAttribute("y1", 0); yAxisLine.setAttribute("y2", height);
yAxisLine.setAttribute("stroke", "#333");
yAxisG.appendChild(yAxisLine);
const numTicksY = 5; // e.g., 0, 25, 50, 75, 100 for HMI
for (let i = 0; i <= numTicksY; i++) {
const val = minVal + (i / numTicksY) * (maxVal - minVal);
const y = yScale(val);
const tick = document.createElementNS("http://www.w3.org/2000/svg", "line");
tick.setAttribute("x1", -5); tick.setAttribute("x2", 0);
tick.setAttribute("y1", y); tick.setAttribute("y2", y);
tick.setAttribute("stroke", "#333");
yAxisG.appendChild(tick);
const label = document.createElementNS("http://www.w3.org/2000/svg", "text");
label.setAttribute("x", -10); label.setAttribute("y", y + 4);
label.setAttribute("text-anchor", "end"); label.style.fontSize = "10px";
label.textContent = val.toFixed(0);
yAxisG.appendChild(label);
}
const yAxisLabel = document.createElementNS("http://www.w3.org/2000/svg", "text");
yAxisLabel.setAttribute("transform", "rotate(-90)");
yAxisLabel.setAttribute("x", -height / 2); yAxisLabel.setAttribute("y", -margin.left + 15);
yAxisLabel.setAttribute("text-anchor", "middle"); yAxisLabel.style.fontSize = "12px"; yAxisLabel.textContent = "HMI Value";
chartG.appendChild(yAxisLabel);
// Lines and Legend
const colors = ["#007bff", "#28a745", "#dc3545", "#ffc107", "#6f42c1"];
seriesKeysToDisplay.forEach((seriesKey, index) => {
const seriesDataPoints = data.map(d => {
const date = new Date(d.year, d.month - 1);
return { x: xScale(date), y: yScale(d[seriesKey]) };
}).filter(p => !isNaN(p.y)); // Filter out NaN points if data is missing for a series
if (seriesDataPoints.length === 0) return;
const points = seriesDataPoints.map(p => `${p.x},${p.y}`).join(" ");
const polyline = document.createElementNS("http://www.w3.org/2000/svg", "polyline");
polyline.setAttribute("points", points);
polyline.setAttribute("fill", "none");
polyline.setAttribute("stroke", colors[index % colors.length]);
polyline.setAttribute("stroke-width", 2);
chartG.appendChild(polyline);
seriesDataPoints.forEach(p => {
const circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
circle.setAttribute("cx", p.x);
circle.setAttribute("cy", p.y);
circle.setAttribute("r", 2.5);
circle.setAttribute("fill", colors[index % colors.length]);
chartG.appendChild(circle);
});
const legendItem = document.createElement('div');
legendItem.style.display = 'flex'; legendItem.style.alignItems = 'center';
const legendColorBox = document.createElement('span');
legendColorBox.style.display = 'inline-block'; legendColorBox.style.width = '12px'; legendColorBox.style.height = '12px';
legendColorBox.style.backgroundColor = colors[index % colors.length]; legendColorBox.style.marginRight = '5px';
legendItem.appendChild(legendColorBox);
legendItem.appendChild(document.createTextNode(window.hmiSeriesKeys[seriesKey]));
legendContainer.appendChild(legendItem);
});
chartContainer.appendChild(svg);
}
// --- PDF Download Logic ---
function hmit_downloadPDF() {
const tableSection = document.getElementById('hmitResultsTable');
const chartSVGContainer = document.getElementById('hmitChartContainer');
const legendSection = document.getElementById('hmitChartLegend');
let filtersHTML = "
Selected Filters & Options
";
const startM = document.getElementById('hmitStartMonth').selectedOptions[0].text;
const startY = document.getElementById('hmitStartYear').value;
const endM = document.getElementById('hmitEndMonth').selectedOptions[0].text;
const endY = document.getElementById('hmitEndYear').value;
filtersHTML += `- Date Range: ${startM} ${startY} to ${endM} ${endY}
`;
const selectedSeries = Array.from(document.querySelectorAll('.hmit-series-checkbox:checked')).map(cb => window.hmiSeriesKeys[cb.value]);
filtersHTML += `- Displayed Series: ${selectedSeries.join(', ') || 'None'}
`;
filtersHTML += "
";
const tableHTML = tableSection ? `
Data Table
${tableSection.outerHTML}` : "
Table data not available.
";
let chartHTML = "
Data Chart
";
const svgElement = chartSVGContainer ? chartSVGContainer.querySelector('svg') : null;
if (svgElement) {
const svgClone = svgElement.cloneNode(true);
svgClone.setAttribute('width', '700');
svgClone.setAttribute('height', '400');
svgClone.style.maxWidth = '100%';
chartHTML += svgClone.outerHTML;
if (legendSection) {
chartHTML += `
Legend
${legendSection.innerHTML}
`;
}
} else {
chartHTML += "
Chart not available.
";
}
const currentDateTime = new Date().toLocaleString();
const latestValueText = document.getElementById('hmitLatestValueText')?.textContent || "N/A";
const pdfContent = `
Housing Market Index (HMI) Tracker Report
Generated: ${currentDateTime}
Latest Available Index (Overall): ${latestValueText}
${filtersHTML}
${tableHTML}
${chartHTML}
This report uses a static, embedded dataset. Data is for informational purposes. Last data update in embedded set: April 2025.
`;
const pdfWindow = window.open('', '_blank');
if (pdfWindow) {
pdfWindow.document.write(pdfContent);
pdfWindow.document.close();
setTimeout(() => { pdfWindow.print(); }, 500);
} else {
alert("Popup blocked. Please allow popups for this site to download the report.");
}
}