Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly use the chartjs datalabels plugin

I'm using Chart.js to create a bar char, I have to display the percentage on each bar, so I found the chartjs-plugin-datalabels, but I can't make it work, the documentation and the examples are not clear for me.

This is What I got:

HTML:

<canvas id="bar-chart" width="800" height="450"></canvas>

Javascript:

// Bar chart

var valuedata= [2478,5267,734,784,433];
var valuelabel=["Africa", "Asia", "Europe", "Latin America", "North America"];


var myBarChart = new Chart(document.getElementById("bar-chart"), {
    type: 'bar',
    data: {
      labels: valuelabel,
      datasets: [
        {
          label: "Population (millions)",
          backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
          data: valuedata,

        }
      ]
    },
    options: {
      legend: { display: false },
      title: {
        display: true,
        text: 'Predicted world population (millions) in 2050'
      },
      scales: {
    xAxes: [{
                gridLines: {
                    display:false
                }
            }],
    yAxes: [{
                gridLines: {
                    display:false,
                    drawBorder: false
                },
                ticks: {
                display: false
            }
            }]
    },
      plugins: {
                    datalabels: {
                        color: 'white',
                        display: function(context) {
                            console.log("Algo: "+context);
                            return context.dataset.data[context.dataIndex] > 15;
                        },
                        font: {
                            weight: 'bold'
                        },
                        formatter: function(value, context) {
                        return context.dataIndex + ': ' + Math.round(value*100) + '%';
                    }
                    }
                }
    }
});

myBarChart.update();

Supposedly, from what I get, the "plugin" inside option is the one that let the values to be display, but as I shown in my code above, doesn't work for me, surely I'm missing something but I don't know what, can somebody help me?

Thanks.

Fiddle:

https://jsfiddle.net/s64bq4sw/16/

like image 257
elunap Avatar asked Mar 20 '18 19:03

elunap


People also ask

How do I use Chartjs plugins?

Using pluginsconst plugin = { /* plugin implementation */ }; // chart1 and chart2 use "plugin" const chart1 = new Chart(ctx, { plugins: [plugin] }); const chart2 = new Chart(ctx, { plugins: [plugin] }); // chart3 doesn't use "plugin" const chart3 = new Chart(ctx, {}); Copied!

What is Chartjs plugin Datalabels?

Highly customizable Chart. js plugin that displays labels on data for any type of charts.


1 Answers

It was because of the version of the Chartjs I was using, this plugin requieres 2.7.0 or later.

like image 87
elunap Avatar answered Oct 27 '22 00:10

elunap