Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add additional data to google chart dataTable

I'm using google scatterchart to show where defects are on a surface. All defects have an ID, when I click on a point I want an event to fire from where I can get that ID and do other stuff with it.

In google chart one can wire an selecthandler from where I can get what's selected, but how do I add an ID (or any other data) in the dataTable to a point without it being displayed?

For example:

    var dataTable = new google.visualization.DataTable();
    dataTable.addColumn('number', 'Width');
    dataTable.addColumn('number', 'Yellow');
    dataTable.addColumn({ type: 'string', role: 'tooltip', 'p': { 'html': true } });
    dataTable.addColumn('number', 'Red');
    dataTable.addColumn({ type: 'string', role: 'tooltip', 'p': { 'html': true } });
    dataTable.addColumn('number', 'Id'); <-- this doesn't work

The last column I want to add the ID of the defect and retrieve it via the selectHandler.

dataTable.addRow([123, 123, 'some tooltip', null, null, 999]);

Here I added ID 999 to the table. But I don't want the chart to display it. How do I add additional (hidden) data to a point?

like image 424
Elger Mensonides Avatar asked Dec 25 '22 04:12

Elger Mensonides


2 Answers

setRowProperties(rowIndex, properties) would work in this case, remove Id column and add dataTable.setRowProperties(0,{'id':'999'});

You can get the property of the row by using getRowProperties(rowIndex);

like image 33
Edward Lee Avatar answered Jan 24 '23 17:01

Edward Lee


You can add extra columns containing any information you want, and then hide them with a DataView:

var dataTable = new google.visualization.DataTable();
dataTable.addColumn('number', 'Width');
dataTable.addColumn('number', 'Yellow');
dataTable.addColumn({ type: 'string', role: 'tooltip', 'p': { 'html': true } });
dataTable.addColumn('number', 'Red');
dataTable.addColumn({ type: 'string', role: 'tooltip', 'p': { 'html': true } });
dataTable.addColumn('number', 'Id');

// populate dataTable

var view = new google.visualization.DataView(dataTable);
// exclude column 5 (Id)
view.setColumns([0, 1, 2, 3, 4]);

Then draw your chart using the DataView instead of the DataTable.

like image 85
asgallant Avatar answered Jan 24 '23 16:01

asgallant