Invalid json string for line chart

860 views
Skip to first unread message

soumya reubro

unread,
Aug 14, 2013, 5:53:54 AM8/14/13
to google-visua...@googlegroups.com
Hi,

I am trying to draw a line chart using Google chart in which data is retrieved from mysql table.But i get the error as follows
Error: Invalid JSON string: {"cols":[{"label":"id","type":"string"},{"label":"date","type":"string"}],"rows":["1","2013-08-13 00:00:00"]}{"cols":[{"label":"id","type":"string"},{"label":"date","type":"string"}],"rows":["2","2013-08-13 00:00:00"]}

the json encoded data looks as follows
{"cols":[{"label":"id","type":"string"},{"label":"date","type":"string"}],"rows":["1","2013-08-13 00:00:00"]}{"cols":[{"label":"id","type":"string"},{"label":"date","type":"string"}],"rows":["2","2013-08-13 00:00:00"]}



Here is the code which i used for line chart developement

 <html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="jquery.js"></script>
 <script type="text/javascript" src="jquery-1.6.2.min.js"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
 var jsonData = $.ajax({
          url: "graph1.php",
          dataType:"json",
          async: false
          }).responseText;
  var data = new google.visualization.DataTable(jsonData);
  var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
   chart.draw(data, {width: 400, height: 240});
      }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>


PHP part:


$sql = mysql_query("SELECT * FROM `da_insulin_details` LIMIT 0,2 ");
$rows['cols'] = array( array('label' => 'id', 'type' => 'string'),
    array('label' => 'date', 'type' => 'string'));
while($row = mysql_fetch_array($sql))
  {
 
$rows['rows'] [0] = $row['id'];
$rows['rows'][1] = $row['date'];
$jsonTable = json_encode($rows);
echo $jsonTable;
 
  }


I am new to this section..anybody pls help me to solve the issue

asgallant

unread,
Aug 15, 2013, 12:10:37 AM8/15/13
to google-visua...@googlegroups.com
You have the JSON formatted incorrectly.  It should represent 1 object with "cols" and "rows" properties.  "cols" is an array of objects with "type" (string, mandatory), "label" (string, optional), "id" (string, optional), and "p" (object, optional) parameters.  Rows is an array of objects with "c" (array, mandatory), and "p" (object, optional) parameters.  The "c" parameter of a row object is an array of objects with "v" (string/number, mandatory), "f" (string, optional), and "p" (object, optional) parameters.

Your columns construction is correct, but you only need it once.  Your rows construction needs some work.  Let's start with this row:

["1","2013-08-13 00:00:00"]

should be:

{"c":[{"v":"1"},{"v":"
2013-08-13 00:00:00"}]}

and this row:

["2","2013-08-13 00:00:00"]

should be:

{"c":[{"v":"2"},{"v":"2013-08-13 00:00:00"}]}

Add them to your "rows" array like this:

"rows":[
{"c":[{"v":"1"},{"v":"2013-08-13 00:00:00"}]},{"c":[{"v":"2"},{"v":"2013-08-13 00:00:00"}]}]

You can then combine the whole thing together to make the JSON string:


{"cols":[{"label":"id","type":"string"},{"label":"date","type":"string"}],"rows":[{"c":[{"v":"1"},{"v":"2013-08-13 00:00:00"}]},{"c":[{"v":"2"},{"v":"2013-08-13 00:00:00"}]}]}

soumya reubro

unread,
Aug 15, 2013, 11:51:00 PM8/15/13
to google-visua...@googlegroups.com
Hi,
Thanx alot for your feedback..will do  it as per your suggestion..
Reply all
Reply to author
Forward
0 new messages