Be careful hardcoding the reqId to 0.  It isn't always 0.  If the API
send you a non-zero reqId, and you return 0, nothing will render.

Also, this technique may not work when the data set you're serializing
is large and you don't want to hold it in memory (or perhaps you just
want to conserve as much server memory as possible).  In this case, it
might be useful to create a class that can stream the results out
directly from the data source without having to hold both the original
data set and JSON data structure in memory at once.  I have a class
that does this, but I'm not particularly happy with it, so am
reluctant to post.  But if anyone else has something like this, feel
free to jump in.




On Jul 11, 4:38 pm, Jim Brannon <[email protected]> wrote:
> I had the same issue, used Tellac's idea, but stripped out the
> unwanted quotes from the json string inside PHP as follows.
>
> I was creating a quick and dirty data source for the Annotated Time
> Line Visualization.  You can use Tellac's code with the following
> changes:
>
> For Annotated Time Line the first column needs to be of type
> "datetime":
>     $coltypes = array("datetime","number");
> Then when creating records in the data table, do it something like
> this:
>     $datatable["rows"][$ts_ndx]["c"][0]["v"]="new Date($y,$m,
> 1,0,0,0)";
>
> It has the quotes in it that confuse the GV library, so now just strip
> them out:
>     $json_data_table = json_encode($datatable);
>     $json_data_table = preg_replace('/\"new/','new',$json_data_table);
>     $json_data_table = preg_replace('/\)\"/',')',$json_data_table);
>     echo
> "google.visualization.Query.setResponse({version:'0.5',reqId:'0',status:'ok 
> ',table:".
> $json_data_table."});";
>
> Worked like a charm.  HTH.
>
> On May 29, 3:07 pm, Piyush Ohari <[email protected]> wrote:
>
>
>
>
>
>
>
> > Hello Team,
>
> > I am also facing the same problem, where I wish to transfer 'Date'
> > value (fetched from DB) in PHP, to "DataTable".
> > I could not find any way so as to do this.
>
> > Based on your questions below, here are my replies (in my case).
> > a. Using DataTable, and not Query
> > b. Using XHR
> > c. Passing Date from PHP as string works fine, but further usage of
> > Data type value is not longer possible
> > d. Here is code excerpt, where prob exists.
>
> > //Initial custom XHR
> >                 function trxImport (UI) {
> >                         var url = "/wms/php/getData.php"
> >                         _getData(url, handleResp, UI);
> >                 }
> > //AJAX Obj and Call
> > function _getData(url, resp_Handle, UI_handle) {
> >         var request = _getHTTPObject(); //NOTE: Object creation avoid for
> > length in forum
> >         if (request) {
> >                 request.onreadystatechange = function() {resp_Handle 
> > (request,
> > UI_handle)};
> >                 request.open( "GET", url, true );
> >                 request.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 
> > 1900
> > 00:00:00 GMT");
> >                 request.send(null);
> >                 return true;
> >         } else {
> >                 return false;
> >         }
>
> > }
>
> > //Visualization Datatable
> >                 function drawVisualization(resp, UI) {
> >                         var JSONObject = (new Function("return " + resp))() 
> > ;
> >                         var data = new 
> > google.visualization.DataTable(JSONObject, 0.5);
>
> >                         // Create and draw the visualization.
> >                         var table = new
> > google.visualization.Table(document.getElementById(UI));
> >                         table.draw(data, {'allowHtml':
> > true,'page':'enable','width':'4800px'});
> > .....
>
> > PHP Code -- tried several ways to pass the Date DB value
> >                         elseif ($fields[$index]->type == 'date') {
> >                                 //Sample Date = 2010-06-28
> >                                 //$dt = date_parse ($field);
> >                                 //echo "{\"v\": \"new Date (", $dt['year'], 
> > ",", $dt['month'],
> > ",", $dt['day'], ")\"}";
> >                                 $dt = strtotime ($field);
> >                                 echo "{\"v\": \"\/new Date($dt)\/\"}";
> > Non of these form (though valid JSON), is acceptable to DataTable
>
> > I am open to give the exact code both for Page as well as PHP, if
> > required, please provide specific ID to mail those pages.
>
> > Appreciate, any work-around also for handling the scenario.
> > I am basic-to-intermediate level programmer, and could not follow the
> > suggestion in the previous update, it would be good to know exact
> > regex used above.
>
> > Thanks, Piyush
>
> > On May 12, 4:11 pm, MC Get Vizzy <[email protected]> wrote:
>
> > > I'll have a look at this.  It sounds like a bug, but it might be tricky to
> > > track down and fix.  Are you using the Query class, or just DataTable?  
> > > Are
> > > you using script injection or XHR?  Have you tried passing the JSON as a
> > > string to the DataTable constructor?  Can you send me a sample of your
> > > client-side code?
>
> > > thanks,
>
> > > MC Get Vizzy
>
> > > On Tue, May 10, 2011 at 5:41 AM, Teliac <[email protected]> wrote:
> > > > Thank you for the thoughts.  I had read that, and attempted to send a
> > > > string in that format.  However, when the column is set to type
> > > > "string", the string displays as received -> Date(2011,0,1).  When
> > > > cloumn type is set to "date", the graph will not display.  I am not
> > > > sure why it is not interpreted as a date (per the GV documentation),
> > > > though I assume it may have to do with either the PHP json formatting
> > > > or (relatedly) the quotation marks which surround each json element
> > > > when encoded.
>
> > > > The workaround I settled on (though it is far from elegant) is to
> > > > deliver the information as a string in javascript date notation form
> > > > ("new Date(2011,0,1)").  In the javascript callback function, I then
> > > > do a global replace (removal) of the quotation marks and eval the
> > > > remaining json variable.  This reads the date string as a javascript
> > > > native Date declaration and works correctly.
>
> > > > This approach is less than ideal because it requires special handling
> > > > for other variables in the json-encoded PHP response as well.
>
> > > > Please let me know if you have other suggestions, or thoughts on why
> > > > the GV documented solution you refer to is not working for me.
>
> > > > On May 8, 7:29 am, MC Get Vizzy <[email protected]> wrote:
> > > > > You can send a date with a string of the format "Date(y,m,d)".   For
> > > > > example, "Date(2011,0,1)" would be January first, 2011.  This is
> > > > addressed
> > > > > here:
>
> > > > >http://code.google.com/apis/chart/interactive/docs/dev/implementing_d...
>
> > > > > <http://code.google.com/apis/chart/interactive/docs/dev/implementing_d..
> > > > .>Though
> > > > > I admit it's kind of buried.
>
> > > > > On Fri, May 6, 2011 at 4:15 AM, Teliac <[email protected]> wrote:
> > > > > > I have a question involving a PHP script that created a json-encoded
> > > > > > array and returns it to the client (where it is used to create a
> > > > > > DataTable object).
>
> > > > > > Is there a way to pass a date value from a php server-side script to
> > > > > > the client side GV JavaScript such that it is recognized by the
> > > > > > DataTable object as a 'date' type?
>
> > > > > > Some details:
> > > > > > The php script creates a json encoded array that is requested from
> > > > > > (and returned to) the client via a jQuery ajax call.  The code looks
> > > > > > like this:
>
> > > > > > ----------
> > > > > > // DataTable object.
> > > > > > $dt = array();
>
> > > > > > // Column information
> > > > > > $col_ids = array("DATE","EU");
> > > > > > $col_labels = array("Date","Equity Utilization");
> > > > > > $col_types = array("string","number");  // <- Would like to pass as
> > > > > > "date" rather than "string"
>
> > > > > > // Populate DataTable object with column information.
> > > > > > for ($y = 0; $y < count($col_ids); $y++) {
> > > > > >   $dt["cols"][$y]["id"] = $col_ids[$y];
> > > > > >   $dt["cols"][$y]["label"] = $col_labels[$y];
> > > > > >   $dt["cols"][$y]["type"] = $col_types[$y];
> > > > > > }
>
> > > > > > // Get data from csv file and populate DataTable rows.
> > > > > > $rc = 0;
> > > > > > if (($handle = fopen($csvFile, "r")) !== FALSE) {
> > > > > >     while (($data = fgetcsv($handle)) !== FALSE) {
>
> > > > > >       // We only need two pieces of data here:
> > > > > >       // Date (data[0])
> > > > > >       // EU (data[7])
> > > > > >        // Add data values to $dt array.
> > > > > >        $d = $data[0];
>
> > > > > >       // $d is the date as a string, would like it to be interpreted
> > > > > > as
> > > > > >       // type 'date' when read in by DataTable call on client.
> > > > > >       // Is it possible to parse it here, or otherwise format the
> > > > > > string to
> > > > > >        // be interpreted as type 'date' by GV on client?
> > > > > >        $dt["rows"][$rc]["c"][0]["v"] = $d;
>
> > > > > >        // Format data value as float - necessary for GV to recognize
> > > > > > as number.
> > > > > >        $d = $data[7];
> > > > > >       $d = floatval($d);
> > > > > >       $dt["rows"][$rc]["c"][1]["v"] = $d;
> > > > > >       $rc++;
> > > > > >    }
> > > > > >    fclose($handle);
> > > > > > }
>
> > > > > > // Send information back to client via encoded json
> > > > > > echo json_encode($dt);
> > > > > > ---------
>
> > > > > > The above code works as long as I pass the date with column type
> > > > > > 'string' (would like column type to be 'date'.
>
> > > > > > Thanks in advance for your input.
>
> > > > > > --
> > > > > > 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
>
> ...
>
> read more »

-- 
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.

Reply via email to