<!DOCTYPE html>
<html>
<head>
<title>Chart.js with Tooltips Config</title>
<script src=
"https://cdn.jsdelivr.net/npm/chart.js@latest/dist/chart.min.js">
</script>
<script src=
"https://cdn.jsdelivr.net/npm/chart.js">
</script>
</head>
<body style="text-align: center;">
<h2 style="color: green">
GEEKSFORGEEKS Chart.js tooltip configuration
</h2>
<div style="width: 1000px">
<canvas id="myTooltip"></canvas>
</div>
<script>
const chart =
document.getElementById("myTooltip");
new Chart(chart, {
type: "line",
data: {
labels: ["January", "February",
"March", "April", "May"],
datasets: [
{
label: "Sales",
data: [20, 10, 40, 50, 30],
borderWidth: 2, // width of the line border
borderColor: "green", // color of the line border
},
],
},
options: {
plugins: {
tooltip: {
enabled: true,
callbacks: {
label: function (tooltipData) {
const labels =
tooltipData.dataset.label.toString();
const values =
tooltipData.dataset.data[tooltipData.dataIndex];
const result =
tooltipData.dataset.data.reduce(
(var1, var2) => var1 + var2,
0
);
const percentage =
((values / result) * 100).toFixed(2) + "%";
return `${labels}: ${values} (${percentage})`;
},
},
backgroundColor: "#9cbba1",
titleColor: "#042a0b",
bodyColor: "#042a0b",
titleFont: { weight: "bold" },
padding: 10,
cornerRadius: 10,
borderColor: "#042a0b",
borderWidth: "2",
},
},
},
});
</script>
</body>
</html>