Google Charts/ Google Table, put tooltip on header row.

A strange problem was seen while trying to enhance google table to add tooltip like function on the header of the table. This problem has few steps, as I am putting down below.

 How to add tooltip like function on the column of the google table

Google data structures for charting work well for the tooltip for all other charts but when you try to put the tooltip in the header of a google table, no inbuilt mechanism works. To solve this we can use following method on either jquery 'hover' or 'mouseover' functions.This will

Javascript
function drawTable(jsondata){

var d = JSON.parse(jsondata);
var tooltip = d[0]; 

var table = new google.visualization.Table(document.getElementById('table_div'));
table.draw(tableData, {showRowNumber: false});

var mouseover = function () {
    var index = jQuery(this).index();
    $("#tooltip").html(tooltip[index]);
};
var mouseout = function () {
    $("#tooltip").html('');
};

 

$('.google-visualization-table-sorthdr').mouseover(mouseover).mouseout(mouseout);  

} 



where tooltip[] is an array of tooltip message for every header. I pullled my messages from jsondata variable.

 
HTML
  
     <body>
        <div id="table_div"></div>
        <div id="tooltip"></div>
    </body> 

Works good till someone hovers around, but as soon as clicked hover functionality ends 

 

This is little weird but as soon as one click on header the hover functionality is disabled, and no or stationary message is displayed in tooltip div. This is because as soon you click on header something in google code unbinds the functions, so you have to explicitly bind the hover functions back in case of the click event.

function drawTable(jsondata){

var d = JSON.parse(jsondata);
var tooltip = d[0];

var table = new google.visualization.Table(document.getElementById('table_div'));
table.draw(tableData, {showRowNumber: false});

var mouseover = function () {
    var index = jQuery(this).index();
    $("#tooltip").html(tooltip[index]);
};
var mouseout = function () {
    $("#tooltip").html('');
};

var mouseclick = function () {
    $('.google-visualization-table-sorthdr').bind("mouseover", mouseover);
    $('.google-visualization-table-sorthdr').bind("mouseout", mouseout);
};

$('.google-visualization-table-sorthdr').mouseover(mouseover).mouseout(mouseout);

$('.google-visualization-table-sorthdr').click(mouseclick);
} 

Works good till the first click breaks on the second click on header with above problem 

This is because we need to bind the click function again on click event. Else second time all the functions get unbind. So we need to create a loop where click event will unbind and we will bind the click function again with all other functions back to the event.

function drawTable(jsondata){

var d = JSON.parse(jsondata);
var tooltip = d[0];

var table = new google.visualization.Table(document.getElementById('table_div'));
table.draw(tableData, {showRowNumber: false});

var mouseover = function () {
    var index = jQuery(this).index();
    $("#tooltip").html(tooltip[index]);
};
var mouseout = function () {
    $("#tooltip").html('');
};

var mouseclick = function () {
    $('.google-visualization-table-sorthdr').bind("mouseover", mouseover);
    $('.google-visualization-table-sorthdr').bind("mouseout", mouseout);
    $('.google-visualization-table-sorthdr').bind("click", mouseclick);
};

$('.google-visualization-table-sorthdr').mouseover(mouseover).mouseout(mouseout);

$('.google-visualization-table-sorthdr').click(mouseclick);
}

 

Comments

Popular posts from this blog

Caused by: java.sql.SQLTimeoutException: ORA-01013: user requested cancel of current operation

HashiCorp Vault Integration with Ansible Etower using approle

utility to extract date from text with java