Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stacked column chart for two data sets - Google Charts

I'm generating some Google Charts and I'm stuck here. Google allows you to have your columns stacked. But it's either limited or I can't configure it to work. Taken from Google, here is an example showing number of cups of coffee produced in each year for two countries:

number of cups of BEAN COFFEE per year for Austria and Belgium

Say I have another data set for the same two countries, but this time I have instant coffee instead of ground. Example:

number of cups of INSTANT COFFEE per year for Austria and Belgium

What I'd like to do is to stack these two datasets on top of each other. So each column would be a single country, but two divisions: bean and instant coffee.

I was thinking if there was a way of formatting the data table in the following way:

['Year', 'Austria', 'Austria (instant)', 'Bulgaria', 'Bulgaria (instant')],
['2003', 1736060, 10051, 250361, 68564],
['2004', 1338156, 65161, 786849, 1854654],
['2005', 1276579, 65451, 120514, 654654]

to generate something like

enter image description here

Your help is appreciated.

like image 506
mavili Avatar asked Jul 29 '13 13:07

mavili


2 Answers

I just ran into this same issue today and followed your submission link. It seems that just recently someone replied with this:

"This is actually possible with the new Material Bar chart (albeit in a somewhat roundabout way). In the new chart, if you make a chart stacked, but place some series on a different axis, that creates a separate stack for those series. Unfortunately, there is currently no way to completely hide an axis yet, and you will have to explicitly set the view window. Eventually we will introduce options to hide axes and to align view windows, but this will have to do for now."

This fiddle seemed to help me solve this problem: http://jsfiddle.net/p7o0pjgg/

Here's the code:

google.load('visualization', '1.1', {
    'packages': ['bar']
});
google.setOnLoadCallback(drawStuff);

function drawStuff() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Topping');
    data.addColumn('number', 'Nescafe Instant');
    data.addColumn('number', 'Folgers Instant');
    data.addColumn('number', 'Nescafe Beans');
    data.addColumn('number', 'Folgers Beans');
    data.addRows([
        ['2001', 321, 621, 816, 319],
        ['2002', 163, 231, 539, 594],
        ['2003', 125, 819, 123, 578],
        ['2004', 197, 536, 613, 298]
    ]);

    // Set chart options
    var options = {
        isStacked: true,
        width: 800,
        height: 600,
        chart: {
            title: 'Year-by-year coffee consumption',
            subtitle: 'This data is not real'
        },
        vAxis: {
            viewWindow: {
                min: 0,
                max: 1000
            }
        },
        series: {
            2: {
                targetAxisIndex: 1
            },
            3: {
                targetAxisIndex: 1
            }
        }
    };

    // Instantiate and draw our chart, passing in some options.
    var chart = new google.charts.Bar(document.getElementById('chart_div'));
    chart.draw(data, google.charts.Bar.convertOptions(options));
};
like image 71
Dan Hogan Avatar answered Sep 24 '22 05:09

Dan Hogan


The Visualization API does not support creating multiple column stacks per row of data. You can make a feature request to add support for this if you want.

like image 28
asgallant Avatar answered Sep 25 '22 05:09

asgallant