admin管理员组

文章数量:1291002

i am using Google charts to build stacked bar charts, created a listener to handler onclick event. there are three columns (site Name, Completed and Inplete). i want listener to capture whether user clicked pleted or inplete, when i click on the first row data this is what i am getting. i know in my getColumnLabel method i am getting all column names but i want that to be captured based upon what user clicked

//my datatable

        var data2 = new google.visualization.DataTable();

        data2.addColumn('string', 'Site Name');

        data2.addColumn('number', 'Completed');

        data2.addColumn('number', 'Inplete');

        data2.addRows([
                        [Site1, 12, 7],
                        [Site2, 10, 9],
                        [Site3, 15, 4],
                        [Site4, 10, 5]
                        ]);


//listener function 
    var selection = ChartName.getSelection();

            var row = selection[0].row;

            data2.getColumnLabel(0);  //givesme: SiteName

            data2.getColumnLabel(1); //givesme: Completed   

    data2.getColumnLabel(2) //givesme: Inpleted

            data2.getValue(row, 0); //givesme: Site1

            data2.getValue(row, 1); //givesme: 12

i am using Google charts to build stacked bar charts, created a listener to handler onclick event. there are three columns (site Name, Completed and Inplete). i want listener to capture whether user clicked pleted or inplete, when i click on the first row data this is what i am getting. i know in my getColumnLabel method i am getting all column names but i want that to be captured based upon what user clicked

//my datatable

        var data2 = new google.visualization.DataTable();

        data2.addColumn('string', 'Site Name');

        data2.addColumn('number', 'Completed');

        data2.addColumn('number', 'Inplete');

        data2.addRows([
                        [Site1, 12, 7],
                        [Site2, 10, 9],
                        [Site3, 15, 4],
                        [Site4, 10, 5]
                        ]);


//listener function 
    var selection = ChartName.getSelection();

            var row = selection[0].row;

            data2.getColumnLabel(0);  //givesme: SiteName

            data2.getColumnLabel(1); //givesme: Completed   

    data2.getColumnLabel(2) //givesme: Inpleted

            data2.getValue(row, 0); //givesme: Site1

            data2.getValue(row, 1); //givesme: 12
Share Improve this question edited Mar 20, 2014 at 15:43 GROVER_SYAAN asked Mar 20, 2014 at 15:33 GROVER_SYAANGROVER_SYAAN 3651 gold badge6 silver badges18 bronze badges 5
  • Does selection[0] have a column attribute? reference – mgilson Commented Mar 20, 2014 at 15:38
  • selection[0] is it not the selected row? not too sure. – GROVER_SYAAN Commented Mar 20, 2014 at 16:04
  • selection[0] is it not the selected row? not too sure. i want to get the column name that the user has selected data from so in my example i want to know if those numbers are ing from pleted or inplete so instead of saying data2.getColumnLabel(1) for pleted and data2.getColumnLabel(2) for Inplete it should be based upon the index of the column – GROVER_SYAAN Commented Mar 20, 2014 at 16:12
  • A word of caution: before you call selection[0].row, you should check to make sure that an element was selected in the first place, because deselecting an element also fires the select event, so selection might be a zero-length array. – asgallant Commented Mar 20, 2014 at 16:59
  • your question is irrelevant to my problem, but your code helped me solve my problem!! thanks! – AndrewBramwell Commented May 5, 2016 at 3:01
Add a ment  | 

1 Answer 1

Reset to default 12

i have managed to find the solution, for reference if anyone is interested

var ColName1 = data2.getColumnLabel(selection.column);

so selection.column property gives you the selected column from the datatable

本文标签: google visualizationJavascript getting Datatable column namesStack Overflow