Google Charts Nested PieChart returns column as null

732 views Asked by At

I have a nested PieChart that renders fine. I have added the "google.visualization.events.addListener" to get the selected data on click. I have a page with several charts and they all return values for row and column. My chart has two years of data "side by side" and I need to distinguish which set of data is being clicked by the user. Both sets of data return the rows but none return the columns. Here is the code I am using:

google.setOnLoadCallback(drawLocationChart);
function drawLocationChart() {

    var newData5 = google.visualization.arrayToDataTable(
    [["Location","Last 12mo Spend"],
    ["OMAHA, NE 68137",17916.2],
    ["SOUTHLAKE, TX 76092",12787.89],
    ["SUNNYVALE, CA 94085",7539.96],
    ["GREENVILLE, SC 29607",6627.21],
    ["TAMPA, FL 33634",3210.89]]);

    var oldData5 = google.visualization.arrayToDataTable(
    [["Location","Previous 12mo Spend"],
    ["OMAHA, NE 68137",17316.2],
    ["SOUTHLAKE, TX 76092",9787.89],
    ["SUNNYVALE, CA 94085",12539.96],
    ["GREENVILLE, SC 29607",9927.21],
    ["TAMPA, FL 33634",3821.89]]);

    var options5 = {
        colors: [
            '#9DB86D',
            '#C6A2BB',
            '#FFB03B',
            '#95AB63',
            '#737373',
            '#1aa2e5',
            '#6C4862',
            '#80551E',
            '#536820',
            '#3A3A3A'
        ],
        pieSliceText: 'none',
        responsive: true
    };

    var chartDiff5 = new google.visualization.PieChart(document.getElementById('location'));
    var diffData5 = chartDiff5.computeDiff(oldData5, newData5);
    var formatter5 = new google.visualization.NumberFormat({pattern:'$###,###'});
    formatter5.format(diffData5, 1);
    formatter5.format(diffData5, 2);

    chartDiff5.draw(diffData5, options5);

    google.visualization.events.addListener(chartDiff5, 'select', function selectHandler5() {
        var selection5 = chartDiff5.getSelection();

        for (var i5 = 0; i5 < selection5.length; i5++) {
            var item5 = selection5[i5];
            // Here diffData5.getFormattedValue(item5.column, 0) ALWAYS returns null
            var str5 = diffData5.getFormattedValue(item5.row, 0);
        }
    });
}

Any help will be greatly appreciated.

1

There are 1 answers

1
WhiteHat On

getSelection will always return null for column on Pie Charts

Because the user only has one pie slice to select.

Thus pie slice = row

Typically, only two columns of data are provided to a Pie Chart.

However, the data resulting from computeDiff contains three columns.

Column 0 = Label
Column 1 = Value from Table 1
Column 2 = Value from Table 2

As such, column is not needed for Pie Charts