sorting ChartWrapper data?

269 views
Skip to first unread message

140Kit Team

unread,
Jul 27, 2011, 5:37:38 PM7/27/11
to Google Visualization API
Hey all,

I'm in a constant struggle with Google's visualization api, but I
really don't see anything better, so I'm coming to you all with my
bind. Let's say we have some data, like the number of tweets per
language for a set of tweets. In this case, our breakdown may look
something like:

+----------+-------+
| label | value |
+----------+-------+
| German | 21 |
| English | 448 |
| Spanish | 30 |
| French | 7 |
| Italian | 2 |
| Japanese | 1 |
+----------+-------+

So, I set up a service on my site that exposes this data in the
fashion I have seen to work with google charts, and a little piece of
code to access and draw() the graph. Altogether, the snippet would
look something like this pastie: http://pastie.org/2281512.

So, my question is pretty simple. I don't want to pre-sort everything
on the database end, as it would get particularly messing in
determining what to sort by (as the code on that level is very
abstracted from the business of displaying any particular graph). It
would be better to sort it once loaded in js, and spit the sorted
version out in the graph. How would one sort this particular graph by
something like value (the number of people per language)? Would it
just make more sense to load direct json like that located at
http://test.140kit.com/test.json, and then sort and display that? what
would the js look like for that?

As you can tell, i know some programming but I'm basically useless and
utterly defeated/hopeless when it comes to js. Any help would be very,
very welcome.

Thanks much.

Viz Kid

unread,
Jul 28, 2011, 9:22:21 AM7/28/11
to google-visua...@googlegroups.com

Hi

You can set the view which sorts the table inside the ChartWrapper: http://code.google.com/apis/chart/interactive/docs/reference.html#chartwrapperobject
If you have the DataTable locally available in javascript, you can just create a DataView over that table and sort it according to your preferences: http://code.google.com/apis/chart/interactive/docs/reference.html#DataView

Hope this helps,
  Viz Kid



--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To post to this group, send email to google-visua...@googlegroups.com.
To unsubscribe from this group, send email to google-visualizati...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-visualization-api?hl=en.


asgallant

unread,
Jul 28, 2011, 9:27:24 AM7/28/11
to google-visua...@googlegroups.com
AFAIK, there is no method of sorting data in a chartWrapper object, but you can sort dataTable objects.  Use AJAX to fetch your json and feed it to a dataTable, sort the dataTable, and pass it to your chartWrapper.  See my example here: http://pastie.org/2284486

The parameters for sorting are described in the #sort method of the dataTable object: http://code.google.com/apis/chart/interactive/docs/reference.html#DataTable

asgallant

unread,
Jul 28, 2011, 9:58:23 AM7/28/11
to google-visua...@googlegroups.com
How would you sort inside the chartWrapper?  The only way I can think of doing it would be like:

var wrap = new google.visualization.ChartWrapper(...);
wrap.setView();
var view = wrap.getView();
var sorted = view.getSortedRows(<sort criteria>);
view.setRows(sorted);
var viewJSON= view.toJSON();
wrap.setView(viewJSON);

which seems overly complicated to me, and may not even work at all.

Viz Kid

unread,
Jul 28, 2011, 10:15:41 AM7/28/11
to google-visua...@googlegroups.com

Actually, the view returned by getView of the ChartWrapper is only the json representation of the view and it doesn't contain the data. Therefore, the only option to sort the table is if you have it locally and use setRows with the getSortedRows of the table itself or have the data sorted in the server using the query language.

  Viz Kid

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.

asgallant

unread,
Jul 28, 2011, 11:24:34 AM7/28/11
to google-visua...@googlegroups.com
Yeah, that's what I thought.
Reply all
Reply to author
Forward
0 new messages