Friday 15 August 2014

php - Building array and formatting JSON for Google Charting API -


I am working on a project where I am using the Google Charting API and I am using JSN Creating chart data table I want to populate the chart

As a test, I try to make a simple array of data from a database with dynamic data and before doing it, Getting JSN in Format Msya getting.

In Google Docs it indicates that the Jason content should be in the following: <"> cols": [{"id": "", "label": " {"Id": "", "label": "slice", "pattern": "", "type": "number"} "topping", "pattern": "", "type": "string"}, {"C": {"v": ["v": "mushroom", "f": null}, {"v": 3, "f": null}]}, {"c" V ":" ov ":" oven "," f ": null}, {" v ": 1," f ": null}]}, {" c ": [{" v ":" olive "," f ": null {"V": 1, "f": null}]}, {"c": [{"v": "zucchini", "f": null}, {"v": 1 {"v" : 2, "f": null}]}]} <, "f": {"v": "pepperoni", "f": null} / code>

I'm calling a function Jsn code

/ P>

  function testing ($) $ array = array (); $ array ['kernel'] [] = "20-01-13"; $ array ['kernel'] [] = " 21-01-13 "; $ array ['col'] [] =" 22-01-13 "; $ array ['rows'] [] = 22; $ array ['rows'] [] = 26; $ Array ['rows'] [] = 12; Return $ array; }   

Javascript generating chart is as follows:

  & lt; Script & gt; Google.load ('Visualization', '1', {'Package': ['Courchart']}); // Set up callback to run when the Google Visualization API loads. Google.setOnLoadCallback (drawChart); Function drawChart () {var jsonData = $ .ajax ({url: "loadGraph.php", data type: "json", async: false}). Reaction text; Var data = new google.visualization.DataTable (jsonData); // Immediately and draw our charts, passing in some options. Var chart = new google.visualization.PieChart (document.getElementById ('lineGraph')); Chart.draw (data, {width: 400, height: 240}); } & Lt; / Script & gt;   

When I echo json, it is being refunded in the following format

  {"cols": ["20-01-13 "," 21 -01-13 "," 22-01-13 "]," rows ": [22,26,12]}   

And if I call it dataset for Google Try to use the chart I get the following message

  Undefined <1> property can not be read   

I only have one I am creating simple line which is just like a date x axis How many times did that happen with th and y axis?

How do I create an array to get it in the correct format of the Google API chart?

You need

) {$ Array ['cols'] [] = array ('type' = & gt; 'string'); $ Array ['cols'] [] = array ('type' = & gt; 'string'); $ Array ['cols'] [] = array ('type' = & gt; 'string'); Array ('V' = & gt; 22) $ array ['rows'] [] = array (' c '=> array (array (' V '=> '21 -01-13'), Array ('V' => 26)); $ Array ['rows'] [] = array (' c '= & gt; array (array (' V '=> '22 -01-13'), array ('V' => 12) )); Return $ array; } Print json_encode (test ());

Your Jason code looks more:

  {"cols": [{"type": "string"}, {"type": " "" "," String "}, {" type ":" string "}]," rows ": [{" c ": [{" v ":" 20-01-13 "}, {" v ": 22}]}, {"C": [{"v": "21-01-13"}, {"v": 26}]}, {"c": [["V": "22-01-13"}, {"V": 12}]}]}    

No comments:

Post a Comment