<!DOCTYPE html>
<
html
>
<
head
>
<
title
>Example 2</
title
>
<
script
src
=
</
script
>
<
script
src
=
</
script
>
</
head
>
<
style
>
#chart1 {
display: inline-block;
position: relative;
width: 50%;
}
element {
display: block;
box-sizing: border-box;
height: 513.85px;
width: 514.567px;
}
</
style
>
<
body
>
<
h1
style
=
"color: green;"
>
GeeksforGeeks
</
h1
>
<
h3
>
Approach 2: Using
Chart.js Plugin
</
h3
>
<
canvas
id
=
"chart1"
style="display: block;
box-sizing: border-box;
height: 514px;
width: 514.667px;"
width
=
"772"
height
=
"771"
>
</
canvas
>
<
script
>
const data = {
labels:
["HTML", "React", "Java",
"Python", "JavaScript"],
datasets: [{
data: [20, 30, 15, 10, 25],
backgroundColor:
["#FFCE56", "#36A2EB", "#FF6384",
"#4CAF50", "#9C27B0"]
}]
};
const options = {
plugins: {
legend: true,
tooltip: {
callbacks: {
label: function (tooltipItem, data) {
const label =
data.labels[tooltipItem.index] || '';
const value =
data.datasets[0].data[tooltipItem.index];
return `${label}: ${value}%`;
}
}
},
textInside: {
text: "GeeksforGeeks",
color: 'green',
fontSize: 28
}
}
};
Chart.register({
id: 'textInside',
afterDatasetsDraw: function (chart, _) {
const ctx = chart.ctx;
const width = chart.width;
const height = chart.height;
const fontSize = options.plugins.textInside.fontSize;
ctx.font = fontSize + 'px Arial';
ctx.fillStyle = options.plugins.textInside.color;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
const text = options.plugins.textInside.text;
const textX = Math.round(width / 2);
const textY = Math.round(height / 2);
ctx.fillText(text, textX, textY);
}
});
const textChart =
new Chart(document.getElementById('chart1'), {
type: 'doughnut',
data: data,
options: options,
responsive: true
});
</
script
>
</
body
>
</
html
>