Highcharts Negative Stack Bar Chart

In the previous chapters, we learned how to setup highcharts library and how to create a chart with required configurations using highcharts library in our webpage. Now, we will learn how to create a stacked negative stack bar chart using highcharts library with examples.

Highcharts Negative Stack Bar Chart Example

Following is the example of creating a bar chart with negative stack by setting the required bar chart properties using highcharts library.

 

<html>

<head>

    <title>Highcharts Negative Stack Bar Chart</title>

    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

    <script src="https://code.highcharts.com/highcharts.js"></script>

    <script src="https://code.highcharts.com/modules/exporting.js"></script>

    <script type="text/javascript">

        $(function() {

            // Age categories

            var categories = [

                '0-4', '5-9', '10-14', '15-19',

                '20-24', '25-29', '30-34', '35-39', '40-44',

                '45-49', '50-54', '55-59', '60-64', '65-69',

                '70-74', '75-79', '80-84', '85-89', '90-94',

                '95-99', '100 + '

            ];

            Highcharts.chart('container', {

                chart: {

                    type: 'bar'

                },

                title: {

                    text: 'Population pyramid for Germany, 2018'

                },

                subtitle: {

                    text: 'Source: Population Pyramids</a>'

                },

                xAxis: [{

                    categories: categories,

                    reversed: false,

                    labels: {

                        step: 1

                    }

                }, { // mirror axis on right side

                    opposite: true,

                    reversed: false,

                    categories: categories,

                    linkedTo: 0,

                    labels: {

                        step: 1

                    }

                }],

                yAxis: {

                    title: {

                        text: null

                    },

                    labels: {

                        formatter: function() {

                            return Math.abs(this.value) + '%';

                        }

                    }

                },

                plotOptions: {

                    series: {

                        stacking: 'normal'

                    }

                },

                tooltip: {

                    formatter: function() {

                        return '<b>' + this.series.name + ', age ' + this.point.category + '</b><br/>' +

                            'Population: ' + Highcharts.numberFormat(Math.abs(this.point.y), 0);

                    }

                },

                series: [{

                    name: 'Male',

                    data: [-2.2, -2.1, -2.2, -2.4, -2.7, -3.0,

                           -3.3, -3.2, -2.9, -3.5, -4.4, -4.1,

                           -3.4, -2.7, -2.3, -2.2, -1.6, -0.6,

                           -0.3, -0.0, -0.0

                   ]

                }, {

                    name: 'Female',

                    data: [

                        2.1, 2.0, 2.1, 2.3, 2.6,

                        2.9, 3.2, 3.1, 2.9, 3.4,

                        4.3, 4.0, 3.5, 2.9, 2.5,

                        2.7, 2.2, 1.1, 0.6, 0.2,

                        0.0

                    ]

                }]

            });

        });

    </script>

</head>

<body>

    <div id="container" style="width: 100%; height: 400px;"> </div>

</body>

</html>

If you observe the above example, we created a bar chart with negative stack using highcharts library with required properties.

 

When we execute the above highcharts example, we will get the result like as shown below.

 

Highcharts Negative Stack Bar Chart Example Result

 

This is how we can create a negative stack bar chart using highcharts library with required properties.

Topics Covered