Show N/A in datalabels, when value is null - Javascript highcharts

Javascript examples for highcharts:Chart Label

Description

Show N/A in datalabels, when value is null

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"></script> 
      <script type="text/javascript">
$(function () {//from   www  .j a v  a  2  s  . c  o m
    $('#container').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: 'Historic World Population by Region'
        },
        subtitle: {
            text: 'Source: Wikipedia.org'
        },
        xAxis: {
            categories: ['Africa', 'America', 'Asia', 'Europe', 'Oceania'],
            title: {
                text: null
            }
        },
        yAxis: {
            min: 0,
            title: {
                text: 'Population (millions)',
                align: 'high'
            },
            labels: {
                overflow: 'justify'
            }
        },
        tooltip: {
            valueSuffix: ' millions'
        },
        plotOptions: {
            bar: {
                dataLabels: {
                    enabled: true
                }
            }
        },
        legend: {
            layout: 'vertical',
            shadow: true
        },
        credits: {
            enabled: false
        },
        series: [{
            name: 'Year 1800',
            data: [107, null, 635, 203, 2],
            dataLabels: {
                formatter: function () {
                    if (this.y == null) {
                        var chart = this.series.chart,
                            categoryWidth = chart.plotWidth / chart.xAxis[0].categories.length,
                            offset = (this.point.x) * categoryWidth + categoryWidth / 2,
                            text = chart.renderer.text('N/A', -999, -999).add();
                        text.attr({
                            x: chart.plotLeft + offset - text.getBBox().width / 2, //center label
                            y: chart.plotTop + chart.plotHeight - 8 // padding
                        });
                    } else {
                        return this.y;
                    }
                },
                enabled: true,
                overflow: 'none',
                style: {
                    fontSize: '13px',
                    fontFamily: 'Verdana, sans-serif',
                    fontWeight: 'normal',
                    textShadow: '0'
                }
            }
        }]
    });
});

      </script> 
   </head> 
   <body> 
      <script src="https://code.highcharts.com/highcharts.js"></script> 
      <script src="https://code.highcharts.com/modules/exporting.js"></script> 
      <div id="container" style="min-width: 310px; max-width: 800px; height: 400px; margin: 0 auto"></div>  
   </body>
</html>

Related Tutorials