Is there a way to refresh Google Charts periodically in the background?

10,832 views
Skip to first unread message

Thomas Wang

unread,
May 18, 2012, 10:25:24 AM5/18/12
to google-visua...@googlegroups.com
Is there a way to refresh Google Charts periodically in the background?  Thanks.

asgallant

unread,
May 18, 2012, 11:55:31 AM5/18/12
to google-visua...@googlegroups.com
If you have your data pulled from an AJAX source (either custom or Google spreadsheets), then yes, you can update the data on whatever interval you choose.

Thomas Wang

unread,
May 18, 2012, 12:32:19 PM5/18/12
to google-visua...@googlegroups.com
My data source is a JSON file.  If yes, could you please tell me how to do that?  Thanks.

asgallant

unread,
May 18, 2012, 12:36:11 PM5/18/12
to google-visua...@googlegroups.com
What code are you using to fetch the data?

Thomas Wang

unread,
May 18, 2012, 2:08:11 PM5/18/12
to google-visua...@googlegroups.com
Here is my site: http://collectiveintel.info

My two js file are located at http://collectiveintel.info/js

Please take a look.  For now I need to press F5 to refresh, I want to reload it periodically like every 1 minute.  Thanks.

asgallant

unread,
May 18, 2012, 3:43:20 PM5/18/12
to google-visua...@googlegroups.com
Try using this as your main.js:

function loadCharts({
    // do these variables need to be in scope here? 
    var ctimecpocpccphcpl;
    load1();
    load2();
    
    function load1 ({
        $.getJSON("http://collectiveintel.info/json/tindex.json"function(json{
            buildChart(json);
            // call load1 after 60000ms (1 min)
            setTimeout(function ({
                load1();
            }60000);
        });
    }
    
    function load2 ({
        $.getJSON("http://collectiveintel.info/json/current.json"function(json{
            ctime json.current_hour[0]["Activity_Time"];
            cpo json.current_hour[0]["Price_Open"];
            cpc json.current_hour[0]["Price_Close"];
            cph json.current_hour[0]["Price_High"];
            cpl json.current_hour[0]["Price_Low"];
            
            document.getElementById("tdCurrentHighPrice").innerHTML cph;
            document.getElementById("tdCurrentOpenPrice").innerHTML cpo;
            document.getElementById("tdCurrentClosePrice").innerHTML cpc;
            document.getElementById("tdCurrentLowPrice").innerHTML cpl;
            document.getElementById("thCurrentActivityTime").innerHTML ctime " (GMT)";
            
            $.getJSON("http://collectiveintel.info/json/pivotp.json"function(json{
                buildPivot(jsonparseFloat(cpc));
                
                // call load2 after 60000ms (1 min)
                setTimeout(function ({
                    load2();
                }60000);
            });
        });

asgallant

unread,
Jun 4, 2012, 3:40:40 PM6/4/12
to google-visua...@googlegroups.com
What are you using for your data source?

On Monday, June 4, 2012 2:25:20 PM UTC-4, Takashi SASAKI wrote:
I'm using ChartWrapper.
When I call ChartWrapper#draw for the first time, it work and a chart shows.
But at the second call, the chart does not refresh.

setRefreshInterval works only if I use Google Spreadsheet as a data source.

How can I redraw charts with ChartWrapper?

Takashi SASAKI

unread,
Jun 4, 2012, 4:41:20 PM6/4/12
to google-visua...@googlegroups.com
I inspected (sniffed) server/client request and responses and found that
my data source always responses with "reqId" : "0" even if GVIZ script 
periodically calls with incrementing  reqId such as tqx=reqId%3A1, tqx=reqId%3A2 and so on.
My data source depends on GVIZ data source Python library
and that fixed reqId behavior is because of the gviz_api library.

I'm going to fix it but .. zzz ... just before the dawn ...

Takashi SASAKI

unread,
Jun 4, 2012, 4:55:25 PM6/4/12
to google-visua...@googlegroups.com
Wow, I was a fool.
In gviz_api, ToResponse function takes tqx parameter and processes it.
Reply all
Reply to author
Forward
0 new messages