0

I want to make a stacked column chart with age intervals.
I have a two-dimensional array containing an age key and a value.
The idea is: every single age is stacked on top of each other within their interval, and thereby represent the accumulated number of people within the age interval in a single column.

I want to approach this solution with highcharts' stacked column chart example.

This is my code so far:

// Age stacked column chart.
        var ageData = [
            [0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 4], [9, 3],
            [10, 24], [11, 33], [12, 31], [13, 21], [14, 61], [15, 42], [16, 43], [17, 87], [18, 64], [19, 120],
            [20, 134], [21, 142], [22, 184], [23, 221], [24, 234], [25, 211], [26, 198], [27, 94], [28, 79], [29, 34],
            [30, 24], [31, 27], [32, 32], [33, 21], [34, 4], [35, 7], [36, 11], [37, 5], [38, 3], [39, 6],
            [40, 3], [41, 4], [42, 13], [43, 6], [44, 4], [45, 3], [46, 1], [47, 2], [48, 2], [49, 0],
            [50, 0], [51, 34]]
        $('#container_stackedColumn').highcharts({
            chart: {
                type: 'column'
            },
            title: {
                text: 'User age distribution'
            },
            xAxis: {
                categories: [
                    '0-12',
                    '13-18',
                    '19-23',
                    '24-28',
                    '29-35',
                    '36-42',
                    '43-50',
                    '>50'
                ]
            },
            yAxis: {
                min: 0,
                title: {
                    text: 'Number of users'
                },
                stackLabels: {
                    enabled: true,
                    style: {
                        fontWeight: 'bold',
                        color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                    }
                }
            },
            plotOptions: {
                column: {
                    stacking: 'normal',
                    dataLabels: {
                        enabled: true,
                        color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
                        style: {
                            textShadow: '0 0 3px black, 0 0 3px black'
                        }
                    }
                }
            },
            series: [{ /* --- What to do here?! --- */ }]
        });

So, with my two-dimensional array in consideration, how do I visualize this stacked column chart?

Any help is greatly appreciated. :)

Anfaje
  • 335
  • 4
  • 14
  • My initial concern here is that each age would have to be a series, meaning that every series would only have a single non-zero entry. That's not necessarily wrong, but it doesn't seem ideal. – Halvor Holsten Strand Jul 06 '14 at 11:40

2 Answers2

1

You need to itearate on you arrays, compare with the limits which you have (categories) and push to correct series array.

See the parser (http://jsfiddle.net/W6AFY/3/) hope that it is correct.

Sebastian Bochan
  • 37,348
  • 3
  • 49
  • 75
0

Or another solution, using different series for each part of bar: http://jsfiddle.net/s9qHH/48/

var ranges = [12, 18, 23, 28, 35, 42, 50, 1000],
    rangesLen = ranges.length,
    series = [];

$.each(ranges, function(i, e){
    series.push({ //create series' containers for legend
        id: e+'index',
        name: e
    });
});

$.each(ageData, function(i, e){
    var x = e[0],
        y = e[1],
        index = 0;

    while(ranges[index] < x){ // get category index
          index ++;
    }
    series.push({
        showInLegend: false,
        linkedTo: ranges[index] +'index', // link to container series
        data: [ [index, y] ]   //add point at specific category
    })

});

Still, I think that's Sebastian's solution is faster.

Paweł Fus
  • 44,795
  • 3
  • 61
  • 77