how to structure onResponse when using setRefreshInterval

181 views
Skip to first unread message

Hazer

unread,
Aug 4, 2009, 3:56:15 AM8/4/09
to Google Visualization API
Hi,

I have successfully created a PieChart which receives its DataTable
via the Query.send method. However, it does not fire this method after
the first time.

Query query = Query.create("http://localhost:8084/
FetchDataTable");
query.setRefreshInterval(5);
query.send(new Callback(){
public void onResponse(QueryResponse response) {
if (response.isError())
SC.say("An error occured: " +
response.getDetailedMessage());
else
{
dataTable = response.getDataTable();
pie = new PieChart(dataTable,
createPieChartOptions(300, 180, "My auto refresh pie chart"));
pie.addSelectHandler
(createPieChartSelectHandler(pie));
hLayout.addMember(pie);
}
}
});


I've got 2 questions really:
1) What is causing the above code to not fire the query.send every 5
secs?
2) This onResponse creates a new pie chart and adds it to hLayout upon
every successful response. I'm assuming the correct thing would be to
call pie.draw(dataTable, createPieChartOptions(300, 180, "My auto
refresh pie chart"))
I also assume I shouldn't be creating a new PieChart in the
onResponse, so it must already have been created, but how can I create
it AND add it to my DOM before I have my first dataTable?

Here's an attempt to create the "empty" pie chart beforehand:

pie = new PieChart();
pie.addSelectHandler(createPieChartSelectHandler
(pie));
hLayout.addMember(pie);

Query query = Query.create("http://localhost:8084/FetchDataTable");
query.setRefreshInterval(5);
query.send(new Callback(){
public void onResponse(QueryResponse response) {
if (response.isError())
SC.say("An error occured: " +
response.getDetailedMessage());
else
{
dataTable = response.getDataTable();
pie.draw(dataTable, createPieChartOptions
(300, 180, "My auto refresh pie chart"));
}
}
});


...but again, the query.send doesn't get called more than once. :(

MC Get Vizzy

unread,
Aug 9, 2009, 8:07:20 AM8/9/09
to google-visua...@googlegroups.com
regarding the refresh interval, is it possible that the underlying data has not changed?  if the data has not changed, your callback will not be called.

regarding the redraw, what you have should work -- let me know if you have further questions.

Hazer

unread,
Aug 11, 2009, 5:56:37 AM8/11/09
to Google Visualization API
Hi, thanks for the response.

But when you ask "has the underlying data changed", do you mean the
remote dataTable to be fetched? How could the client know if the data
has changed without sending the query at the refresh interval? Is
there some sort of implicit "hasDataChanged" request sent before
query.send is fired??

thanks in advance!

On Aug 9, 2:07 pm, MC Get Vizzy <getvi...@google.com> wrote:
> regarding the refresh interval, is it possible that the underlying data has
> not changed?  if the data has not changed, your callback will not be called.
> regarding the redraw, what you have should work -- let me know if you have
> further questions.
>

MC Get Vizzy

unread,
Aug 11, 2009, 7:33:06 AM8/11/09
to google-visua...@googlegroups.com
On Tue, Aug 11, 2009 at 12:56 PM, Hazer <alan....@gmail.com> wrote:

Hi, thanks for the response.

But when you ask "has the underlying data changed", do you mean the
remote dataTable to be fetched? How could the client know if the data
has changed without sending the query at the refresh interval? Is
there some sort of implicit "hasDataChanged" request sent before
query.send is fired??

the data source is queried, and if the data source responds that the underlying data has not changed, your callback is not called.

Hazer

unread,
Aug 13, 2009, 2:46:51 AM8/13/09
to Google Visualization API
I see. Thanks for that, so there must be code in the servlet which
handles this "hasTheDataChanged" query.

I have a Servlet which extends DataSourceServlet, so it only
implements one method: generateTable. This method builds a dataTable
object from a db backend. Is there another method I should be
overriding to get the desired effect of a servlet response which says
"Yes, the data has changed" or "No, the data is the same".

Thanks very much for your help.


On Aug 11, 1:33 pm, MC Get Vizzy <getvi...@google.com> wrote:

VizWiz

unread,
Aug 13, 2009, 4:17:32 AM8/13/09
to google-visua...@googlegroups.com
Hi,

I am not sure why you would like to change this behavior, however, there is currently no api for changing it.

The code that handles this is in JsonRenderer.renderJsonResponse (lines 120-128). It is open source, so you can change it as you like for your own use and as long as it is in the confines of the code license.

Cheers,
VizWiz.

Hazer

unread,
Aug 13, 2009, 5:05:58 AM8/13/09
to Google Visualization API
Hi,

I'm not being very clear, sorry. Basically, all I want is to have my
charts update automatically. As you know, there's 2 methods talking to
each other here:

Client: Query.send(callback)

Servlet: generateTable(query, request)

The generateTable sends a DataTable back to the callback correctly the
first time. But nothing happens after this. Regardless of whether or
not the DataTable produced by generateTable has changed, I would still
expect the generateTable method to be called at the
query.setRefreshInterval (5 seconds).

I must be doing something wrong, because I can't see how the
query.send can poll if the DataTable has changed, since generateTable
is just a stupid method that returns a DataTable object whenever it is
called.

I don't even mind if the full dataTable is sent back to the callback
every 5 seconds, I just want to see that it updates in realtime.

Here are the important parts of my code:

Servlet:
public class FetchDataTable extends DataSourceServlet{
@Override
public DataTable generateDataTable(Query query, HttpServletRequest
request) throws DataSourceException {
System.out.println("DataTableGenerator: [" + new Date() + "] -
" + request.getRequestURL() + " - " + request.getQueryString());
DataTable dataTable = generator.buildDataTableFromDB();
return dataTable;
}
...
}

Client:
final Runnable onLoadCallback = new Runnable() {
public void run() {
final Visualization chart = ChartManager.buildChart
(chartType, width, height, title);
wrapper.addChild(chart);
Query query = Query.create("http://localhost:8084/
FetchDataTable?tqx=reqId:" + (Math.random() * 1000000)));
query.setRefreshInterval(5);
query.send(new Callback(){
public void onResponse(QueryResponse response) {
if (response.isError())
SC.say("An error occured: " +
response.getDetailedMessage());
else
{
chart.draw(response.getDataTable
(),ChartOptions.create(chartType, width, height, title));
wrapper.unmask();
}
}
});
}
};
// Load the visualization api, passing the onLoadCallback to
be called when loading is done.
ChartOnlineAPI.loadAPI(chartType, onLoadCallback);


On Aug 13, 10:17 am, VizWiz <viz...@google.com> wrote:
> Hi,
>
> I am not sure why you would like to change this behavior, however, there is
> currently no api for changing it.
>
> The code that handles this is in JsonRenderer.renderJsonResponse (lines
> 120-128). It is open source, so you can change it as you like for your own
> use and as long as it is in the confines of the code license.
>
> Cheers,
> VizWiz.
>

Hazer

unread,
Aug 17, 2009, 5:56:44 AM8/17/09
to Google Visualization API
Can somebody post a working example of a Query.send(callback) method,
and a servlet which returns a datatable, which has a working
RefreshInterval?

i.e. If the datatable returned is full of random data, then the chart
should refresh with the new random data at the set refresh interval...

Thanks

cmurtaugh

unread,
Oct 14, 2009, 2:33:25 PM10/14/09
to Google Visualization API
FWIW, I'm having the same problem, with similar code. I see the first
request to load data (both from the server point of view and in the
browser, using Firebug), but no subsequent requests. The
setRefreshInterval call seems to have no effect, though when I inspect
the query object using Firebug, I can see that the refresh interval
value has been set.

Does anyone have a working example?

Hazer

unread,
Dec 3, 2009, 1:13:20 PM12/3/09
to Google Visualization API
Hi there,

I left this unsolved, but revisited it yesterday. It really looks like
the setRefreshInterval does not work for Bar/Column/Pie Charts. I
removed the call and instead implemented a timer like so:

[code]
public static void refreshChart(final Visualization chart, final
String chartType, final int width, final int height, final String
title, final Query query)
{
Timer timer = new Timer() {
public void run()
{
query.send(new Callback(){
public void onResponse(QueryResponse response) {
if (response.isError())
SC.say("An error occured: " +
response.getDetailedMessage());
else
chart.draw(response.getDataTable(),
ChartOptions.create(chartType, width, height, title));
}
});
}
};
timer.scheduleRepeating(15000);
}
[/code]

And it updates the charts every 15secs perfectly with zero redraw
flickering fortunately.... the only problem is it reloads the data
even if there are no changes...

Hope this helps,
Alan
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages