Ok, ty for the example. It's what I had in mind. I'm going to enter an enhancement request for the syntax I described.
I've personally been part of 3 different datatable implementations in 2 different languages, and we found that supporting this syntax allows one to do much more sophisticated aggregations. In certain application areas, such as finance, these types of aggregations are critical. Google's is the first I see in Javascript. It's nice, a little different. But I think it would benefit from some of the experience of other table implementations. Thanks for taking the time to post the example, I appreciate it. On Jun 29, 3:43 am, Viz Kid <[email protected]> wrote: > Hi > > I stated that this can be done using a DataView, but I did not say that it > would be as clean as you would like, especially satisfying your request to > use the general existing weighted average function as is. If the > WeightedAverage function is indeed what you wrote (I did not see any > normalization there so I wasn't sure), you can indeed get the desired > outcome using first a DataView to create the weighted columns [2,5,6,7] (by > multiplying their value by the weight) and then applying the group call as > you did it where replacing the WeightedAverage with the sum function. > > I agree that it would be more natural to have the syntax as you wrote it > available for this use case but currently it simply does not exist. > > Here is a snippet of the code: > > view = new google.visualization.DataView(table); > view.setColumns([ > 0, > 1, > {calc: weightedColumn(2, 4), type: 'number'}, > 3, > 4, > {calc: weightedColumn(5, 4), type: 'number'}, > {calc: weightedColumn(6, 4), type: 'number'}, > {calc: weightedColumn(7, 4), type: 'number'}]); > > bySector = new google.visualization.data.group(view, [1], > [ {column:0, aggregation:AllSameOrMany, type:'string'} > ,{column:2, aggregation:google.visualization.data.sum,type:'number'} > ,{column:3, aggregation:google.visualization.data.sum,type:'number'} > ,{column:4, aggregation:google.visualization.data.sum,type:'number'} > ,{column:5, aggregation:google.visualization.data.sum,type:'number'} > ,{column:6, aggregation:google.visualization.data.sum,type:'number'} > ,{column:7, aggregation:google.visualization.data.sum,type:'number'} > ] > ); > > function weightedColumn(dataColumnIndex, wightsColumnIndex) { > return function(dataTable, rowNum) { > return dataTable.getValue(rowNum, dataColumnIndex) > * dataTable.getValue(rowNum, weightsColumnIndex); > } > > } > > Best, > Viz Kid > > > > > > > > On Wed, Jun 29, 2011 at 5:04 AM, NA <[email protected]> wrote: > > VIz Kid, can you post that example? > > > On Jun 24, 9:58 pm, NA <[email protected]> wrote: > > > So can you present an example using DataView? I can't see a > > > straightforward way to do this, but I'll give you the benefit of the > > > doubt. Show me how you'd do the following: > > > > table has these columns: > > > > 0 1 2 3 4 5 6 7 > > > id, sector, price, shares, weight, f1, f2, f3 > > > > I want to aggregate this by sector. The aggregation functions for > > > price, f1, f2, and f3 is a weighted average. The aggregation for > > > shares and weight is a sum. The aggregation for id and sector is to > > > return the string "Many" if there are multiple values in that column, > > > or if all the entries are the same return that value. > > > > Such aggregation functions might look like: > > > > function WeightedAverage(q,w) { > > > var wsum = 0; > > > for (i=0;i<w.length;i++) {wsum+= w[i]*q[i];} > > > return wsum; > > > > } > > > > function AllSameOrMany(c) { > > > var r = c[0]; > > > for (var i=0;i<c.length;i++) {if (r !=c[i]){return 'Many';}}; > > > return r; > > > > } > > > > Note that the WeightedAverage function is a general function that > > > doesn't require the weight to always be in column 4. It also doesn't > > > know what Tables are. It's used in many places; its existence > > > predates the google visualization API. This is important for > > > reusability, maintainability, and interoperability across many > > > libraries. > > > > What I'd like to do is; > > > > bySector = new google.visualization.data.group(table,[1], > > > [ {column:0, aggregation:AllSameOrMany, type:'string'} > > > ,{column:[2,4],aggregation:WeightedAverage,type:'number'} > > > ,{column:3, aggregation:google.visualization.data.sum,type:'number'} > > > ,{column:4, aggregation:google.visualization.data.sum,type:'number'} > > > ,{column:[5,4], aggregation:WeightedAverage,type:'number'} > > > ,{column:[6,4], aggregation:WeightedAverage,type:'number'} > > > ,{column:[7,4], aggregation:WeightedAverage,type:'number'} > > > ] > > > ); > > > > Since this syntax doesn't exist, can you show me how to do this with > > > google.visualization.data.group or with DataViews, without having to > > > create special versions of my aggregation functions? > > > > In my case, I used currying to wrap functions like WeightedAverage in > > > a way to accommodate the grouping and DataView APIs. > > > > But I'd like to learn a cleaner way of doing this. > > > > thanks, > > > -- > > You received this message because you are subscribed to the Google Groups > > "Google Visualization API" group. > > 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. -- You received this message because you are subscribed to the Google Groups "Google Visualization API" group. 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.
