As our documentation states, Google Charts requires your data to be formatted as JSON. There isn't really any way to get around it, but you could format as a CSV if you prefer that instead.
On Thu Sep 04 2014 at 2:39:04 AM Spark Plasma <[email protected]> wrote: > I noticed the example which mentions it can be linked to a database > <https://developers.google.com/chart/interactive/docs/php_example> > requires the data to be formatted in .json format. > > I would like to keep my data in the format which is shown in the Line > Charts > <https://developers.google.com/chart/interactive/docs/gallery/linechart> > section. > > Is it possible to stay away from having to format my sql query to json > format? > > Formatting my SQL query into this so Google Charts has turned into a bit > of a nightmare. > { "cols": [ {"id":"","label":"Topping","pattern":"","type":"string"}, { > "id":"","label":"Slices","pattern":"","type":"number"} ], "rows": [ {"c" > :[{"v":"Mushrooms","f":null},{"v":3,"f":null}]}, {"c":[{"v":"Onions","f": > null},{"v":1,"f":null}]}, {"c":[{"v":"Olives","f":null},{"v":1,"f":null > }]}, {"c":[{"v":"Zucchini","f":null},{"v":1,"f":null}]}, {"c":[{"v": > "Pepperoni","f":null},{"v":2,"f":null}]} ] } > > my getData2.php > $query = sqlsrv_query($conn,'SELECT elapsedTime, vfor FROM voltage1'); > > $table = array(); > $table['cols'] = array( > /* define your DataTable columns here > * each column gets its own array > * syntax of the arrays is: > * label => column label > * type => data type of column (string, number, date, datetime, boolean) > */ > //array('label' => 'elapsedTime), > array('id' => 'Elapsed Time', 'type' => 'string'), > array('id' => 'Forward Voltage', 'type' => 'string'), > // etc... > ); > > $rows = array(); > while($r = sqlsrv_fetch_array($query)) { > $temp = array(); > // each column needs to have data inserted via the $temp array > $temp[] = array('v' => $r['elapsedTime']); > $temp[] = array('v' => $r['vfor']); > // etc... > // insert the temp array into $rows > $rows[] = array('c' => $temp); > } > > // populate the table with rows of data > $table['rows'] = $rows; > > // encode the table as JSON > $jsonTable = json_encode($table); > > // set up header; first two prevent IE from caching queries > // header('Cache-Control: no-cache, must-revalidate'); > // header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); > // header('Content-type: application/json'); > > // return the JSON data > echo $jsonTable; > > My getData2.php output > > {"cols":[{"id":"Elapsed Time","type":"string"},{"id":"Forward > Voltage","type":"string"}],"rows":[{"c":[{"v":1},{"v":"7.362491054"}]},{"c":[{"v":2},{"v":"13.69094212"}]},{"c":[{"v":3},{"v":"18.09654105"}]},{"c":[{"v":4},{"v":"19.96053457"}]},{"c":[{"v":5},{"v":"19.02113033"}]},{"c":[{"v":6},{"v":"15.41026486"}]},{"c":[{"v":7},{"v":"9.635073482"}]},{"c":[{"v":8},{"v":"2.506664671"}]},{"c":[{"v":9},{"v":"-4.973797743"}]},{"c":[{"v":10},{"v":"-11.75570505"}]},{"c":[{"v":11},{"v":"-16.88655851"}]},{"c":[{"v":12},{"v":"-19.64574501"}]},{"c":[{"v":13},{"v":"-19.64574501"}]},{"c":[{"v":14},{"v":"-16.88655851"}]},{"c":[{"v":15},{"v":"-11.75570505"}]},{"c":[{"v":16},{"v":"-4.973797743"}]},{"c":[{"v":17},{"v":"2.506664671"}]},{"c":[{"v":18},{"v":"9.635073482"}]},{"c":[{"v":19},{"v":"15.41026486"}]},{"c":[{"v":20},{"v":"19.02113033"}]},{"c":[{"v":21},{"v":"19.96053457"}]},{"c":[{"v":22},{"v":"18.09654105"}]},{"c":[{"v":23},{"v":"13.69094212"}]},{"c":[{"v":24},{"v":"7.362491054"}]},{"c":[{"v":25},{"v":"7.35089E-15"}]},{"c":[{"v":26},{"v":"-7.362491054"}]},{"c":[{"v":27},{"v":"-13.69094212"}]},{"c":[{"v":28},{"v":"-18.09654105"}]},{"c":[{"v":29},{"v":"-19.96053457"}]},{"c":[{"v":30},{"v":"-19.02113033"}]},{"c":[{"v":31},{"v":"-15.41026486"}]},{"c":[{"v":32},{"v":"-9.635073482"}]},{"c":[{"v":33},{"v":"-2.506664671"}]},{"c":[{"v":34},{"v":"4.973797743"}]},{"c":[{"v":35},{"v":"11.75570505"}]},{"c":[{"v":36},{"v":"16.88655851"}]},{"c":[{"v":37},{"v":"19.64574501"}]},{"c":[{"v":38},{"v":"19.64574501"}]},{"c":[{"v":39},{"v":"16.88655851"}]},{"c":[{"v":40},{"v":"11.75570505"}]},{"c":[{"v":41},{"v":"4.973797743"}]},{"c":[{"v":42},{"v":"-2.506664671"}]},{"c":[{"v":43},{"v":"-9.635073482"}]},{"c":[{"v":44},{"v":"-15.41026486"}]},{"c":[{"v":45},{"v":"-19.02113033"}]},{"c":[{"v":46},{"v":"-19.96053457"}]},{"c":[{"v":47},{"v":"-18.09654105"}]},{"c":[{"v":48},{"v":"-13.69094212"}]},{"c":[{"v":49},{"v":"-7.362491054"}]},{"c":[{"v":50},{"v":"-1.47018E-14"}]}]}?> > > > > -- > 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.
