I understand you're having issues with the animation and labels not showing up properly. Let's fix both versions and ensure they work correctly with animations and labels. I'll provide two corrected versions:
Version 1: Fixed Left Version (With Animation and Labels)
This version fixes the animation and ensures labels are displayed correctly.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Weekly Bar Chart (Colored + Animated)</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: #f9fafb;
}
.chart-container {
width: 400px;
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
overflow: hidden;
}
.bar-chart {
display: flex;
justify-content: space-between;
align-items: flex-end;
height: 200px;
border-left: 2px solid #ccc;
border-bottom: 2px solid #ccc;
padding-top: 10px;
}
.bar-item {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
}
.bar {
width: 40px;
height: 0;
border-radius: 4px 4px 0 0;
transition: height 0.8s ease-out, background-color 0.3s ease;
}
.red { background: #ff2222; }
.yellow { background: #facc15; }
.green { background: #34d399; }
.bar.red:hover { background: #bb2626; }
.bar.yellow:hover { background: #eab308; }
.bar.green:hover { background: #059669; }
.bar-value {
font-size: 12px;
margin-bottom: 4px;
color: #333;
opacity: 0;
transition: opacity 0.2s ease 0.2s;
}
.bar-value.visible { opacity: 1; }
.bar-labels {
display: flex;
justify-content: space-between;
margin-top: 8px;
}
.bar-label {
flex: 1;
text-align: center;
font-size: 14px;
color: #555;
}
</style>
</head>
<body>
<div class="chart-container">
<div class="bar-chart" id="barChart"></div>
<div class="bar-labels" id="barLabels"></div>
</div>
<script>
function get_bar_chart() {
const days = ["Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"];
function randInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const data = days.map(() => randInt(80, 90));
const chart = document.getElementById('barChart');
const labelRow = document.getElementById('barLabels');
const maxVal = Math.max(...data);
const chartH = 200;
let prevVal = null;
data.forEach((val, i) => {
const colour = i === 0 ? 'red' : val > prevVal ? 'red' : val === prevVal ? 'yellow' : 'green';
prevVal = val;
const item = document.createElement('div');
item.className = 'bar-item';
const spanVal = document.createElement('span');
spanVal.className = 'bar-value';
spanVal.textContent = val;
const bar = document.createElement('div');
bar.className = `bar ${colour}`;
item.append(spanVal, bar);
chart.appendChild(item);
const lbl = document.createElement('span');
lbl.className = 'bar-label';
lbl.textContent = days[i];
labelRow.appendChild(lbl);
const finalH = (val / maxVal) * chartH + 'px';
setTimeout(() => {
bar.style.height = finalH;
spanVal.classList.add('visible');
}, 50 * i + 50);
});
}
// Call the function to initialize the chart
get_bar_chart();
</script>
</body>
</html>
Fixes in Version 1:
- Labels: Added a dedicated
<div class="bar-labels" id="barLabels"></div> in the HTML to ensure labels are injected correctly.
- Animation: Ensured the animation works by setting the initial
height: 0 in CSS and using setTimeout to animate the height to the final value.
Version 2: Fixed Right Version (With Output, Animation, and Labels)
This version fixes the issue of no output by ensuring the HTML is properly injected and animations work.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Weekly Bar Chart (Colored + Animated)</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: #f9fafb;
}
.chart-container {
width: 400px;
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
overflow: hidden;
}
.bar-chart {
display: flex;
justify-content: space-between;
align-items: flex-end;
height: 200px;
border-left: 2px solid #ccc;
border-bottom: 2px solid #ccc;
padding-top: 10px;
}
.bar-item {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
}
.bar {
width: 40px;
height: 0;
border-radius: 4px 4px 0 0;
transition: height 0.8s ease-out, background-color 0.3s ease;
}
.red { background: #ff2222; }
.yellow { background: #facc15; }
.green { background: #34d399; }
.bar.red:hover { background: #bb2626; }
.bar.yellow:hover { background: #eab308; }
.bar.green:hover { background: #059669; }
.bar-value {
font-size: 12px;
margin-bottom: 4px;
color: #333;
opacity: 0;
transition: opacity 0.2s ease 0.2s;
}
.bar-value.visible { opacity: 1; }
.bar-labels {
display: flex;
justify-content: space-between;
margin-top: 8px;
}
.bar-label {
flex: 1;
text-align: center;
font-size: 14px;
color: #555;
}
</style>
</head>
<body>
<div id="chart-container"></div>
<script>
function getBarChartHtml() {
const days = ["Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"];
const data = days.map(() => Math.floor(Math.random() * (90 - 80 + 1)) + 80);
const maxVal = Math.max(...data);
const chartH = 200;
let html = `
<div class="chart-container">
<div class="bar-chart" id="barChart">
`;
data.forEach((val, i) => {
const colour = i === 0 ? 'red' : val > data[i - 1] ? 'red' : val === data[i - 1] ? 'yellow' : 'green';
html += `
<div class="bar-item">
<span class="bar-value">${val}</span>
<div class="bar ${colour}"></div>
</div>
`;
});
html += `
</div>
<div class="bar-labels">
`;
days.forEach((day) => {
html += `
<span class="bar-label">${day}</span>
`;
});
html += `
</div>
</div>
`;
return html;
}
// Function to add animation after HTML is injected
function animateBars() {
const bars = document.querySelectorAll('.bar');
const values = document.querySelectorAll('.bar-value');
const data = Array.from(bars).map((_, i) => parseInt(values[i].textContent));
const maxVal = Math.max(...data);
const chartH = 200;
bars.forEach((bar, i) => {
const val = data[i];
const finalH = (val / maxVal) * chartH + 'px';
setTimeout(() => {
bar.style.height = finalH;
values[i].classList.add('visible');
}, 50 * i + 50);
});
}
// Example usage:
const chartContainer = document.getElementById('chart-container');
chartContainer.innerHTML = getBarChartHtml();
animateBars(); // Call animation after HTML is injected
</script>
</body>
</html>
Fixes in Version 2:
- Output Issue: The chart now appears because the HTML is correctly injected into the
chart-container div.
- Animation: Added an
animateBars function to handle animations after the HTML is injected. This ensures the bars animate from height: 0 to their final height.
- Labels: Labels are now correctly included in the generated HTML and displayed below the chart.
Key Differences Between the Two Versions
- Version 1 builds the chart dynamically using DOM manipulation (
createElement, appendChild, etc.) and is more suitable if you need to update the chart dynamically without regenerating the entire HTML.
- Version 2 generates the chart as a string of HTML and injects it using
innerHTML. This is simpler for static charts but requires a separate animation step after injection.
How to Test
- Copy and paste either version into an HTML file.
- Open the file in a browser.
- You should see a bar chart with:
- Animated bars growing from the bottom.
- Values appearing above each bar.
- Day labels (Sat, Sun, etc.) below the chart.
If you still encounter issues, please let me know, and I'll help debug further!