<!-- script.js -->
const ctx = document.getElementById('myChart').getContext('2d');
// Sample data
const labels = ['January', 'February', 'March', 'April', 'May', 'June'];
const data = [10, 20, 30, 40, 50, 60];
const chart = new Chart(ctx, {
type: 'bar', // Can also be 'line', 'pie', etc.
data: {
labels: labels,
datasets: [{
label: 'Sales',
data: data,
backgroundColor: 'rgb(0, 255, 81)',
borderColor: 'rgba(54, 162, 235, 1)',
borderWidth: 1
}]
},
options: {
responsive: true,
plugins: {
legend: {
display: true
},
tooltip: {
enabled: true
}
},
scales: {
y: {
beginAtZero: true
}
},
// Custom plugin to add labels above bars
plugins: [{
id: 'customLabels',
afterDatasetsDraw: function (chart) {
const ctx = chart.ctx;
chart.data.datasets.forEach(function (dataset, i) {
const meta = chart.getDatasetMeta(i);
meta.data.forEach(function (bar, index) {
const data = dataset.data[index];
ctx.fillStyle = 'rgba(0, 0, 0, 1)'; // Label color
ctx.textAlign = 'center';
ctx.textBaseline = 'bottom';
ctx.font = 'bold 12px Arial';
ctx.fillText(data, bar.x, bar.y - 5); // Position the label
});
});
}
}]
}
});