Please make the js function return an html code that will be added as innerHTML:…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Please make the js function return an html code that will be added as innerHTML:
.bar-labels{ display:flex;justify-content:space-between; margin-top:8px; } .bar-label{ flex:1;text-align:center;font-size:14px;color:#555; }
Here's the JavaScript function that returns the HTML code as a string, which can be used as innerHTML. I've wrapped the relevant parts into a function that returns the complete HTML structure, including styles and scripts:
function getBarChartHTML() {
return `
<div class="chart-container">
<div class="bar-chart" id="barChart"></div>
</div>
<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 .8s ease-out, background-color .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 .2s ease .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>
<script>
(function() {
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 container = chart.parentNode;
const labelRow = document.createElement('div');
labelRow.className = 'bar-labels';
container.appendChild(labelRow);
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);
});
})();
</script>
`;
}
// Example usage:
// document.getElementById('someElement').innerHTML = getBarChartHTML();
Key changes and notes:
The function getBarChartHTML() returns a template literal string containing all the necessary HTML, CSS, and JavaScript.
The JavaScript code is wrapped in an IIFE (Immediately Invoked Function Expression) to execute immediately when the HTML is added to the DOM.
The body styles are included but note that if you're adding this to an existing page, these body styles might affect your entire page. You might want to remove or scope these styles differently depending on your use case.
To use this, you would call it like:
document.getElementById('someElement').innerHTML = getBarChartHTML();
The chart will automatically initialize and animate when added to the DOM.
Make sure the container element where you're adding this HTML has appropriate dimensions and positioning in your existing page.
If you're using this in a larger application, you might want to:
This code will create the same animated bar chart as your original code, but now it's encapsulated in a function that returns HTML that can be used as innerHTML.