my first google chart

77 views
Skip to first unread message

Tammi

unread,
Mar 25, 2019, 10:58:51 AM3/25/19
to Google Visualization API
I am trying to get a returned JSON array to display in a google table chart. Can someone help me with this. So far no luck. 

Here is a sample of the json that is returned. That is what is echo'd out to browser.


          [{
   "ts": 1552903183141,
   "sk": "testing123",
    "val": 0,
      "qual": 0,
     "tg": "hb"
}, {
 "ts": 1552903173141,
   "sk": "testing123",
    "val": 0,
      "qual": 0,
     "tg": "hb"
}, {
 "ts": 1552903163141,
   "sk": "testing123",
    "val": 0,
      "qual": 0,
     "tg": "hb"
}, {
 "ts": 1552903153141,
   "sk": "testing123",
    "val": 0,
      "qual": 0,
     "tg": "hb"
}, {
 "ts": 1552903143141,
   "sk": "testing123",
    "val": 0,
      "qual": 0,
     "tg": "hb"
}, {
 "ts": 1552903133141,
   "sk": "testing123",
    "val": 0,
      "qual": 0,
     "tg": "hb"
}, {
 "ts": 1552903123141,
   "sk": "testing123",
    "val": 0,
      "qual": 0,
     "tg": "hb"
}, {
 "ts": 1552903113141,
   "sk": "testing123",
    "val": 0,
      "qual": 0,
     "tg": "hb"
}, {
 "ts": 1552903103141,
   "sk": "testing123",
    "val": 0,
      "qual": 0,
     "tg": "hb"
}]



this is the graph.



<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['table']});
      google.charts.setOnLoadCallback(drawTable);
      function drawTable() {
        var data = google.visualization.arrayToDataTable([
        data.addColumn('string', 'sk');
        data.addColumn('string', 'ts');
        data.addColumn('string', 'val');
data.addColumn('string', 'qual');
data.addColumn('string', 'tg');
        data.addRow([
         
    ['<?php 
for ($i = 0; $i < count($json); $i++) {
echo $json[$i]['sk'];
echo $json[$i]['ts'];
echo $json[$i]['val'];
echo $json[$i]['qual'];
echo $json[$i]['tg'];
}
  ?>']
          
        ]);

        var table = new google.visualization.Table(document.getElementById('table_div'));

        table.draw(data, {showRowNumber: true, width: '50%', height: '50%'});
      }
    </script>
  </head>
  <body>
  </br>
    <div id="table_div"></div>
  </body>
</html>


thanks for any help

Tammi

unread,
Mar 25, 2019, 11:01:20 AM3/25/19
to Google Visualization API
forgot to mention I am getting this error.

Uncaught SyntaxError: Unexpected token ;

I tried a few different loops to get the data to display, but only get errors.

Jerry O'Brien

unread,
Mar 25, 2019, 11:11:35 AM3/25/19
to google-visua...@googlegroups.com
arrayToDataTable expects an array. Use 

var data = new google.visualization.DataTable();  

if you want to specify/customize columns, or just 

var data = google.visualization.arrayToDataTable( JSON.parse(<?= $json ?>));

(Not tested, but this should get you headed in the right direction ;) 


--
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 google-visualizati...@googlegroups.com.
To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-visualization-api.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-visualization-api/ba547473-4b4e-423b-8fcf-0d285c3b0caf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tammi

unread,
Mar 25, 2019, 11:21:04 AM3/25/19
to Google Visualization API
Thank you. I tried and I still get this error

Uncaught SyntaxError: Unexpected token <




<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['table']});
      google.charts.setOnLoadCallback(drawTable);
      function drawTable() {
        var data = google.visualization.arrayToDataTable( JSON.parse(<?= $json ?>)); 

        var table = new google.visualization.Table(document.getElementById('table_div'));

        table.draw(data, {showRowNumber: true, width: '50%', height: '50%'});
      }
    </script>
  </head>
  <body>
  </br>
    <div id="table_div"></div>
  </body>
</html>
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualization-api+unsub...@googlegroups.com.

Tammi

unread,
Mar 26, 2019, 8:14:02 AM3/26/19
to Google Visualization API
can anyone else help ? give me an example of how to display this data from the json array variable?

Jerry O'Brien

unread,
Mar 26, 2019, 9:09:47 AM3/26/19
to google-visua...@googlegroups.com
JSON.parse expects a string, so add quotes:
JSON.parse('<?php echo $json ?>')

To unsubscribe from this group and stop receiving emails from it, send an email to google-visualizati...@googlegroups.com.

--
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 google-visualizati...@googlegroups.com.

To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-visualization-api.

Tammi

unread,
Mar 26, 2019, 9:19:36 AM3/26/19
to Google Visualization API
Thanks. Tried that I get this error.

Array to string conversion in <b>C:\xampp\htdocs\dashboard\index.php</b> on line <b>151</b><br />


To unsubscribe from this group and stop receiving emails from it, send an email to google-visualization-api+unsub...@googlegroups.com.

--
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 google-visualization-api+unsub...@googlegroups.com.

Jerry O'Brien

unread,
Mar 26, 2019, 8:28:24 PM3/26/19
to google-visua...@googlegroups.com
Have you serialized your PHP array to JSON in index.php?

$json = json_encode($my_array);

Or in your JS, you could say JSON.parse('<?php echo  json_encode($my_array)   ?>')


To unsubscribe from this group and stop receiving emails from it, send an email to google-visualizati...@googlegroups.com.

--
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 google-visualizati...@googlegroups.com.

--
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 google-visualizati...@googlegroups.com.

To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-visualization-api.

Tammi

unread,
Mar 27, 2019, 8:08:55 AM3/27/19
to Google Visualization API
Yes I have done that already. I send a curl request, when the response comes back I :

$json = json_encode($json);

I still get errors and the chart never loads.

To unsubscribe from this group and stop receiving emails from it, send an email to google-visualization-api+unsub...@googlegroups.com.

--
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 google-visualization-api+unsub...@googlegroups.com.

--
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 google-visualization-api+unsub...@googlegroups.com.

Jerry O'Brien

unread,
Mar 27, 2019, 9:20:53 AM3/27/19
to google-visua...@googlegroups.com
Can you post a complete code example?

To unsubscribe from this group and stop receiving emails from it, send an email to google-visualizati...@googlegroups.com.

--
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 google-visualizati...@googlegroups.com.

--
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 google-visualizati...@googlegroups.com.

--
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 google-visualizati...@googlegroups.com.

To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-visualization-api.

Tammi

unread,
Mar 27, 2019, 9:35:05 AM3/27/19
to Google Visualization API
yes here you go.


<?php
echo "<h3>testing</h3>";
 
//initiate the curl request for token
$curl = curl_init();
//create the request array for token
curl_setopt_array($curl, array(
  CURLOPT_URL => "blablabla/token?grant_type=password",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_HTTPHEADER => array(
    "authorization: Basic 123"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
  echo "cURL Error #:" . $err;
} else {
  $json = json_decode($response, true);
  $token = $json['token'];
}

?>

</br>
<form action=# method="post">
Source Key: <input type="text" name="skk">
</br></br>
Date Start: <input type="date" name="dtstart"> (not in use yet)
</br>
Date End: <input type="date" name="dtend"> (not in use yet)
</br>
<input type="submit">
</form>


<?php

     if(isset($_POST['submit']))
{
        $test = $_POST['skk'];//assigning your input value

        if(isset($test))
        {
            $fr=fopen("php://stdin","r");   // open our file pointer to read from stdin
            $input = fgets($fr,128);        // read a maximum of 128 characters
            $input = rtrim($input);         // trim any trailing spaces.
            fclose ($fr);                   // close the file handle
            $result =  $input;              // return the text entered
        }
    }
?>


You have entered : <?php echo $_POST["skk"]; 
echo "</br></br>";




//set my base url for the api
//set my limit 
$limit = "&limit=10";
//create my concatenated url
$endurl = $myurl.$_POST["skk"].$limit;

//send the request
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $endurl,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_HTTPHEADER => array(
    "authorization: bearer $token"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
  echo "cURL Error #:" . $err;
} else {
  $json = json_encode($json);
  //print_r($json);

//create some variables for data points
$ts = $json[0]['ts'];
$val = $json[0]['val'];
$qual = $json[0]['qual'];
$tag = $json[0]['tg'];
$sk = $json[0]['sk'];


//create a formatted date from timestamp for future use
$formatdt = date("Y-m-d H:i:s", substr($dt, 0, 10));
//whats my current date
$cur_date = date("Y-m-d H:i:s");
echo "</br>";

//convert back to timestamps
$date1Timestamp = strtotime($formatdt);
$date2Timestamp = strtotime($cur_date);

//Calculate the difference
$difference = $date2Timestamp - $date1Timestamp;
//echo $difference;

//tellem whats wrong
if ($difference<60)
{
echo "<h2>" . $skk . " is <font color=green>UP </font></h2>";
}
else
{
echo "<h2>" . $skk . " is <font color=red>DOWN </font></h2>";
}

echo "</br>";


//remove brackets
//$suba = substr($json_stringa, 1, -1);
//echo $suba;

?>


<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['table']});
      google.charts.setOnLoadCallback(drawTable);
      function drawTable() {
        var data = google.visualization.arrayToDataTable([
        data.addColumn('string', 'sk');
        data.addColumn('string', 'ts');
        data.addColumn('string', 'val');
data.addColumn('string', 'qual');
data.addColumn('string', 'tg');
        data.addRow([
         
 
          
        ]);

        var table = new google.visualization.Table(document.getElementById('table_div'));

        table.draw(data, {showRowNumber: true, width: '50%', height: '50%'});
      }
    </script>
  </head>
  <body>
  </br>
    <div id="table_div"></div>
  </body>
</html>

To unsubscribe from this group and stop receiving emails from it, send an email to google-visualization-api+unsub...@googlegroups.com.

--
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 google-visualization-api+unsub...@googlegroups.com.

--
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 google-visualization-api+unsub...@googlegroups.com.

--
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 google-visualization-api+unsub...@googlegroups.com.

Jerry O'Brien

unread,
Mar 27, 2019, 12:57:27 PM3/27/19
to google-visua...@googlegroups.com
Have a look at this fiddle using your sample data:  https://jsfiddle.net/obrienje/gq5j6L0u/15/

yes here you go.


To unsubscribe from this group and stop receiving emails from it, send an email to google-visualizati...@googlegroups.com.

--
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 google-visualizati...@googlegroups.com.

--
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 google-visualizati...@googlegroups.com.

--
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 google-visualizati...@googlegroups.com.

--
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 google-visualizati...@googlegroups.com.

To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-visualization-api.

Tammi

unread,
Mar 27, 2019, 3:01:44 PM3/27/19
to Google Visualization API
Awesome. thank you its working now. 
yes here you go.


To unsubscribe from this group and stop receiving emails from it, send an email to google-visualization-api+unsub...@googlegroups.com.

--
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 google-visualization-api+unsub...@googlegroups.com.

--
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 google-visualization-api+unsub...@googlegroups.com.

--
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 google-visualization-api+unsub...@googlegroups.com.

--
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 google-visualization-api+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages