Skip to content
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

Closed
thealein opened this issue Feb 20, 2017 · 8 comments
Closed

How to add labels on top of each bar in a bar chart #605

thealein opened this issue Feb 20, 2017 · 8 comments

Comments

@thealein
Copy link

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?

@lfhenaol
Copy link

lfhenaol commented Feb 20, 2017

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);

                            });
                        });
                    }
                }

@jtblin jtblin closed this as completed Apr 28, 2017
@masudshrabon
Copy link

Thanks @lfhenaol.
How to set '%' (eg., 5.5%) beside the value on each bar

@Rupesh87
Copy link

Rupesh87 commented Jan 4, 2019

var data = dataset.data[index]+" %";

@Sapkotaanish
Copy link

Sapkotaanish commented Aug 18, 2020

Is there any way to show label in the bottom of negative bar and total data in the top in case of stacked bar?

@thomasvt1
Copy link

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;

const offset = value >= 0 ? -5 : 20; // Calculate offset
...
ctx.fillText(data, bar._model.x, bar._model.y + offset);

@brunoromeo001
Copy link

It's possible define color to text?

@Magomed26
Copy link

Magomed26 commented Apr 5, 2021

It's possible define color to text?

ctx.fillStyle = 'red'

@pjrobertson
Copy link

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);
                              });
                            });
                          }
                        },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants