New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to add labels on top of each bar in a bar chart #605
Comments
Maybe this will help you scope.options = {
tooltips: {
enabled: true
},
hover: {
animationDuration: 1
},
animation: {
duration: 1,
onComplete: function () {
var chartInstance = this.chart,
ctx = chartInstance.ctx;
ctx.textAlign = 'center';
ctx.fillStyle = "rgba(0, 0, 0, 1)";
ctx.textBaseline = 'bottom';
this.data.datasets.forEach(function (dataset, i) {
var meta = chartInstance.controller.getDatasetMeta(i);
meta.data.forEach(function (bar, index) {
var data = dataset.data[index];
ctx.fillText(data, bar._model.x, bar._model.y - 5);
});
});
}
} |
Thanks @lfhenaol. |
var data = dataset.data[index]+" %"; |
Is there any way to show label in the bottom of negative bar and total data in the top in case of stacked bar? |
Something like this would work;
|
It's possible define color to text? |
ctx.fillStyle = 'red' |
Using chart.js 3.7.0, here's the code that worked for me (above code had to be modified slightly, likely due to updated versions of chart.js: animation: {
duration: 1,
onComplete: function() {
var chart = this;
var ctx = chart.ctx;
ctx.font = Chart.helpers.fontString(Chart.defaults.font.size, Chart.defaults.font.style, Chart.defaults.font.family);
ctx.textAlign = 'center';
ctx.textBaseline = 'bottom';
this.data.datasets.forEach(function(dataset, i) {
var meta = chart.getDatasetMeta(i);
meta.data.forEach(function(bar, index) {
var data = dataset.data[index];
ctx.fillText(data, bar.x, bar.y - 5);
});
});
}
}, |
Hello everyone,
I want to add labels at the top of each bar in my bar chart..
I found out that this is possible with this callback:
onAnimationComplete
But I have no idea how to use it and how to display the data without having a context or chart object (like in Chart.js).
If I use this code for example:
scope.options.onAnimationComplete = function() { console.log("hi")};
It wont even make the console log in the browser.. so it doesn't enter the function..
Does anyone have a solution for that?
The text was updated successfully, but these errors were encountered: