Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highcharts: grouped columns with percentages

My question is similar to this one or this one, except that I don't have a simple series but groups of data.

Basically, I just want to have a chart with the behaviour of a "stacked percentage columns" chart, but without stacking the column.

Here is an example with absolute values (fiddle) :

var data = [
{
    name : 'A',
    data : [72, 50, 52]
},
{
    name : 'B',
    data : [23, 41, 12]
},
{
    name : 'C',
    data : [18, 9, 11]
},
{
    name : 'D',
    data : [89, 46, 54]
}];


// CHART

$('#container').highcharts(
{
    chart :
    {
        type : 'column'
    },
    xAxis :
    {
        categories : ['Group 1', 'Group 2', 'Group 3']
    },
    yAxis :
    {
        title :
        {
            text : null
        }
    },
    tooltip :
    {
        shared: true
    },
    plotOptions :
    {
        column :
        {
            dataLabels :
            {
                enabled : true
            }
        }
    },
    title :
    {
        text : 'Example'
    },
    series : data
});

In this example, I have three groups ("Group 1", "Group 2" and "Group 3") and four data ("A", "B", "C" and "D").

I would like to display the percentage of "A", "B", "C" and "D" for each group, and also I would like that percentage to be updated when I click on an item of the legend to hide/show a data (just like it works with stacked columns). Actually it's all like a "stacked percentage columns" chart, except that I don't want to stack the columns...

like image 223
nunivek Avatar asked Dec 03 '25 01:12

nunivek


1 Answers

Hi Checkout example here to resolve you issue.

http://jsfiddle.net/Evsw4/63/.

you can use formatter function to format the data label. Note that if a format is defined, the format takes precedence and the formatter is ignored.

API Ref : http://api.highcharts.com/highcharts#plotOptions.series.dataLabels.formatter

Code for formatter function to display percentage along with considering Group.

            dataLabels: {
                enabled: true,
                formatter: function () {
                    var mychart = $('#container').highcharts();
                    var mytotal = 0;

                    for (i = 0; i < mychart.series.length; i++) {
                        if (mychart.series[i].visible) {
                            mytotal += parseInt(mychart.series[i].yData[0]);
                        }
                    }
                    var pcnt = (this.y / mytotal) * 100;
                    return Highcharts.numberFormat(pcnt) + '%';
                }
            }

Full code:

var data = [{
    name: 'A',
    data: [72, 50, 52]
}, {
    name: 'B',
    data: [23, 41, 12]
}, {
    name: 'C',
    data: [18, 9, 11]
}, {
    name: 'D',
    data: [89, 46, 54]
}];
$('#container').highcharts({
    chart: {
        type: 'column'
    },
    xAxis: {
        categories: ['Group 1', 'Group 2', 'Group 3']
    },
    yAxis: {
        title: {
            text: null
        }
    },
    tooltip: {
        shared: true
    },
    plotOptions: {
        column: {
            dataLabels: {
                enabled: true
            }
        },
        series: {
            dataLabels: {
                enabled: true,
                formatter: function () {
                    var mychart = $('#container').highcharts();
                    var mytotal = 0;

                    for (i = 0; i < mychart.series.length; i++) {
                        if (mychart.series[i].visible) {
                            mytotal += parseInt(mychart.series[i].yData[0]);
                        }
                    }
                    var pcnt = (this.y / mytotal) * 100;
                    return Highcharts.numberFormat(pcnt) + '%';
                }
            }
        }
    },
    title: {
        text: 'Example'
    },
    series: data
});
like image 156
TechnoCrat Avatar answered Dec 05 '25 16:12

TechnoCrat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!