Darn, I thought the automation would be simple, when I use the below code 
to loop through the column's distinct values to create columns, I end up 
with all of the columns having the last column's values.  The bizarre thing 
is that the 'columns' array passed to 'setColumns' is exactly the same when 
inspected.

var columns = [0];
var columnNames = data.getDistinctValues(2);
for (var i=0; i<columnNames.length; i++) {
name = columnNames[i];
columns.push({
type:'number',
label: name,
calc: function (dt, row) {
return (dt.getValue(row, 1) == name) ? dt.getValue(row, 2) : null;
}
});
}
var view = new google.visualization.DataView(by_year_country);
view.setColumns(columns);

// (the following works fine, I've just included for completeness sake)
columns = [];
for (var i=1;i<columnNames.length+1;i++) {
columns.push({column:i, type: 'number', label: view.getColumnLabel(i), 
aggregation: google.visualization.data.sum});
}
var pivotedData = google.visualization.data.group(view, [0], columns);

Just thought I'd post in case I was missing something obvious.

Iain

On Friday, November 16, 2012 12:07:13 PM UTC, Iain wrote:
>
> Nice - I've written an implementation of that that works - now I just have 
> to figure out how to automate it as I won't know what the data will be - 
> some form of loop through the distinct values of the given column I expect.
>
> Any experience of the performance of this pivot function for real data 
> sets?
>
> Thanks for your help, really appreciated.
> Iain
>
> On Thursday, November 15, 2012 8:25:52 PM UTC, asgallant wrote:
>>
>> You need to pivot your data, so you end up with one data series for each 
>> country.  There isn't any support for pivots in the API, but I wrote a hack 
>> that shows you how to do a pivot manually: 
>> http://jsfiddle.net/asgallant/HkjDe/
>>
>> On Thursday, November 15, 2012 1:45:18 PM UTC-5, Iain wrote:
>>>
>>> Hi, hoping to get some help with DataViews.  My ultimate goal is to draw 
>>> Pie Charts and Column Charts form one DataTable - my understanding is that 
>>> I can create different DataViews from that DataTable to support this.
>>>
>>> I've created a DataTable with columns 'KGs', 'Year', 'Country', and 
>>> 'Material'.  I was able to create a pie chart that aggregated each 
>>> country's KG's by year using the following code:
>>>
>>> var by_year = google.visualization.data.group(dt, [1], [{'column': 0, 
>>> 'aggregation': google.visualization.data.sum, 'type': 'number'}]);
>>> var chart = new 
>>> google.visualization.PieChart(document.getElementById('chart_year'));
>>> chart.draw(by_year, options);
>>>
>>> I then wanted to create a Column Chart with KGs on the y-axis, Year on 
>>> the x-axis and Country in the Legend.  I grouped the data as follows:
>>>
>>> var by_year_country = google.visualization.data.group(dt, [1,2], 
>>> [{'column': 0, 'aggregation': google.visualization.data.sum, 'type': 
>>> 'number'}]);
>>>
>>> Then I tried to tell the API what was data and what was grouping with 
>>> the following code, which produced a Column Chart but one in which the 
>>> x-axis repeats the year for every country, and the legend has just one 
>>> entry with no label:
>>>
>>> var view = new google.visualization.DataView(by_year_country);
>>> view.setColumns([
>>> {sourceColumn:0, type:'string', role:'domain', label:'Year'},
>>> {sourceColumn:1, type:'string', role:'domain', label:'Country'},
>>> {sourceColumn:2, type:'number', role:'data'}
>>> ]);
>>> chart.draw(view, options);
>>>
>>> Can anyone provide any advice on how this is supposed to be implemented, 
>>> or the appropriate terminology upon which to seek an answer?
>>>
>>> Many thanks in advance,
>>> Iain
>>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Visualization API" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/google-visualization-api/-/HpUzeHVHjugJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-visualization-api?hl=en.

Reply via email to