Stacked and grouped column

author(s): Torstein Hønsi

by grant

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>

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

<figure class="highcharts-figure">
    <div id="container"></div>
    <p class="highcharts-description">
        Chart showing stacked columns with grouping, allowing specific series to
        be stacked on the same column. Stacking is often used to visualize
        data that accumulates to a sum.
    </p>
</figure>

CSS

.highcharts-figure, .highcharts-data-table table {
    min-width: 310px; 
    max-width: 800px;
    margin: 1em auto;
}

#container {
    height: 400px;
}

.highcharts-data-table table {
	font-family: Verdana, sans-serif;
	border-collapse: collapse;
	border: 1px solid #EBEBEB;
	margin: 10px auto;
	text-align: center;
	width: 100%;
	max-width: 500px;
}
.highcharts-data-table caption {
    padding: 1em 0;
    font-size: 1.2em;
    color: #555;
}
.highcharts-data-table th {
	font-weight: 600;
    padding: 0.5em;
}
.highcharts-data-table td, .highcharts-data-table th, .highcharts-data-table caption {
    padding: 0.5em;
}
.highcharts-data-table thead tr, .highcharts-data-table tr:nth-child(even) {
    background: #f8f8f8;
}
.highcharts-data-table tr:hover {
    background: #f1f7ff;
}

JavaScript

var json = [
{"firstname":"Grant","year":"2016","rooster":"7","miss":"4","quail":"0"},
{"firstname":"Grant","year":"2017","rooster":"15","miss":"10","quail":"3"},
{"firstname":"Grant","year":"2018","rooster":"2","miss":"5","quail":"1"},
{"firstname":"Grant","year":"2019","rooster":"2","miss":"1","quail":"1"},
{"firstname":"Jim","year":"2016","rooster":"5","miss":"2","quail":"0"},
{"firstname":"Jim","year":"2017","rooster":"12","miss":"2","quail":"1"},
{"firstname":"Jim","year":"2018","rooster":"2","miss":"1","quail":"0"}
];
                
$(function () {


var year = [], categories = [],  firstname = [], rooster = [], miss = [], quail = [];
for(var i in json){
categories.push(json[i].year);
   //var firstname = (json[i].firstname);
   firstname.push(json[i].firstname);
   rooster.push(parseInt(json[i].rooster));
   miss.push(parseInt(json[i].miss));
   quail.push(parseInt(json[i].quail));
   year.push(parseInt(json[i].year));
}
console.log(categories);
console.log(rooster);
console.log(year);
console.log(firstname);
//get just one instance of each in array
Array.prototype.unique = function() {
    return Array.from(new Set(this));
}
oneYear = year.unique();
console.log(oneYear);
const last = a => a[a.length - 1];
console.log(last(year));
const end = last(year);
const first = year[0];
console.log(first);


Highcharts.chart('container', {

    chart: {
        type: 'column'
    },

    title: {
        text: 'Total fruit consumtion, grouped by gender'
    },

    xAxis: {
        categories: categories
        //['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
    },

    yAxis: {
        allowDecimals: false,
        min: 0,
        title: {
            text: 'Number of fruits'
        }
    },

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

    plotOptions: {
        column: {
           ...