Fixed x axis label bug

This commit is contained in:
Your Name
2025-10-18 10:01:51 -04:00
parent 50c20585cb
commit 0762bfbd1e

View File

@@ -271,10 +271,15 @@ class ASCIIBarChart {
output += yAxisPadding + ' +' + '-'.repeat(this.maxDataPoints) + '\n'; // TEMP: back to original length output += yAxisPadding + ' +' + '-'.repeat(this.maxDataPoints) + '\n'; // TEMP: back to original length
// Draw X-axis labels based on mode and format // Draw X-axis labels based on mode and format
let xAxisLabels = yAxisPadding + ' '; // Initial padding to align with X-axis let xAxisLabels = yAxisPadding + ' '; // Initial padding to align with X-axis
// Determine label interval (every 5 columns)
const labelInterval = 5;
// Generate all labels first and store in array
let labels = []; let labels = [];
for (let i = 0; i < this.maxDataPoints; i++) { for (let i = 0; i < this.maxDataPoints; i++) {
if (i % 5 === 0) { if (i % labelInterval === 0) {
let label = ''; let label = '';
if (this.useBinMode) { if (this.useBinMode) {
// For bin mode, show labels for all possible positions // For bin mode, show labels for all possible positions
@@ -286,23 +291,27 @@ class ASCIIBarChart {
label = elapsedSec.toFixed(1) + 's'; label = elapsedSec.toFixed(1) + 's';
} else { } else {
// Show whole seconds for 1+ second bins // Show whole seconds for 1+ second bins
label = ' ' + String(Math.round(elapsedSec)) + 's'; label = String(Math.round(elapsedSec)) + 's';
} }
} else { } else {
// For legacy mode, show data point numbers // For legacy mode, show data point numbers
const startIndex = Math.max(1, this.totalDataPoints - this.maxDataPoints + 1); const startIndex = Math.max(1, this.totalDataPoints - this.maxDataPoints + 1);
label = String(startIndex + i).padStart(3, ' '); label = String(startIndex + i);
} }
labels.push({ position: i, text: label }); labels.push(label);
} }
} }
// Position all labels // Build the label string with calculated spacing
let currentPosition = yAxisPadding.length + 3; // Start after initial padding for (let i = 0; i < labels.length; i++) {
for (const labelInfo of labels) { const label = labels[i];
const spacesBefore = Math.max(0, labelInfo.position - currentPosition); xAxisLabels += label;
xAxisLabels += ' '.repeat(spacesBefore) + labelInfo.text;
currentPosition = labelInfo.position + labelInfo.text.length; // Add spacing: labelInterval - label.length (except for last label)
if (i < labels.length - 1) {
const spacing = labelInterval - label.length;
xAxisLabels += ' '.repeat(spacing);
}
} }
// Ensure the label line extends to match the X-axis dash line length // Ensure the label line extends to match the X-axis dash line length