<!DOCTYPE html>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
meta
name
=
"viewport"
content="
width
=
device
-width,
initial-scale
=
1
.0">
<
title
>Bar Chart with Custom Background Color</
title
>
<
script
src
=
</
script
>
<
script
src
=
</
script
>
</
head
>
<
body
>
<
div
>
<
h1
style
=
"color:green;"
>
GeeksforGeeks
</
h1
>
<
h3
>
Chart.js Canvas background
Configuration - Color
</
h3
>
<
div
>
<
canvas
id
=
"barChart"
width
=
"400"
height
=
"400"
>
</
canvas
>
</
div
>
</
div
>
<
script
>
const data = {
labels: ['Java', 'Python', 'C++',
'JavaScript', 'C#', 'PHP'],
datasets: [{
label: 'Programming Languages',
data: [1, 2, 3, 4, 5, 6],
backgroundColor: [
'rgba(255, 99, 132, 0.5)',
'rgba(54, 162, 235, 0.5)',
'rgba(255, 205, 86, 0.5)',
'rgba(75, 192, 192, 0.5)',
'rgba(153, 102, 255, 0.5)',
'rgba(255, 159, 64, 0.5)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 205, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
};
const backgroundColorPlugin = {
id: 'customCanvasBackgroundColor',
beforeDraw: (chart) => {
const ctx = chart.ctx;
ctx.fillStyle = 'rgba(220, 220, 0, 0.5)';
ctx.fillRect(0, 0, chart.width, chart.height);
}
};
const config = {
type: 'bar',
data: data,
plugins: [backgroundColorPlugin],
options: {
scales: {
y: {
beginAtZero: true
}
}
}
};
let ctx = document.getElementById('barChart')
.getContext('2d');
let myBarChart = new Chart(ctx, config);
</
script
>
</
body
>
</
html
>