<!DOCTYPE html>
<
html
>
<
head
>
<
title
>Prefix, Suffix in ChartJS</
title
>
<
script
src
=
</
script
>
<
script
src
=
</
script
>
<
style
>
* {
font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
}
</
style
>
</
head
>
<
body
>
<
div
>
<
h1
style
=
"color: green; text-align: center"
>GeeksforGeeks</
h1
>
<
h3
style
=
"text-align: center"
>
Prefix, Suffix for tooltip labels in Chart.js
</
h3
>
<
div
>
<
canvas
id
=
"chartID"
></
canvas
>
</
div
>
</
div
>
<
script
>
// Chart object
new Chart($("#chartID"), {
// Line chart
type: "line",
data: {
labels: ["I sem", "II sem", "III sem", "IV sem", "V sem", "VI sem"],
datasets: [
// Dataset 1
{
label: "Harry",
data: [55, 45, 60, 45, 89, 67],
borderColor: "green",
backgroundColor: "rgba(0, 128, 0, 0.5)",
pointRadius: 7,
tension: 0.2,
pointStyle: "circle",
},
// Dataset 2
{
label: "Hermione",
data: [99, 90, 95, 89, 99, 100],
borderColor: "blue",
backgroundColor: "rgba(0, 0, 255, 0.5)",
tension: 0.2,
pointRadius: 7,
pointStyle: "circle",
},
],
},
options: {
plugins: {
tooltip: {
enabled: true,
mode: "point",
position: "nearest",
callbacks: {
afterBody: (context) => {
return "\nThis is a tooltip to explain "+
"\nthe usage prefix and suffix.";
},
// Custom label callback function
label: function (context) {
// Prefix containing the student label
let prefix = `${context.dataset.label} got`;
// Suffix computed pased on their marks
let suffix =
`marks! : ${context.parsed.y >= 50 ? "PASS" : "FAIL"
}`;
// Add my custom prefix and suffix set
// context.parsed.y is the value of y-axis
let label = `${prefix} ${context.parsed.y} ${suffix}`;
return label;
},
},
},
},
},
});
</
script
>
</
body
>
</
html
>