<!DOCTYPE html>
<html lang="en">
<head>
<title>Custom Bar Chart</title>
<script src=
"https://cdn.jsdelivr.net/npm/chart.js">
</script>
</head>
<body>
<canvas id="myBarChart"
width="550px" height="550px">
</canvas>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded',
function () {
class CustomBarController extends Chart.controllers.bar {
draw() {
super.draw(arguments);
const meta = this.getMeta();
const ctx = this.chart.ctx;
meta.data.forEach((bar) => {
const { x, y, width, height } =
bar.getProps(['x', 'y', 'width', 'height']);
ctx.save();
ctx.strokeStyle = 'red';
ctx.lineWidth = 1;
ctx.strokeRect(x, y, width, height);
ctx.restore();
});
}
}
CustomBarController.id = 'customBar';
CustomBarController.defaults =
Chart.controllers.bar.defaults;
// Register the custom controller
Chart.register(CustomBarController);
const data = {
labels: ['A', 'B', 'C', 'D', 'E'],
datasets: [{
label: 'Custom Bar Chart',
data: [3, 5, 2, 8, 1],
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 2
}]
};
// Chart options
const options = {
scales: {
x: {
type: 'category',
labels: ['A', 'B', 'C', 'D', 'E']
},
y: {
type: 'linear',
position: 'left'
}
}
};
// Create and use the custom chart type
const ctx = document
.getElementById('myBarChart').getContext('2d');
new Chart(ctx, {
type: 'customBar',
data: data,
options: options
});
});
</script>
</body>
</html>