cmill
cmill

Reputation: 891

Google Chart How can I pass variables to a DataView calculated column function?

According to Google a DataView can be used to calculated column values on the fly. (see documentation)

var myValue = 100;

var view = new google.visualization.DataView(data);
    
view.setColumns([
  data.getColumnIndex('PART'),
  data.getColumnIndex('VALUE'), 
  { calc:  exampleFromGoogle, type: 'number'},
  { calc:  customWithExternalValues, type: 'number'},
]);

The google calc example does not do not use any outside values and is self contained.

function exampleFromGoogle(dataTable, rowNum) {     
    return Math.floor(dataTable.getValue(rowNum, 1) / 2.54);
}

I would like to pass the calc function a variable myValue but I do not know the syntax where the calculation function is called. { calc: customWithExternalValues, type: 'number'}

This is the function I wish to use.

function customWithExternalValues(dataTable, rowNum, myValue) { 
    return (Math.floor(dataTable.getValue(rowNum, 1) / 2.54))* myValue;
}

How can I accomplish this?

Any ideas are greatly appreciated...Thanks!

Upvotes: 3

Views: 324

Answers (1)

WhiteHat
WhiteHat

Reputation: 61230

use the custom function within the example function...

var myValue = 100;

var view = new google.visualization.DataView(data);

view.setColumns([
  data.getColumnIndex('PART'),
  data.getColumnIndex('VALUE'), 
  { calc:  exampleFromGoogle, type: 'number'},
  { calc:  exampleFromGoogle, type: 'number'},
]);

function exampleFromGoogle(dataTable, rowNum) {
    return customWithExternalValues(dataTable, rowNum, myValue);
}

function customWithExternalValues(dataTable, rowNum, myValue) {
    return (Math.floor(dataTable.getValue(rowNum, 1) / 2.54)) * myValue;
}

Upvotes: 1

Related Questions