It's generally considered dangerous to use eval to parse JSON. Here's a
relevant excerpt from "JavaScript The Good Parts":
https://www.safaribooksonline.com/library/view/javascript-the-good/9780596517748/apes02.html

The more correct way would be to encode your Dates into the Date String
representation, as I outlined in my previous post, and use either the
DataTable object literal notation or arrayToDataTable to get the data into
a DataTable. Personally, I prefer (and recommend) to use the
arrayToDataTable function.

Assuming that you change your PHP to return Dates in the "correct" way, it
would change your code to the following:
       var dataArray = [
         [{ type: 'date', id: 'Date' }, { type: 'number', id: 'Won/Loss' }]
       ]; // Set up the headers
       // Add the parsed JSON data to the data array.
       dataArray.push.apply(dataArray, $.parseJSON(jsonData));  // note
that this will fail for large arrays, and you may need to use a for loop
instead
       var dataTable = google.visualization.arrayToDataTable(dataArray);

On Wed Feb 04 2015 at 11:35:42 AM Raphael Saldanha <[email protected]>
wrote:

> Just got! Using dataTable.addRows(eval(jsonData)); works. But which is the
> "most correct" method?
>
> On Wed, Feb 4, 2015 at 2:32 PM, 'Sergey Grabkovsky' via Google
> Visualization API <[email protected]> wrote:
>
>> Your issue now is two-fold. The data stored in jsonData is a string, not
>> an array of arrays, so you will need to parse it. You can use
>> $.parseJSON(jsonData) to do that for you. However, the second issue is that
>> that won't work, since "new Date" isn't valid JSON. You will need to use
>> the Date String representation (
>> https://developers.google.com/chart/interactive/docs/datesandtimes#datestring)
>> in order to make that work. Please note that the Date String representation
>> does not work with addRows, so you will either have to use the DataTable
>> object literal notation or arrayToDataTable instead.
>>
>> On Wed Feb 04 2015 at 11:15:47 AM Raphael Saldanha <[email protected]>
>> wrote:
>>
>>> Hi! Really thanks for your attention. I had changed the line, but still
>>> no plot... You can check here:
>>> http://ebib.com.br/plotsus/data/index.html
>>>
>>> On Wed, Feb 4, 2015 at 1:10 PM, 'Sergey Grabkovsky' via Google
>>> Visualization API <[email protected]> wrote:
>>>
>>>> Hi Raphael,
>>>>
>>>> You have an extra set of brackets in your dataTable.addRows call. You
>>>> JSON data already returns an array of arrays, and putting brackets around
>>>> it causes it to be an array of arrays of arrays, which is not the format
>>>> that addRows takes. Simply changing your line to "dataTable.addRows(
>>>> jsonData)" should fix your issues.
>>>>
>>>> On Wed Feb 04 2015 at 7:01:33 AM Raphael Saldanha <[email protected]>
>>>> wrote:
>>>>
>>>>> Hi everyone,
>>>>>
>>>>> I have a php file that returns an array and I'm trying to use those
>>>>> data to create a Google Chart Calendar visualization.
>>>>>
>>>>> The php file is here: http://ebib.com.br/plotsus/data/dados.php
>>>>>
>>>>> And below is my html code. If I just paste the results from PHP inside
>>>>> dataTable.addRows([ ... ]);
>>>>>
>>>>> everything goes ok, but not happens when I use the code below. Please
>>>>> help!
>>>>>
>>>>> <html>
>>>>>   <head>
>>>>>     <script language="javascript" type="text/javascript"
>>>>>         src="
>>>>> http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js";>
>>>>>     </script>
>>>>>     <script type="text/javascript" src="https://www.google.com/jsapi";
>>>>> ></script>
>>>>>     <script type="text/javascript">
>>>>>       google.load("visualization", "1.1", {packages:["calendar"]});
>>>>>       google.setOnLoadCallback(drawChart);
>>>>>
>>>>>    function drawChart() {
>>>>>             var jsonData = $.ajax({
>>>>>                 url: "dados.php",
>>>>>                 dataType: "json",
>>>>>                 async: false
>>>>>             }).responseText;
>>>>>
>>>>>
>>>>>        var dataTable = new google.visualization.DataTable();
>>>>>        dataTable.addColumn({ type: 'date', id: 'Date' });
>>>>>        dataTable.addColumn({ type: 'number', id: 'Won/Loss' });
>>>>>
>>>>>        dataTable.addRows([ jsonData ]);
>>>>>
>>>>>
>>>>>
>>>>>        var chart = new google.visualization.Calendar(document.
>>>>> getElementById('calendar_basic'));
>>>>>
>>>>>        var options = {
>>>>>          title: "Red Sox Attendance",
>>>>>          height: 350,
>>>>>        };
>>>>>
>>>>>        chart.draw(dataTable, options);
>>>>>    }
>>>>>     </script>
>>>>>   </head>
>>>>>   <body>
>>>>>     <div id="calendar_basic" style="width: 1000px; height: 350px;"
>>>>> ></div>
>>>>>   </body>
>>>>> </html>
>>>>>
>>>>>
>>>>>  --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Google Visualization API" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>> To post to this group, send email to
>>>>> [email protected].
>>>>> Visit this group at
>>>>> http://groups.google.com/group/google-visualization-api.
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>  --
>>>> You received this message because you are subscribed to a topic in the
>>>> Google Groups "Google Visualization API" group.
>>>> To unsubscribe from this topic, visit
>>>> https://groups.google.com/d/topic/google-visualization-api/goOPKt0-hYw/unsubscribe
>>>> .
>>>> To unsubscribe from this group and all its topics, send an email to
>>>> [email protected].
>>>>
>>>
>>>> To post to this group, send email to
>>>> [email protected].
>>>> Visit this group at
>>>> http://groups.google.com/group/google-visualization-api.
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>  --
>>> You received this message because you are subscribed to the Google
>>> Groups "Google Visualization API" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To post to this group, send email to
>>> [email protected].
>>> Visit this group at
>>> http://groups.google.com/group/google-visualization-api.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>  --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Google Visualization API" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/google-visualization-api/goOPKt0-hYw/unsubscribe
>> .
>> To unsubscribe from this group and all its topics, send an email to
>> [email protected].
>> To post to this group, send email to
>> [email protected].
>> Visit this group at
>> http://groups.google.com/group/google-visualization-api.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Google Visualization API" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to
> [email protected].
> Visit this group at
> http://groups.google.com/group/google-visualization-api.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/d/optout.
  • [visualization-api] Googl... Raphael Saldanha
    • Re: [visualization-a... 'Sergey Grabkovsky' via Google Visualization API
      • Re: [visualizati... Raphael Saldanha
        • Re: [visuali... 'Sergey Grabkovsky' via Google Visualization API
          • Re: [vis... Raphael Saldanha
            • Re:... 'Sergey Grabkovsky' via Google Visualization API
              • ... Raphael Saldanha
                • ... 'Sergey Grabkovsky' via Google Visualization API
                • ... 'Daniel LaLiberte' via Google Visualization API
                • ... Raphael Saldanha
                • ... 'Sergey Grabkovsky' via Google Visualization API
                • ... Raphael Saldanha
                • ... 'Sergey Grabkovsky' via Google Visualization API
                • ... Raphael Saldanha

Reply via email to