I'm using a DataView object to only select specific columns from a
DataTable and draw then on a chart. I want to be able to create new columns
in the DataView that are functions of the values already in the DataView.
So far i have managed to produce a cumulative number from the code below
(copy into google playground
https://code.google.com/apis/ajax/playground/?type=visualization#table)
however i want to know the column id of the column created in the
dataview/pass the column id as well as the row number. For example I
ultimately want to be able to have this:
| Number 1 | Number 2 | Cumulative Number 1 | Cumulative
Number 2 | Other functions
| 4 | 3 | 4
| 3 |
| 2 | 1 | 6
| 4 |
| 1 | 4 | 7
| 8 |
| 0 | 2 | 7
| 10 |
| 3 | 1 | 10
| 11 |
| 5 | 0 | 15
| 11 |
> // Create and populate the data table.
> var data = google.visualization.arrayToDataTable([
> ['Name', 'Foo Distance', 'Bar Distance', 'Smokes'],
> ['Tong Ning mu', 4, 3, true],
> ['Huang Ang fa', 2, 6, false],
> ['Huang Ang fa', 1, 0, false],
> ['Huang Ang fa', 0, 2, false],
> ['Huang Ang fa', 3, 5, false],
> ['Huang Ang fa', 5, 7, false],
> ['Huang Ang fa', 8, 2, false],
> ['Huang Ang fa', 0, 1, false],
> ['Teng nu', 2, 0, true]
>
> ]);
>
> var dataview = new google.visualization.DataView(data);
>
> dataview.setColumns([1,2,{calc:cumulative, type:'number', label:'Foo ::
> Total Distance'}, {calc:cumulative, type:'number', label:'Bar :: Total
> Distance'}]);
>
> function cumulative(dataTable, rowNum){
> if(rowNum > 0){
> return (dataview.getValue(rowNum-1, 2) + dataTable.getValue(rowNum, 1)
> );
> }
> return dataTable.getValue(rowNum, 1);
> }
> // Create and draw the visualization.
> visualization = new google.visualization.Table(document.getElementById(
> 'table'));
> visualization.draw(dataview, null);
> }
>
function drawVisualization() {
The only way I could do this without column id is have a cumulative
function for each column, however this needs to be dynamic.
tl;dr Basically i need a way to pass the column id into the calc function
when calculating new columns.
i.e.
dataview.setColumns([1,2,{calc:
> cumulative(column_id_to_calc_cumulative_from), type:'number', label:'Foo
> :: Total Distance'}]);
--
You received this message because you are subscribed to the Google Groups
"Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/groups/opt_out.