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 a JavaScript function that returns the HTML code for the bar chart:
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}" style="height: ${(val / maxVal) * chartH}px;"></div>
</div>
`;
});
html += `
</div>
<div class="bar-labels">
`;
days.forEach((day) => {
html += `
<span class="bar-label">${day}</span>
`;
});
html += `
</div>
</div>
`;
return html;
}
// Example usage:
const chartContainer = document.getElementById('chart-container');
chartContainer.innerHTML = getBarChartHtml();
You can use this function to generate the HTML code for the bar chart and add it to your page using the innerHTML property.
Note that this function generates random data for the chart, so the chart will look different each time you call the function. If you want to use specific data, you can modify the function to accept an array of values as an argument.
Also, this function does not include the CSS styles for the chart. You will need to add those separately to your page.
Here is the complete HTML code with the JavaScript function:
<!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}" style="height: ${(val / maxVal) * chartH}px;"></div>
</div>
`;
});
html += `
</div>
<div class="bar-labels">
`;
days.forEach((day) => {
html += `
<span class="bar-label">${day}</span>
`;
});
html += `
</div>
</div>
`;
return html;
}
// Example usage:
const chartContainer = document.getElementById('chart-container');
chartContainer.innerHTML = getBarChartHtml();
</script>
</body>
</html>
This code will generate a bar chart with random data and add it to the page. The chart will be colored based on the values, with red indicating an increase, yellow indicating no change, and green indicating a decrease. The chart will also be animated, with the bars growing to their final height over time.