I'm currently working on a Play! project that involves graphing JSON data similar to the following using the Google Visualization API:
[{"votes":4,"ID":123,"rating":50,"name":"Bob"}]
My current setup involves using jQuery $.getJSON
in the view requesting a URL that returns data with renderJSON()
. I then build the Google Visualization data table with .each
. This method works fine; however, my future data set could expand such that constructing the data table in this way would seem inefficient.
Ideally, I'd like to be able to pass the formatted JSON into the DataTable constructor or use google.visualization.Query
and skip the manual addition of the rows, but I'm stuck on how best to accomplish this. Looking at the options, it seems I can either try to format my JSON response similar to the example described in the documentation:
var dt = new google.visualization.DataTable(
{
开发者_如何学运维 cols: [{id: 'task', label: 'Task', type: 'string'},
{id: 'hours', label: 'Hours per Day', type: 'number'}],
rows: [{c:[{v: 'Work'}, {v: 11}]},
{c:[{v: 'Eat'}, {v: 2}]},
...
or I could attempt to work with the Visualization Data Source Library. Unfortunately, my Java knowledge is not evolved enough for me to determine how to start setting up either within Play! If someone could point me in the right direction, it would be much appreciated.
Thanks in advance.
P.S. I'm loving the Play! Framework. I never thought I would be able to accomplish so much with so little effort.
if you are not fluent in Java, don't go in the custom datasource direction because apparently, as often, Google provides a nice library but the code is stuck on Java servlets. Unfortunately, Play doesn't use java servlets (which is one reason of being of the framework) and to use Google libraries, you generally have to dig into the code to find the right classes to use.
So I would advise you to use a simple JSON generator creating in Ajax the {cols: ..., rows: ... } on Play side and persist your data using a simple persistence module such as Siena (ok I'm not objective I'm lead dev of siena :)) or JPA if you really want to stay near Java EE world.
You have to write a few code for generating your JSON (GSON is well doc) but it's quite trivial with a few work.
精彩评论