I have this very useful chart but my problem the client want percentage label must display I have attach image below of what what I want.
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
<body>
<canvas id="myChart" style="width:100%;max-width:600px"></canvas>
<script>
const xValues = ["Italy", "France", "Spain", "USA", "Argentina"];
const yValues = [55, 49, 44, 24, 15];
const barColors = ["red", "green","blue","orange","brown"];
new Chart("myChart", {
type: "bar",
data: {
labels: xValues,
datasets: [{
backgroundColor: barColors,
data: yValues
}]
},
options: {
legend: {display: false},
title: {
display: true,
text: "World Wine Production 2018"
}
}
});
</script>
</body>
</html>
2
Answers
To show the percentage on the top of each bar, you can use the datalabel plugin with your charts. It helps to add the Y-Axis actual count on the top of each bar. Even, you can modify its behavior too.
Giving an example link, from where you can also get a better understanding of it. [Video Link]
You can use
onComplete
hook to draw/write these values after chart animation is fully rendered. And then usectx.fillText()
to customise how you prefer.https://cdpn.io/pen/debug/gOJwWLb?authentication_hash=LQkExZebvzdA