Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a label above each bar in HighCharts

I'm trying to put a label within a bar chart in Highcharts. In my case above each bar which you can see here:

$(function () {
  var chart;
  $(document).ready(function() {
    chart = new Highcharts.Chart({

        chart: {
            renderTo: 'container',
            type: 'column'
        },

        title: {
            text: 'Indicator per Sex'
        },

        xAxis: {
           categories: [
                'Jan',
                'Fev',
                'Mar',
                'Apr',
                'May',
               'Jun',
               'Jul',
               'Aug',
               'Sep',
               'Oct',
               'Nov',
               'Dez'
           ]
        },

        yAxis: {
            allowDecimals: false,
            min: 0,
            title: {
                text: 'Consults'
            }
        },

        tooltip: {
            formatter: function() {
                return '<b>'+ this.x +'</b><br/>'+
                    this.series.name +': '+ this.y +'<br/>'+
                    'Total: '+ this.point.stackTotal;
            }
        },

        plotOptions: {
            column: {
                stacking: 'normal'
            }
        },

        series: [
        {
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 29.9, 71.5, 106.4, 129.2, 144.0, 10, 20],
        color: '#FF0011',
        stack: 0
    }, {
        data: [30, 176.0, 135.6, 148.5, 216.4, 29.9, 71.5, 106.4, 129.2, 144.0, 10, 20],
        color: '#3333FF',
        stack: 0
    }, {
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 29.9, 71.5, 106.4, 129.2, 144.0, 10, 20],
        color: '#FF0011',
        stack: 1
    }, {
        data: [30, 176.0, 135.6, 148.5, 216.4, 29.9, 71.5, 106.4, 129.2, 144.0, 10, 20],
        color: '#3333FF',
        stack: 1
    }, {
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 29.9, 71.5, 106.4, 129.2, 144.0, 10, 20],
        color: '#FF0011',
        stack: 2
    }, {
        data: [30, 176.0, 135.6, 148.5, 216.4, 29.9, 71.5, 106.4, 129.2, 144.0, 10, 20],
        color: '#3333FF',
        stack: 2
    }]
    });
  });
 });`

I tried, but when I add labels appear one label each bar. So, how do this?!

like image 699
PradoComp Avatar asked Dec 06 '22 08:12

PradoComp


1 Answers

add this to your series:

dataLabels: {
    enabled: true,
    rotation: 0,
    color: '#000000',
    backgroundColor: '#FFFFFF',
    align: 'center',
    x: 4,
    y: 0,
    style: {
        fontSize: '10px',
        fontFamily: 'Verdana, sans-serif'
    }

of course adjust as needed.. you can also add a formatter to add $ sign and commas for large numbers if you need it..

EDIT:
just saw it was bar chart..
you will need to add this to your plot options:

plotOptions: {
   bar: {
      dataLabels: {
          enabled: true
      }
   }
},

check out the bar chart demo as well as their api documentation

like image 98
kingkode Avatar answered Dec 08 '22 22:12

kingkode